Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
60 #ifndef MIN
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #endif
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
69 {
70 const struct got_error *err = NULL;
71 char *path;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
77 free(path);
78 return err;
79 }
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
83 {
84 const struct got_error *err = NULL;
85 FILE *tmpfile = NULL;
86 char *tmppath = NULL;
87 char *path = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
91 path = NULL;
92 goto done;
93 }
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
96 if (err)
97 goto done;
99 if (content) {
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
103 goto done;
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
109 unlink(tmppath);
110 goto done;
113 done:
114 if (fclose(tmpfile) == EOF && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
116 free(tmppath);
117 return err;
120 static const struct got_error *
121 write_head_ref(const char *path_got, struct got_reference *head_ref)
123 const struct got_error *err = NULL;
124 char *refstr = NULL;
126 if (got_ref_is_symbolic(head_ref)) {
127 refstr = got_ref_to_str(head_ref);
128 if (refstr == NULL)
129 return got_error_from_errno("got_ref_to_str");
130 } else {
131 refstr = strdup(got_ref_get_name(head_ref));
132 if (refstr == NULL)
133 return got_error_from_errno("strdup");
135 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
136 free(refstr);
137 return err;
140 const struct got_error *
141 got_worktree_init(const char *path, struct got_reference *head_ref,
142 const char *prefix, struct got_repository *repo)
144 const struct got_error *err = NULL;
145 struct got_object_id *commit_id = NULL;
146 uuid_t uuid;
147 uint32_t uuid_status;
148 int obj_type;
149 char *path_got = NULL;
150 char *formatstr = NULL;
151 char *absprefix = NULL;
152 char *basestr = NULL;
153 char *uuidstr = NULL;
155 if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 err = got_error(GOT_ERR_WORKTREE_REPO);
157 goto done;
160 err = got_ref_resolve(&commit_id, repo, head_ref);
161 if (err)
162 return err;
163 err = got_object_get_type(&obj_type, repo, commit_id);
164 if (err)
165 return err;
166 if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 return got_error(GOT_ERR_OBJ_TYPE);
169 if (!got_path_is_absolute(prefix)) {
170 if (asprintf(&absprefix, "/%s", prefix) == -1)
171 return got_error_from_errno("asprintf");
174 /* Create top-level directory (may already exist). */
175 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 err = got_error_from_errno2("mkdir", path);
177 goto done;
180 /* Create .got directory (may already exist). */
181 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 err = got_error_from_errno("asprintf");
183 goto done;
185 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 err = got_error_from_errno2("mkdir", path_got);
187 goto done;
190 /* Create an empty lock file. */
191 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 if (err)
193 goto done;
195 /* Create an empty file index. */
196 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 if (err)
198 goto done;
200 /* Write the HEAD reference. */
201 err = write_head_ref(path_got, head_ref);
202 if (err)
203 goto done;
205 /* Record our base commit. */
206 err = got_object_id_str(&basestr, commit_id);
207 if (err)
208 goto done;
209 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
210 if (err)
211 goto done;
213 /* Store path to repository. */
214 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 got_repo_get_path(repo));
216 if (err)
217 goto done;
219 /* Store in-repository path prefix. */
220 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 absprefix ? absprefix : prefix);
222 if (err)
223 goto done;
225 /* Generate UUID. */
226 uuid_create(&uuid, &uuid_status);
227 if (uuid_status != uuid_s_ok) {
228 err = got_error_uuid(uuid_status, "uuid_create");
229 goto done;
231 uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 if (uuid_status != uuid_s_ok) {
233 err = got_error_uuid(uuid_status, "uuid_to_string");
234 goto done;
236 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
237 if (err)
238 goto done;
240 /* Stamp work tree with format file. */
241 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 err = got_error_from_errno("asprintf");
243 goto done;
245 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
246 if (err)
247 goto done;
249 done:
250 free(commit_id);
251 free(path_got);
252 free(formatstr);
253 free(absprefix);
254 free(basestr);
255 free(uuidstr);
256 return err;
259 const struct got_error *
260 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 const char *path_prefix)
263 char *absprefix = NULL;
265 if (!got_path_is_absolute(path_prefix)) {
266 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 return got_error_from_errno("asprintf");
269 *match = (strcmp(absprefix ? absprefix : path_prefix,
270 worktree->path_prefix) == 0);
271 free(absprefix);
272 return NULL;
275 const char *
276 got_worktree_get_head_ref_name(struct got_worktree *worktree)
278 return worktree->head_ref_name;
281 const struct got_error *
282 got_worktree_set_head_ref(struct got_worktree *worktree,
283 struct got_reference *head_ref)
285 const struct got_error *err = NULL;
286 char *path_got = NULL, *head_ref_name = NULL;
288 if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 GOT_WORKTREE_GOT_DIR) == -1) {
290 err = got_error_from_errno("asprintf");
291 path_got = NULL;
292 goto done;
295 head_ref_name = strdup(got_ref_get_name(head_ref));
296 if (head_ref_name == NULL) {
297 err = got_error_from_errno("strdup");
298 goto done;
301 err = write_head_ref(path_got, head_ref);
302 if (err)
303 goto done;
305 free(worktree->head_ref_name);
306 worktree->head_ref_name = head_ref_name;
307 done:
308 free(path_got);
309 if (err)
310 free(head_ref_name);
311 return err;
314 struct got_object_id *
315 got_worktree_get_base_commit_id(struct got_worktree *worktree)
317 return worktree->base_commit_id;
320 const struct got_error *
321 got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 struct got_repository *repo, struct got_object_id *commit_id)
324 const struct got_error *err;
325 struct got_object *obj = NULL;
326 char *id_str = NULL;
327 char *path_got = NULL;
329 if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 GOT_WORKTREE_GOT_DIR) == -1) {
331 err = got_error_from_errno("asprintf");
332 path_got = NULL;
333 goto done;
336 err = got_object_open(&obj, repo, commit_id);
337 if (err)
338 return err;
340 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 err = got_error(GOT_ERR_OBJ_TYPE);
342 goto done;
345 /* Record our base commit. */
346 err = got_object_id_str(&id_str, commit_id);
347 if (err)
348 goto done;
349 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
350 if (err)
351 goto done;
353 free(worktree->base_commit_id);
354 worktree->base_commit_id = got_object_id_dup(commit_id);
355 if (worktree->base_commit_id == NULL) {
356 err = got_error_from_errno("got_object_id_dup");
357 goto done;
359 done:
360 if (obj)
361 got_object_close(obj);
362 free(id_str);
363 free(path_got);
364 return err;
367 const struct got_gotconfig *
368 got_worktree_get_gotconfig(struct got_worktree *worktree)
370 return worktree->gotconfig;
373 static const struct got_error *
374 lock_worktree(struct got_worktree *worktree, int operation)
376 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 : got_error_from_errno2("flock",
379 got_worktree_get_root_path(worktree)));
380 return NULL;
383 static const struct got_error *
384 add_dir_on_disk(struct got_worktree *worktree, const char *path)
386 const struct got_error *err = NULL;
387 char *abspath;
389 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 return got_error_from_errno("asprintf");
392 err = got_path_mkdir(abspath);
393 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
394 struct stat sb;
395 err = NULL;
396 if (lstat(abspath, &sb) == -1) {
397 err = got_error_from_errno2("lstat", abspath);
398 } else if (!S_ISDIR(sb.st_mode)) {
399 /* TODO directory is obstructed; do something */
400 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
403 free(abspath);
404 return err;
407 static const struct got_error *
408 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
410 const struct got_error *err = NULL;
411 uint8_t fbuf1[8192];
412 uint8_t fbuf2[8192];
413 size_t flen1 = 0, flen2 = 0;
415 *same = 1;
417 for (;;) {
418 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 if (flen1 == 0 && ferror(f1)) {
420 err = got_error_from_errno("fread");
421 break;
423 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 if (flen2 == 0 && ferror(f2)) {
425 err = got_error_from_errno("fread");
426 break;
428 if (flen1 == 0) {
429 if (flen2 != 0)
430 *same = 0;
431 break;
432 } else if (flen2 == 0) {
433 if (flen1 != 0)
434 *same = 0;
435 break;
436 } else if (flen1 == flen2) {
437 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
438 *same = 0;
439 break;
441 } else {
442 *same = 0;
443 break;
447 return err;
450 static const struct got_error *
451 check_files_equal(int *same, FILE *f1, FILE *f2)
453 struct stat sb;
454 size_t size1, size2;
456 *same = 1;
458 if (fstat(fileno(f1), &sb) != 0)
459 return got_error_from_errno("fstat");
460 size1 = sb.st_size;
462 if (fstat(fileno(f2), &sb) != 0)
463 return got_error_from_errno("fstat");
464 size2 = sb.st_size;
466 if (size1 != size2) {
467 *same = 0;
468 return NULL;
471 if (fseek(f1, 0L, SEEK_SET) == -1)
472 return got_ferror(f1, GOT_ERR_IO);
473 if (fseek(f2, 0L, SEEK_SET) == -1)
474 return got_ferror(f2, GOT_ERR_IO);
476 return check_file_contents_equal(same, f1, f2);
479 /*
480 * Perform a 3-way merge where the file f_orig acts as the common
481 * ancestor, the file f_deriv acts as the first derived version,
482 * and the file f_deriv2 acts as the second derived version.
483 * The merge result will be written to a new file at ondisk_path; any
484 * existing file at this path will be replaced.
485 */
486 static const struct got_error *
487 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
488 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
489 const char *path, uint16_t st_mode,
490 const char *label_orig, const char *label_deriv, const char *label_deriv2,
491 enum got_diff_algorithm diff_algo, struct got_repository *repo,
492 got_worktree_checkout_cb progress_cb, void *progress_arg)
494 const struct got_error *err = NULL;
495 int merged_fd = -1;
496 FILE *f_merged = NULL;
497 char *merged_path = NULL, *base_path = NULL;
498 int overlapcnt = 0;
499 char *parent = NULL;
501 *local_changes_subsumed = 0;
503 err = got_path_dirname(&parent, ondisk_path);
504 if (err)
505 return err;
507 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
508 err = got_error_from_errno("asprintf");
509 goto done;
512 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
513 if (err)
514 goto done;
516 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
517 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
518 if (err)
519 goto done;
521 err = (*progress_cb)(progress_arg,
522 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
523 if (err)
524 goto done;
526 if (fsync(merged_fd) != 0) {
527 err = got_error_from_errno("fsync");
528 goto done;
531 f_merged = fdopen(merged_fd, "r");
532 if (f_merged == NULL) {
533 err = got_error_from_errno("fdopen");
534 goto done;
536 merged_fd = -1;
538 /* Check if a clean merge has subsumed all local changes. */
539 if (overlapcnt == 0) {
540 err = check_files_equal(local_changes_subsumed, f_deriv,
541 f_merged);
542 if (err)
543 goto done;
546 if (fchmod(fileno(f_merged), st_mode) != 0) {
547 err = got_error_from_errno2("fchmod", merged_path);
548 goto done;
551 if (rename(merged_path, ondisk_path) != 0) {
552 err = got_error_from_errno3("rename", merged_path,
553 ondisk_path);
554 goto done;
556 done:
557 if (err) {
558 if (merged_path)
559 unlink(merged_path);
561 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
562 err = got_error_from_errno("close");
563 if (f_merged && fclose(f_merged) == EOF && err == NULL)
564 err = got_error_from_errno("fclose");
565 free(merged_path);
566 free(base_path);
567 free(parent);
568 return err;
571 static const struct got_error *
572 update_symlink(const char *ondisk_path, const char *target_path,
573 size_t target_len)
575 /* This is not atomic but matches what 'ln -sf' does. */
576 if (unlink(ondisk_path) == -1)
577 return got_error_from_errno2("unlink", ondisk_path);
578 if (symlink(target_path, ondisk_path) == -1)
579 return got_error_from_errno3("symlink", target_path,
580 ondisk_path);
581 return NULL;
584 /*
585 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
586 * in the work tree with a file that contains conflict markers and the
587 * conflicting target paths of the original version, a "derived version"
588 * of a symlink from an incoming change, and a local version of the symlink.
590 * The original versions's target path can be NULL if it is not available,
591 * such as if both derived versions added a new symlink at the same path.
593 * The incoming derived symlink target is NULL in case the incoming change
594 * has deleted this symlink.
595 */
596 static const struct got_error *
597 install_symlink_conflict(const char *deriv_target,
598 struct got_object_id *deriv_base_commit_id, const char *orig_target,
599 const char *label_orig, const char *local_target, const char *ondisk_path)
601 const struct got_error *err;
602 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
603 FILE *f = NULL;
605 err = got_object_id_str(&id_str, deriv_base_commit_id);
606 if (err)
607 return got_error_from_errno("asprintf");
609 if (asprintf(&label_deriv, "%s: commit %s",
610 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
611 err = got_error_from_errno("asprintf");
612 goto done;
615 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
616 if (err)
617 goto done;
619 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
620 err = got_error_from_errno2("fchmod", path);
621 goto done;
624 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
625 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
626 deriv_target ? deriv_target : "(symlink was deleted)",
627 orig_target ? label_orig : "",
628 orig_target ? "\n" : "",
629 orig_target ? orig_target : "",
630 orig_target ? "\n" : "",
631 GOT_DIFF_CONFLICT_MARKER_SEP,
632 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
633 err = got_error_from_errno2("fprintf", path);
634 goto done;
637 if (unlink(ondisk_path) == -1) {
638 err = got_error_from_errno2("unlink", ondisk_path);
639 goto done;
641 if (rename(path, ondisk_path) == -1) {
642 err = got_error_from_errno3("rename", path, ondisk_path);
643 goto done;
645 done:
646 if (f != NULL && fclose(f) == EOF && err == NULL)
647 err = got_error_from_errno2("fclose", path);
648 free(path);
649 free(id_str);
650 free(label_deriv);
651 return err;
654 /* forward declaration */
655 static const struct got_error *
656 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
657 const char *, const char *, uint16_t, const char *,
658 struct got_blob_object *, struct got_object_id *,
659 struct got_repository *, got_worktree_checkout_cb, void *);
661 /*
662 * Merge a symlink into the work tree, where blob_orig acts as the common
663 * ancestor, deriv_target is the link target of the first derived version,
664 * and the symlink on disk acts as the second derived version.
665 * Assume that contents of both blobs represent symlinks.
666 */
667 static const struct got_error *
668 merge_symlink(struct got_worktree *worktree,
669 struct got_blob_object *blob_orig, const char *ondisk_path,
670 const char *path, const char *label_orig, const char *deriv_target,
671 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
672 got_worktree_checkout_cb progress_cb, void *progress_arg)
674 const struct got_error *err = NULL;
675 char *ancestor_target = NULL;
676 struct stat sb;
677 ssize_t ondisk_len, deriv_len;
678 char ondisk_target[PATH_MAX];
679 int have_local_change = 0;
680 int have_incoming_change = 0;
682 if (lstat(ondisk_path, &sb) == -1)
683 return got_error_from_errno2("lstat", ondisk_path);
685 ondisk_len = readlink(ondisk_path, ondisk_target,
686 sizeof(ondisk_target));
687 if (ondisk_len == -1) {
688 err = got_error_from_errno2("readlink",
689 ondisk_path);
690 goto done;
692 ondisk_target[ondisk_len] = '\0';
694 if (blob_orig) {
695 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
696 if (err)
697 goto done;
700 if (ancestor_target == NULL ||
701 (ondisk_len != strlen(ancestor_target) ||
702 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
703 have_local_change = 1;
705 deriv_len = strlen(deriv_target);
706 if (ancestor_target == NULL ||
707 (deriv_len != strlen(ancestor_target) ||
708 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
709 have_incoming_change = 1;
711 if (!have_local_change && !have_incoming_change) {
712 if (ancestor_target) {
713 /* Both sides made the same change. */
714 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
715 path);
716 } else if (deriv_len == ondisk_len &&
717 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
718 /* Both sides added the same symlink. */
719 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
720 path);
721 } else {
722 /* Both sides added symlinks which don't match. */
723 err = install_symlink_conflict(deriv_target,
724 deriv_base_commit_id, ancestor_target,
725 label_orig, ondisk_target, ondisk_path);
726 if (err)
727 goto done;
728 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
729 path);
731 } else if (!have_local_change && have_incoming_change) {
732 /* Apply the incoming change. */
733 err = update_symlink(ondisk_path, deriv_target,
734 strlen(deriv_target));
735 if (err)
736 goto done;
737 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
738 } else if (have_local_change && have_incoming_change) {
739 if (deriv_len == ondisk_len &&
740 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
741 /* Both sides made the same change. */
742 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
743 path);
744 } else {
745 err = install_symlink_conflict(deriv_target,
746 deriv_base_commit_id, ancestor_target, label_orig,
747 ondisk_target, ondisk_path);
748 if (err)
749 goto done;
750 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
751 path);
755 done:
756 free(ancestor_target);
757 return err;
760 static const struct got_error *
761 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
763 const struct got_error *err = NULL;
764 char target_path[PATH_MAX];
765 ssize_t target_len;
766 size_t n;
767 FILE *f;
769 *outfile = NULL;
771 f = got_opentemp();
772 if (f == NULL)
773 return got_error_from_errno("got_opentemp");
774 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
775 if (target_len == -1) {
776 err = got_error_from_errno2("readlink", ondisk_path);
777 goto done;
779 n = fwrite(target_path, 1, target_len, f);
780 if (n != target_len) {
781 err = got_ferror(f, GOT_ERR_IO);
782 goto done;
784 if (fflush(f) == EOF) {
785 err = got_error_from_errno("fflush");
786 goto done;
788 if (fseek(f, 0L, SEEK_SET) == -1) {
789 err = got_ferror(f, GOT_ERR_IO);
790 goto done;
792 done:
793 if (err)
794 fclose(f);
795 else
796 *outfile = f;
797 return err;
800 /*
801 * Perform a 3-way merge where blob_orig acts as the common ancestor,
802 * blob_deriv acts as the first derived version, and the file on disk
803 * acts as the second derived version.
804 */
805 static const struct got_error *
806 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
807 struct got_blob_object *blob_orig, const char *ondisk_path,
808 const char *path, uint16_t st_mode, const char *label_orig,
809 struct got_blob_object *blob_deriv,
810 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
811 got_worktree_checkout_cb progress_cb, void *progress_arg)
813 const struct got_error *err = NULL;
814 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
815 char *blob_orig_path = NULL;
816 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
817 char *label_deriv = NULL, *parent = NULL;
819 *local_changes_subsumed = 0;
821 err = got_path_dirname(&parent, ondisk_path);
822 if (err)
823 return err;
825 if (blob_orig) {
826 if (asprintf(&base_path, "%s/got-merge-blob-orig",
827 parent) == -1) {
828 err = got_error_from_errno("asprintf");
829 base_path = NULL;
830 goto done;
833 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
834 if (err)
835 goto done;
836 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
837 blob_orig);
838 if (err)
839 goto done;
840 free(base_path);
841 } else {
842 /*
843 * No common ancestor exists. This is an "add vs add" conflict
844 * and we simply use an empty ancestor file to make both files
845 * appear in the merged result in their entirety.
846 */
847 f_orig = got_opentemp();
848 if (f_orig == NULL) {
849 err = got_error_from_errno("got_opentemp");
850 goto done;
854 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
855 err = got_error_from_errno("asprintf");
856 base_path = NULL;
857 goto done;
860 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
861 if (err)
862 goto done;
863 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
864 blob_deriv);
865 if (err)
866 goto done;
868 err = got_object_id_str(&id_str, deriv_base_commit_id);
869 if (err)
870 goto done;
871 if (asprintf(&label_deriv, "%s: commit %s",
872 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
873 err = got_error_from_errno("asprintf");
874 goto done;
877 /*
878 * In order the run a 3-way merge with a symlink we copy the symlink's
879 * target path into a temporary file and use that file with diff3.
880 */
881 if (S_ISLNK(st_mode)) {
882 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
883 if (err)
884 goto done;
885 } else {
886 int fd;
887 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
888 if (fd == -1) {
889 err = got_error_from_errno2("open", ondisk_path);
890 goto done;
892 f_deriv2 = fdopen(fd, "r");
893 if (f_deriv2 == NULL) {
894 err = got_error_from_errno2("fdopen", ondisk_path);
895 close(fd);
896 goto done;
900 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
901 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
902 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
903 done:
904 if (f_orig && fclose(f_orig) == EOF && err == NULL)
905 err = got_error_from_errno("fclose");
906 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
907 err = got_error_from_errno("fclose");
908 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
909 err = got_error_from_errno("fclose");
910 free(base_path);
911 if (blob_orig_path) {
912 unlink(blob_orig_path);
913 free(blob_orig_path);
915 if (blob_deriv_path) {
916 unlink(blob_deriv_path);
917 free(blob_deriv_path);
919 free(id_str);
920 free(label_deriv);
921 free(parent);
922 return err;
925 static const struct got_error *
926 create_fileindex_entry(struct got_fileindex_entry **new_iep,
927 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
928 int wt_fd, const char *path, struct got_object_id *blob_id)
930 const struct got_error *err = NULL;
931 struct got_fileindex_entry *new_ie;
933 *new_iep = NULL;
935 err = got_fileindex_entry_alloc(&new_ie, path);
936 if (err)
937 return err;
939 err = got_fileindex_entry_update(new_ie, wt_fd, path,
940 blob_id->sha1, base_commit_id->sha1, 1);
941 if (err)
942 goto done;
944 err = got_fileindex_entry_add(fileindex, new_ie);
945 done:
946 if (err)
947 got_fileindex_entry_free(new_ie);
948 else
949 *new_iep = new_ie;
950 return err;
953 static mode_t
954 get_ondisk_perms(int executable, mode_t st_mode)
956 mode_t xbits = S_IXUSR;
958 if (executable) {
959 /* Map read bits to execute bits. */
960 if (st_mode & S_IRGRP)
961 xbits |= S_IXGRP;
962 if (st_mode & S_IROTH)
963 xbits |= S_IXOTH;
964 return st_mode | xbits;
967 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
970 /* forward declaration */
971 static const struct got_error *
972 install_blob(struct got_worktree *worktree, const char *ondisk_path,
973 const char *path, mode_t te_mode, mode_t st_mode,
974 struct got_blob_object *blob, int restoring_missing_file,
975 int reverting_versioned_file, int installing_bad_symlink,
976 int path_is_unversioned, struct got_repository *repo,
977 got_worktree_checkout_cb progress_cb, void *progress_arg);
979 /*
980 * This function assumes that the provided symlink target points at a
981 * safe location in the work tree!
982 */
983 static const struct got_error *
984 replace_existing_symlink(int *did_something, const char *ondisk_path,
985 const char *target_path, size_t target_len)
987 const struct got_error *err = NULL;
988 ssize_t elen;
989 char etarget[PATH_MAX];
990 int fd;
992 *did_something = 0;
994 /*
995 * "Bad" symlinks (those pointing outside the work tree or into the
996 * .got directory) are installed in the work tree as a regular file
997 * which contains the bad symlink target path.
998 * The new symlink target has already been checked for safety by our
999 * caller. If we can successfully open a regular file then we simply
1000 * replace this file with a symlink below.
1002 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1003 if (fd == -1) {
1004 if (!got_err_open_nofollow_on_symlink())
1005 return got_error_from_errno2("open", ondisk_path);
1007 /* We are updating an existing on-disk symlink. */
1008 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1009 if (elen == -1)
1010 return got_error_from_errno2("readlink", ondisk_path);
1012 if (elen == target_len &&
1013 memcmp(etarget, target_path, target_len) == 0)
1014 return NULL; /* nothing to do */
1017 *did_something = 1;
1018 err = update_symlink(ondisk_path, target_path, target_len);
1019 if (fd != -1 && close(fd) == -1 && err == NULL)
1020 err = got_error_from_errno2("close", ondisk_path);
1021 return err;
1024 static const struct got_error *
1025 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1026 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1028 const struct got_error *err = NULL;
1029 char canonpath[PATH_MAX];
1030 char *path_got = NULL;
1032 *is_bad_symlink = 0;
1034 if (target_len >= sizeof(canonpath)) {
1035 *is_bad_symlink = 1;
1036 return NULL;
1040 * We do not use realpath(3) to resolve the symlink's target
1041 * path because we don't want to resolve symlinks recursively.
1042 * Instead we make the path absolute and then canonicalize it.
1043 * Relative symlink target lookup should begin at the directory
1044 * in which the blob object is being installed.
1046 if (!got_path_is_absolute(target_path)) {
1047 char *abspath, *parent;
1048 err = got_path_dirname(&parent, ondisk_path);
1049 if (err)
1050 return err;
1051 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1052 free(parent);
1053 return got_error_from_errno("asprintf");
1055 free(parent);
1056 if (strlen(abspath) >= sizeof(canonpath)) {
1057 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1058 free(abspath);
1059 return err;
1061 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1062 free(abspath);
1063 if (err)
1064 return err;
1065 } else {
1066 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1067 if (err)
1068 return err;
1071 /* Only allow symlinks pointing at paths within the work tree. */
1072 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1073 *is_bad_symlink = 1;
1074 return NULL;
1077 /* Do not allow symlinks pointing into the .got directory. */
1078 if (asprintf(&path_got, "%s/%s", wtroot_path,
1079 GOT_WORKTREE_GOT_DIR) == -1)
1080 return got_error_from_errno("asprintf");
1081 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1082 *is_bad_symlink = 1;
1084 free(path_got);
1085 return NULL;
1088 static const struct got_error *
1089 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1090 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1091 int restoring_missing_file, int reverting_versioned_file,
1092 int path_is_unversioned, int allow_bad_symlinks,
1093 struct got_repository *repo,
1094 got_worktree_checkout_cb progress_cb, void *progress_arg)
1096 const struct got_error *err = NULL;
1097 char target_path[PATH_MAX];
1098 size_t len, target_len = 0;
1099 char *path_got = NULL;
1100 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1101 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1103 *is_bad_symlink = 0;
1106 * Blob object content specifies the target path of the link.
1107 * If a symbolic link cannot be installed we instead create
1108 * a regular file which contains the link target path stored
1109 * in the blob object.
1111 do {
1112 err = got_object_blob_read_block(&len, blob);
1113 if (len + target_len >= sizeof(target_path)) {
1114 /* Path too long; install as a regular file. */
1115 *is_bad_symlink = 1;
1116 got_object_blob_rewind(blob);
1117 return install_blob(worktree, ondisk_path, path,
1118 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1119 restoring_missing_file, reverting_versioned_file,
1120 1, path_is_unversioned, repo, progress_cb,
1121 progress_arg);
1123 if (len > 0) {
1124 /* Skip blob object header first time around. */
1125 memcpy(target_path + target_len, buf + hdrlen,
1126 len - hdrlen);
1127 target_len += len - hdrlen;
1128 hdrlen = 0;
1130 } while (len != 0);
1131 target_path[target_len] = '\0';
1133 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1134 ondisk_path, worktree->root_path);
1135 if (err)
1136 return err;
1138 if (*is_bad_symlink && !allow_bad_symlinks) {
1139 /* install as a regular file */
1140 got_object_blob_rewind(blob);
1141 err = install_blob(worktree, ondisk_path, path,
1142 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1143 restoring_missing_file, reverting_versioned_file, 1,
1144 path_is_unversioned, repo, progress_cb, progress_arg);
1145 goto done;
1148 if (symlink(target_path, ondisk_path) == -1) {
1149 if (errno == EEXIST) {
1150 int symlink_replaced;
1151 if (path_is_unversioned) {
1152 err = (*progress_cb)(progress_arg,
1153 GOT_STATUS_UNVERSIONED, path);
1154 goto done;
1156 err = replace_existing_symlink(&symlink_replaced,
1157 ondisk_path, target_path, target_len);
1158 if (err)
1159 goto done;
1160 if (progress_cb) {
1161 if (symlink_replaced) {
1162 err = (*progress_cb)(progress_arg,
1163 reverting_versioned_file ?
1164 GOT_STATUS_REVERT :
1165 GOT_STATUS_UPDATE, path);
1166 } else {
1167 err = (*progress_cb)(progress_arg,
1168 GOT_STATUS_EXISTS, path);
1171 goto done; /* Nothing else to do. */
1174 if (errno == ENOENT) {
1175 char *parent;
1176 err = got_path_dirname(&parent, ondisk_path);
1177 if (err)
1178 goto done;
1179 err = add_dir_on_disk(worktree, parent);
1180 free(parent);
1181 if (err)
1182 goto done;
1184 * Retry, and fall through to error handling
1185 * below if this second attempt fails.
1187 if (symlink(target_path, ondisk_path) != -1) {
1188 err = NULL; /* success */
1189 goto done;
1193 /* Handle errors from first or second creation attempt. */
1194 if (errno == ENAMETOOLONG) {
1195 /* bad target path; install as a regular file */
1196 *is_bad_symlink = 1;
1197 got_object_blob_rewind(blob);
1198 err = install_blob(worktree, ondisk_path, path,
1199 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1200 restoring_missing_file, reverting_versioned_file, 1,
1201 path_is_unversioned, repo,
1202 progress_cb, progress_arg);
1203 } else if (errno == ENOTDIR) {
1204 err = got_error_path(ondisk_path,
1205 GOT_ERR_FILE_OBSTRUCTED);
1206 } else {
1207 err = got_error_from_errno3("symlink",
1208 target_path, ondisk_path);
1210 } else if (progress_cb)
1211 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1212 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1213 done:
1214 free(path_got);
1215 return err;
1218 static const struct got_error *
1219 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1220 const char *path, mode_t te_mode, mode_t st_mode,
1221 struct got_blob_object *blob, int restoring_missing_file,
1222 int reverting_versioned_file, int installing_bad_symlink,
1223 int path_is_unversioned, struct got_repository *repo,
1224 got_worktree_checkout_cb progress_cb, void *progress_arg)
1226 const struct got_error *err = NULL;
1227 int fd = -1;
1228 size_t len, hdrlen;
1229 int update = 0;
1230 char *tmppath = NULL;
1232 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1233 GOT_DEFAULT_FILE_MODE);
1234 if (fd == -1) {
1235 if (errno == ENOENT) {
1236 char *parent;
1237 err = got_path_dirname(&parent, path);
1238 if (err)
1239 return err;
1240 err = add_dir_on_disk(worktree, parent);
1241 free(parent);
1242 if (err)
1243 return err;
1244 fd = open(ondisk_path,
1245 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1246 GOT_DEFAULT_FILE_MODE);
1247 if (fd == -1)
1248 return got_error_from_errno2("open",
1249 ondisk_path);
1250 } else if (errno == EEXIST) {
1251 if (path_is_unversioned) {
1252 err = (*progress_cb)(progress_arg,
1253 GOT_STATUS_UNVERSIONED, path);
1254 goto done;
1256 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1257 !S_ISREG(st_mode) && !installing_bad_symlink) {
1258 /* TODO file is obstructed; do something */
1259 err = got_error_path(ondisk_path,
1260 GOT_ERR_FILE_OBSTRUCTED);
1261 goto done;
1262 } else {
1263 err = got_opentemp_named_fd(&tmppath, &fd,
1264 ondisk_path);
1265 if (err)
1266 goto done;
1267 update = 1;
1269 } else
1270 return got_error_from_errno2("open", ondisk_path);
1273 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1274 err = got_error_from_errno2("fchmod",
1275 update ? tmppath : ondisk_path);
1276 goto done;
1279 if (progress_cb) {
1280 if (restoring_missing_file)
1281 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1282 path);
1283 else if (reverting_versioned_file)
1284 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1285 path);
1286 else
1287 err = (*progress_cb)(progress_arg,
1288 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1289 if (err)
1290 goto done;
1293 hdrlen = got_object_blob_get_hdrlen(blob);
1294 do {
1295 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1296 err = got_object_blob_read_block(&len, blob);
1297 if (err)
1298 break;
1299 if (len > 0) {
1300 /* Skip blob object header first time around. */
1301 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1302 if (outlen == -1) {
1303 err = got_error_from_errno("write");
1304 goto done;
1305 } else if (outlen != len - hdrlen) {
1306 err = got_error(GOT_ERR_IO);
1307 goto done;
1309 hdrlen = 0;
1311 } while (len != 0);
1313 if (fsync(fd) != 0) {
1314 err = got_error_from_errno("fsync");
1315 goto done;
1318 if (update) {
1319 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1320 err = got_error_from_errno2("unlink", ondisk_path);
1321 goto done;
1323 if (rename(tmppath, ondisk_path) != 0) {
1324 err = got_error_from_errno3("rename", tmppath,
1325 ondisk_path);
1326 goto done;
1328 free(tmppath);
1329 tmppath = NULL;
1332 done:
1333 if (fd != -1 && close(fd) == -1 && err == NULL)
1334 err = got_error_from_errno("close");
1335 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1336 err = got_error_from_errno2("unlink", tmppath);
1337 free(tmppath);
1338 return err;
1341 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1342 static const struct got_error *
1343 get_modified_file_content_status(unsigned char *status, FILE *f)
1345 const struct got_error *err = NULL;
1346 const char *markers[3] = {
1347 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1348 GOT_DIFF_CONFLICT_MARKER_SEP,
1349 GOT_DIFF_CONFLICT_MARKER_END
1351 int i = 0;
1352 char *line = NULL;
1353 size_t linesize = 0;
1354 ssize_t linelen;
1356 while (*status == GOT_STATUS_MODIFY) {
1357 linelen = getline(&line, &linesize, f);
1358 if (linelen == -1) {
1359 if (feof(f))
1360 break;
1361 err = got_ferror(f, GOT_ERR_IO);
1362 break;
1365 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1366 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1367 == 0)
1368 *status = GOT_STATUS_CONFLICT;
1369 else
1370 i++;
1373 free(line);
1375 return err;
1378 static int
1379 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1381 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1382 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1385 static int
1386 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1388 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1389 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1390 ie->mtime_sec == sb->st_mtim.tv_sec &&
1391 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1392 ie->size == (sb->st_size & 0xffffffff) &&
1393 !xbit_differs(ie, sb->st_mode));
1396 static unsigned char
1397 get_staged_status(struct got_fileindex_entry *ie)
1399 switch (got_fileindex_entry_stage_get(ie)) {
1400 case GOT_FILEIDX_STAGE_ADD:
1401 return GOT_STATUS_ADD;
1402 case GOT_FILEIDX_STAGE_DELETE:
1403 return GOT_STATUS_DELETE;
1404 case GOT_FILEIDX_STAGE_MODIFY:
1405 return GOT_STATUS_MODIFY;
1406 default:
1407 return GOT_STATUS_NO_CHANGE;
1411 static const struct got_error *
1412 get_symlink_modification_status(unsigned char *status,
1413 struct got_fileindex_entry *ie, const char *abspath,
1414 int dirfd, const char *de_name, struct got_blob_object *blob)
1416 const struct got_error *err = NULL;
1417 char target_path[PATH_MAX];
1418 char etarget[PATH_MAX];
1419 ssize_t elen;
1420 size_t len, target_len = 0;
1421 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1422 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1424 *status = GOT_STATUS_NO_CHANGE;
1426 /* Blob object content specifies the target path of the link. */
1427 do {
1428 err = got_object_blob_read_block(&len, blob);
1429 if (err)
1430 return err;
1431 if (len + target_len >= sizeof(target_path)) {
1433 * Should not happen. The blob contents were OK
1434 * when this symlink was installed.
1436 return got_error(GOT_ERR_NO_SPACE);
1438 if (len > 0) {
1439 /* Skip blob object header first time around. */
1440 memcpy(target_path + target_len, buf + hdrlen,
1441 len - hdrlen);
1442 target_len += len - hdrlen;
1443 hdrlen = 0;
1445 } while (len != 0);
1446 target_path[target_len] = '\0';
1448 if (dirfd != -1) {
1449 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1450 if (elen == -1)
1451 return got_error_from_errno2("readlinkat", abspath);
1452 } else {
1453 elen = readlink(abspath, etarget, sizeof(etarget));
1454 if (elen == -1)
1455 return got_error_from_errno2("readlink", abspath);
1458 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1459 *status = GOT_STATUS_MODIFY;
1461 return NULL;
1464 static const struct got_error *
1465 get_file_status(unsigned char *status, struct stat *sb,
1466 struct got_fileindex_entry *ie, const char *abspath,
1467 int dirfd, const char *de_name, struct got_repository *repo)
1469 const struct got_error *err = NULL;
1470 struct got_object_id id;
1471 size_t hdrlen;
1472 int fd = -1;
1473 FILE *f = NULL;
1474 uint8_t fbuf[8192];
1475 struct got_blob_object *blob = NULL;
1476 size_t flen, blen;
1477 unsigned char staged_status = get_staged_status(ie);
1479 *status = GOT_STATUS_NO_CHANGE;
1480 memset(sb, 0, sizeof(*sb));
1483 * Whenever the caller provides a directory descriptor and a
1484 * directory entry name for the file, use them! This prevents
1485 * race conditions if filesystem paths change beneath our feet.
1487 if (dirfd != -1) {
1488 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1489 if (errno == ENOENT) {
1490 if (got_fileindex_entry_has_file_on_disk(ie))
1491 *status = GOT_STATUS_MISSING;
1492 else
1493 *status = GOT_STATUS_DELETE;
1494 goto done;
1496 err = got_error_from_errno2("fstatat", abspath);
1497 goto done;
1499 } else {
1500 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1501 if (fd == -1 && errno != ENOENT &&
1502 !got_err_open_nofollow_on_symlink())
1503 return got_error_from_errno2("open", abspath);
1504 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1505 if (lstat(abspath, sb) == -1)
1506 return got_error_from_errno2("lstat", abspath);
1507 } else if (fd == -1 || fstat(fd, sb) == -1) {
1508 if (errno == ENOENT) {
1509 if (got_fileindex_entry_has_file_on_disk(ie))
1510 *status = GOT_STATUS_MISSING;
1511 else
1512 *status = GOT_STATUS_DELETE;
1513 goto done;
1515 err = got_error_from_errno2("fstat", abspath);
1516 goto done;
1520 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1521 *status = GOT_STATUS_OBSTRUCTED;
1522 goto done;
1525 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1526 *status = GOT_STATUS_DELETE;
1527 goto done;
1528 } else if (!got_fileindex_entry_has_blob(ie) &&
1529 staged_status != GOT_STATUS_ADD) {
1530 *status = GOT_STATUS_ADD;
1531 goto done;
1534 if (!stat_info_differs(ie, sb))
1535 goto done;
1537 if (S_ISLNK(sb->st_mode) &&
1538 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1539 *status = GOT_STATUS_MODIFY;
1540 goto done;
1543 if (staged_status == GOT_STATUS_MODIFY ||
1544 staged_status == GOT_STATUS_ADD)
1545 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1546 else
1547 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1549 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1550 if (err)
1551 goto done;
1553 if (S_ISLNK(sb->st_mode)) {
1554 err = get_symlink_modification_status(status, ie,
1555 abspath, dirfd, de_name, blob);
1556 goto done;
1559 if (dirfd != -1) {
1560 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1561 if (fd == -1) {
1562 err = got_error_from_errno2("openat", abspath);
1563 goto done;
1567 f = fdopen(fd, "r");
1568 if (f == NULL) {
1569 err = got_error_from_errno2("fdopen", abspath);
1570 goto done;
1572 fd = -1;
1573 hdrlen = got_object_blob_get_hdrlen(blob);
1574 for (;;) {
1575 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1576 err = got_object_blob_read_block(&blen, blob);
1577 if (err)
1578 goto done;
1579 /* Skip length of blob object header first time around. */
1580 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1581 if (flen == 0 && ferror(f)) {
1582 err = got_error_from_errno("fread");
1583 goto done;
1585 if (blen - hdrlen == 0) {
1586 if (flen != 0)
1587 *status = GOT_STATUS_MODIFY;
1588 break;
1589 } else if (flen == 0) {
1590 if (blen - hdrlen != 0)
1591 *status = GOT_STATUS_MODIFY;
1592 break;
1593 } else if (blen - hdrlen == flen) {
1594 /* Skip blob object header first time around. */
1595 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1596 *status = GOT_STATUS_MODIFY;
1597 break;
1599 } else {
1600 *status = GOT_STATUS_MODIFY;
1601 break;
1603 hdrlen = 0;
1606 if (*status == GOT_STATUS_MODIFY) {
1607 rewind(f);
1608 err = get_modified_file_content_status(status, f);
1609 } else if (xbit_differs(ie, sb->st_mode))
1610 *status = GOT_STATUS_MODE_CHANGE;
1611 done:
1612 if (blob)
1613 got_object_blob_close(blob);
1614 if (f != NULL && fclose(f) == EOF && err == NULL)
1615 err = got_error_from_errno2("fclose", abspath);
1616 if (fd != -1 && close(fd) == -1 && err == NULL)
1617 err = got_error_from_errno2("close", abspath);
1618 return err;
1622 * Update timestamps in the file index if a file is unmodified and
1623 * we had to run a full content comparison to find out.
1625 static const struct got_error *
1626 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1627 struct got_fileindex_entry *ie, struct stat *sb)
1629 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1630 return got_fileindex_entry_update(ie, wt_fd, path,
1631 ie->blob_sha1, ie->commit_sha1, 1);
1633 return NULL;
1636 static const struct got_error *
1637 update_blob(struct got_worktree *worktree,
1638 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1639 struct got_tree_entry *te, const char *path,
1640 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1641 void *progress_arg)
1643 const struct got_error *err = NULL;
1644 struct got_blob_object *blob = NULL;
1645 char *ondisk_path;
1646 unsigned char status = GOT_STATUS_NO_CHANGE;
1647 struct stat sb;
1649 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1650 return got_error_from_errno("asprintf");
1652 if (ie) {
1653 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1654 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1655 goto done;
1657 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1658 repo);
1659 if (err)
1660 goto done;
1661 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1662 sb.st_mode = got_fileindex_perms_to_st(ie);
1663 } else {
1664 if (stat(ondisk_path, &sb) == -1) {
1665 if (errno != ENOENT) {
1666 err = got_error_from_errno2("stat",
1667 ondisk_path);
1668 goto done;
1670 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1671 status = GOT_STATUS_UNVERSIONED;
1672 } else {
1673 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1674 status = GOT_STATUS_UNVERSIONED;
1675 else
1676 status = GOT_STATUS_OBSTRUCTED;
1680 if (status == GOT_STATUS_OBSTRUCTED) {
1681 if (ie)
1682 got_fileindex_entry_mark_skipped(ie);
1683 err = (*progress_cb)(progress_arg, status, path);
1684 goto done;
1686 if (status == GOT_STATUS_CONFLICT) {
1687 if (ie)
1688 got_fileindex_entry_mark_skipped(ie);
1689 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1690 path);
1691 goto done;
1694 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1695 (S_ISLNK(te->mode) ||
1696 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1698 * This is a regular file or an installed bad symlink.
1699 * If the file index indicates that this file is already
1700 * up-to-date with respect to the repository we can skip
1701 * updating contents of this file.
1703 if (got_fileindex_entry_has_commit(ie) &&
1704 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1705 SHA1_DIGEST_LENGTH) == 0) {
1706 /* Same commit. */
1707 err = sync_timestamps(worktree->root_fd,
1708 path, status, ie, &sb);
1709 if (err)
1710 goto done;
1711 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1712 path);
1713 goto done;
1715 if (got_fileindex_entry_has_blob(ie) &&
1716 memcmp(ie->blob_sha1, te->id.sha1,
1717 SHA1_DIGEST_LENGTH) == 0) {
1718 /* Different commit but the same blob. */
1719 err = sync_timestamps(worktree->root_fd,
1720 path, status, ie, &sb);
1721 if (err)
1722 goto done;
1723 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1724 path);
1725 goto done;
1729 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1730 if (err)
1731 goto done;
1733 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1734 int update_timestamps;
1735 struct got_blob_object *blob2 = NULL;
1736 char *label_orig = NULL;
1737 if (got_fileindex_entry_has_blob(ie)) {
1738 struct got_object_id id2;
1739 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1740 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1741 if (err)
1742 goto done;
1744 if (got_fileindex_entry_has_commit(ie)) {
1745 char id_str[SHA1_DIGEST_STRING_LENGTH];
1746 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1747 sizeof(id_str)) == NULL) {
1748 err = got_error_path(id_str,
1749 GOT_ERR_BAD_OBJ_ID_STR);
1750 goto done;
1752 if (asprintf(&label_orig, "%s: commit %s",
1753 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1754 err = got_error_from_errno("asprintf");
1755 goto done;
1758 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1759 char *link_target;
1760 err = got_object_blob_read_to_str(&link_target, blob);
1761 if (err)
1762 goto done;
1763 err = merge_symlink(worktree, blob2, ondisk_path, path,
1764 label_orig, link_target, worktree->base_commit_id,
1765 repo, progress_cb, progress_arg);
1766 free(link_target);
1767 } else {
1768 err = merge_blob(&update_timestamps, worktree, blob2,
1769 ondisk_path, path, sb.st_mode, label_orig, blob,
1770 worktree->base_commit_id, repo,
1771 progress_cb, progress_arg);
1773 free(label_orig);
1774 if (blob2)
1775 got_object_blob_close(blob2);
1776 if (err)
1777 goto done;
1779 * Do not update timestamps of files with local changes.
1780 * Otherwise, a future status walk would treat them as
1781 * unmodified files again.
1783 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1784 blob->id.sha1, worktree->base_commit_id->sha1,
1785 update_timestamps);
1786 } else if (status == GOT_STATUS_MODE_CHANGE) {
1787 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1788 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1789 } else if (status == GOT_STATUS_DELETE) {
1790 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1791 if (err)
1792 goto done;
1793 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1794 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1795 if (err)
1796 goto done;
1797 } else {
1798 int is_bad_symlink = 0;
1799 if (S_ISLNK(te->mode)) {
1800 err = install_symlink(&is_bad_symlink, worktree,
1801 ondisk_path, path, blob,
1802 status == GOT_STATUS_MISSING, 0,
1803 status == GOT_STATUS_UNVERSIONED, 0,
1804 repo, progress_cb, progress_arg);
1805 } else {
1806 err = install_blob(worktree, ondisk_path, path,
1807 te->mode, sb.st_mode, blob,
1808 status == GOT_STATUS_MISSING, 0, 0,
1809 status == GOT_STATUS_UNVERSIONED, repo,
1810 progress_cb, progress_arg);
1812 if (err)
1813 goto done;
1815 if (ie) {
1816 err = got_fileindex_entry_update(ie,
1817 worktree->root_fd, path, blob->id.sha1,
1818 worktree->base_commit_id->sha1, 1);
1819 } else {
1820 err = create_fileindex_entry(&ie, fileindex,
1821 worktree->base_commit_id, worktree->root_fd, path,
1822 &blob->id);
1824 if (err)
1825 goto done;
1827 if (is_bad_symlink) {
1828 got_fileindex_entry_filetype_set(ie,
1829 GOT_FILEIDX_MODE_BAD_SYMLINK);
1832 got_object_blob_close(blob);
1833 done:
1834 free(ondisk_path);
1835 return err;
1838 static const struct got_error *
1839 remove_ondisk_file(const char *root_path, const char *path)
1841 const struct got_error *err = NULL;
1842 char *ondisk_path = NULL, *parent = NULL;
1844 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1845 return got_error_from_errno("asprintf");
1847 if (unlink(ondisk_path) == -1) {
1848 if (errno != ENOENT)
1849 err = got_error_from_errno2("unlink", ondisk_path);
1850 } else {
1851 size_t root_len = strlen(root_path);
1852 err = got_path_dirname(&parent, ondisk_path);
1853 if (err)
1854 goto done;
1855 while (got_path_cmp(parent, root_path,
1856 strlen(parent), root_len) != 0) {
1857 free(ondisk_path);
1858 ondisk_path = parent;
1859 parent = NULL;
1860 if (rmdir(ondisk_path) == -1) {
1861 if (errno != ENOTEMPTY)
1862 err = got_error_from_errno2("rmdir",
1863 ondisk_path);
1864 break;
1866 err = got_path_dirname(&parent, ondisk_path);
1867 if (err)
1868 break;
1871 done:
1872 free(ondisk_path);
1873 free(parent);
1874 return err;
1877 static const struct got_error *
1878 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1879 struct got_fileindex_entry *ie, struct got_repository *repo,
1880 got_worktree_checkout_cb progress_cb, void *progress_arg)
1882 const struct got_error *err = NULL;
1883 unsigned char status;
1884 struct stat sb;
1885 char *ondisk_path;
1887 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1888 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1890 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1891 == -1)
1892 return got_error_from_errno("asprintf");
1894 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1895 if (err)
1896 goto done;
1898 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
1899 char ondisk_target[PATH_MAX];
1900 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
1901 sizeof(ondisk_target));
1902 if (ondisk_len == -1) {
1903 err = got_error_from_errno2("readlink", ondisk_path);
1904 goto done;
1906 ondisk_target[ondisk_len] = '\0';
1907 err = install_symlink_conflict(NULL, worktree->base_commit_id,
1908 NULL, NULL, /* XXX pass common ancestor info? */
1909 ondisk_target, ondisk_path);
1910 if (err)
1911 goto done;
1912 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1913 ie->path);
1914 goto done;
1917 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1918 status == GOT_STATUS_ADD) {
1919 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1920 if (err)
1921 goto done;
1923 * Preserve the working file and change the deleted blob's
1924 * entry into a schedule-add entry.
1926 err = got_fileindex_entry_update(ie, worktree->root_fd,
1927 ie->path, NULL, NULL, 0);
1928 } else {
1929 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1930 if (err)
1931 goto done;
1932 if (status == GOT_STATUS_NO_CHANGE) {
1933 err = remove_ondisk_file(worktree->root_path, ie->path);
1934 if (err)
1935 goto done;
1937 got_fileindex_entry_remove(fileindex, ie);
1939 done:
1940 free(ondisk_path);
1941 return err;
1944 struct diff_cb_arg {
1945 struct got_fileindex *fileindex;
1946 struct got_worktree *worktree;
1947 struct got_repository *repo;
1948 got_worktree_checkout_cb progress_cb;
1949 void *progress_arg;
1950 got_cancel_cb cancel_cb;
1951 void *cancel_arg;
1954 static const struct got_error *
1955 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1956 struct got_tree_entry *te, const char *parent_path)
1958 struct diff_cb_arg *a = arg;
1960 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1961 return got_error(GOT_ERR_CANCELLED);
1963 return update_blob(a->worktree, a->fileindex, ie, te,
1964 ie->path, a->repo, a->progress_cb, a->progress_arg);
1967 static const struct got_error *
1968 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1970 struct diff_cb_arg *a = arg;
1972 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1973 return got_error(GOT_ERR_CANCELLED);
1975 return delete_blob(a->worktree, a->fileindex, ie,
1976 a->repo, a->progress_cb, a->progress_arg);
1979 static const struct got_error *
1980 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1982 struct diff_cb_arg *a = arg;
1983 const struct got_error *err;
1984 char *path;
1986 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1987 return got_error(GOT_ERR_CANCELLED);
1989 if (got_object_tree_entry_is_submodule(te))
1990 return NULL;
1992 if (asprintf(&path, "%s%s%s", parent_path,
1993 parent_path[0] ? "/" : "", te->name)
1994 == -1)
1995 return got_error_from_errno("asprintf");
1997 if (S_ISDIR(te->mode))
1998 err = add_dir_on_disk(a->worktree, path);
1999 else
2000 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2001 a->repo, a->progress_cb, a->progress_arg);
2003 free(path);
2004 return err;
2007 const struct got_error *
2008 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2010 uint32_t uuid_status;
2012 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2013 if (uuid_status != uuid_s_ok) {
2014 *uuidstr = NULL;
2015 return got_error_uuid(uuid_status, "uuid_to_string");
2018 return NULL;
2021 static const struct got_error *
2022 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2024 const struct got_error *err = NULL;
2025 char *uuidstr = NULL;
2027 *refname = NULL;
2029 err = got_worktree_get_uuid(&uuidstr, worktree);
2030 if (err)
2031 return err;
2033 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2034 err = got_error_from_errno("asprintf");
2035 *refname = NULL;
2037 free(uuidstr);
2038 return err;
2041 const struct got_error *
2042 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2044 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2047 static const struct got_error *
2048 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2050 return get_ref_name(refname, worktree,
2051 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2054 static const struct got_error *
2055 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2057 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2060 static const struct got_error *
2061 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2063 return get_ref_name(refname, worktree,
2064 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2067 static const struct got_error *
2068 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2070 return get_ref_name(refname, worktree,
2071 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2074 static const struct got_error *
2075 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2077 return get_ref_name(refname, worktree,
2078 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2081 static const struct got_error *
2082 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2084 return get_ref_name(refname, worktree,
2085 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2088 static const struct got_error *
2089 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2091 return get_ref_name(refname, worktree,
2092 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2095 static const struct got_error *
2096 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2098 return get_ref_name(refname, worktree,
2099 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2102 const struct got_error *
2103 got_worktree_get_histedit_script_path(char **path,
2104 struct got_worktree *worktree)
2106 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2107 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2108 *path = NULL;
2109 return got_error_from_errno("asprintf");
2111 return NULL;
2114 static const struct got_error *
2115 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2117 return get_ref_name(refname, worktree,
2118 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2121 static const struct got_error *
2122 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2124 return get_ref_name(refname, worktree,
2125 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2129 * Prevent Git's garbage collector from deleting our base commit by
2130 * setting a reference to our base commit's ID.
2132 static const struct got_error *
2133 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2135 const struct got_error *err = NULL;
2136 struct got_reference *ref = NULL;
2137 char *refname;
2139 err = got_worktree_get_base_ref_name(&refname, worktree);
2140 if (err)
2141 return err;
2143 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2144 if (err)
2145 goto done;
2147 err = got_ref_write(ref, repo);
2148 done:
2149 free(refname);
2150 if (ref)
2151 got_ref_close(ref);
2152 return err;
2155 static const struct got_error *
2156 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2158 const struct got_error *err = NULL;
2160 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2161 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2162 err = got_error_from_errno("asprintf");
2163 *fileindex_path = NULL;
2165 return err;
2169 static const struct got_error *
2170 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2171 struct got_worktree *worktree)
2173 const struct got_error *err = NULL;
2174 FILE *index = NULL;
2176 *fileindex_path = NULL;
2177 *fileindex = got_fileindex_alloc();
2178 if (*fileindex == NULL)
2179 return got_error_from_errno("got_fileindex_alloc");
2181 err = get_fileindex_path(fileindex_path, worktree);
2182 if (err)
2183 goto done;
2185 index = fopen(*fileindex_path, "rb");
2186 if (index == NULL) {
2187 if (errno != ENOENT)
2188 err = got_error_from_errno2("fopen", *fileindex_path);
2189 } else {
2190 err = got_fileindex_read(*fileindex, index);
2191 if (fclose(index) == EOF && err == NULL)
2192 err = got_error_from_errno("fclose");
2194 done:
2195 if (err) {
2196 free(*fileindex_path);
2197 *fileindex_path = NULL;
2198 got_fileindex_free(*fileindex);
2199 *fileindex = NULL;
2201 return err;
2204 struct bump_base_commit_id_arg {
2205 struct got_object_id *base_commit_id;
2206 const char *path;
2207 size_t path_len;
2208 const char *entry_name;
2209 got_worktree_checkout_cb progress_cb;
2210 void *progress_arg;
2213 /* Bump base commit ID of all files within an updated part of the work tree. */
2214 static const struct got_error *
2215 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2217 const struct got_error *err;
2218 struct bump_base_commit_id_arg *a = arg;
2220 if (a->entry_name) {
2221 if (strcmp(ie->path, a->path) != 0)
2222 return NULL;
2223 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2224 return NULL;
2226 if (got_fileindex_entry_was_skipped(ie))
2227 return NULL;
2229 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2230 SHA1_DIGEST_LENGTH) == 0)
2231 return NULL;
2233 if (a->progress_cb) {
2234 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2235 ie->path);
2236 if (err)
2237 return err;
2239 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2240 return NULL;
2243 static const struct got_error *
2244 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2245 struct got_fileindex *fileindex,
2246 got_worktree_checkout_cb progress_cb, void *progress_arg)
2248 struct bump_base_commit_id_arg bbc_arg;
2250 bbc_arg.base_commit_id = worktree->base_commit_id;
2251 bbc_arg.entry_name = NULL;
2252 bbc_arg.path = "";
2253 bbc_arg.path_len = 0;
2254 bbc_arg.progress_cb = progress_cb;
2255 bbc_arg.progress_arg = progress_arg;
2257 return got_fileindex_for_each_entry_safe(fileindex,
2258 bump_base_commit_id, &bbc_arg);
2261 static const struct got_error *
2262 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2264 const struct got_error *err = NULL;
2265 char *new_fileindex_path = NULL;
2266 FILE *new_index = NULL;
2267 struct timespec timeout;
2269 err = got_opentemp_named(&new_fileindex_path, &new_index,
2270 fileindex_path);
2271 if (err)
2272 goto done;
2274 err = got_fileindex_write(fileindex, new_index);
2275 if (err)
2276 goto done;
2278 if (rename(new_fileindex_path, fileindex_path) != 0) {
2279 err = got_error_from_errno3("rename", new_fileindex_path,
2280 fileindex_path);
2281 unlink(new_fileindex_path);
2285 * Sleep for a short amount of time to ensure that files modified after
2286 * this program exits have a different time stamp from the one which
2287 * was recorded in the file index.
2289 timeout.tv_sec = 0;
2290 timeout.tv_nsec = 1;
2291 nanosleep(&timeout, NULL);
2292 done:
2293 if (new_index)
2294 fclose(new_index);
2295 free(new_fileindex_path);
2296 return err;
2299 static const struct got_error *
2300 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2301 struct got_object_id **tree_id, const char *wt_relpath,
2302 struct got_worktree *worktree, struct got_repository *repo)
2304 const struct got_error *err = NULL;
2305 struct got_object_id *id = NULL;
2306 char *in_repo_path = NULL;
2307 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2309 *entry_type = GOT_OBJ_TYPE_ANY;
2310 *tree_relpath = NULL;
2311 *tree_id = NULL;
2313 if (wt_relpath[0] == '\0') {
2314 /* Check out all files within the work tree. */
2315 *entry_type = GOT_OBJ_TYPE_TREE;
2316 *tree_relpath = strdup("");
2317 if (*tree_relpath == NULL) {
2318 err = got_error_from_errno("strdup");
2319 goto done;
2321 err = got_object_id_by_path(tree_id, repo,
2322 worktree->base_commit_id, worktree->path_prefix);
2323 if (err)
2324 goto done;
2325 return NULL;
2328 /* Check out a subset of files in the work tree. */
2330 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2331 is_root_wt ? "" : "/", wt_relpath) == -1) {
2332 err = got_error_from_errno("asprintf");
2333 goto done;
2336 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2337 in_repo_path);
2338 if (err)
2339 goto done;
2341 free(in_repo_path);
2342 in_repo_path = NULL;
2344 err = got_object_get_type(entry_type, repo, id);
2345 if (err)
2346 goto done;
2348 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2349 /* Check out a single file. */
2350 if (strchr(wt_relpath, '/') == NULL) {
2351 /* Check out a single file in work tree's root dir. */
2352 in_repo_path = strdup(worktree->path_prefix);
2353 if (in_repo_path == NULL) {
2354 err = got_error_from_errno("strdup");
2355 goto done;
2357 *tree_relpath = strdup("");
2358 if (*tree_relpath == NULL) {
2359 err = got_error_from_errno("strdup");
2360 goto done;
2362 } else {
2363 /* Check out a single file in a subdirectory. */
2364 err = got_path_dirname(tree_relpath, wt_relpath);
2365 if (err)
2366 return err;
2367 if (asprintf(&in_repo_path, "%s%s%s",
2368 worktree->path_prefix, is_root_wt ? "" : "/",
2369 *tree_relpath) == -1) {
2370 err = got_error_from_errno("asprintf");
2371 goto done;
2374 err = got_object_id_by_path(tree_id, repo,
2375 worktree->base_commit_id, in_repo_path);
2376 } else {
2377 /* Check out all files within a subdirectory. */
2378 *tree_id = got_object_id_dup(id);
2379 if (*tree_id == NULL) {
2380 err = got_error_from_errno("got_object_id_dup");
2381 goto done;
2383 *tree_relpath = strdup(wt_relpath);
2384 if (*tree_relpath == NULL) {
2385 err = got_error_from_errno("strdup");
2386 goto done;
2389 done:
2390 free(id);
2391 free(in_repo_path);
2392 if (err) {
2393 *entry_type = GOT_OBJ_TYPE_ANY;
2394 free(*tree_relpath);
2395 *tree_relpath = NULL;
2396 free(*tree_id);
2397 *tree_id = NULL;
2399 return err;
2402 static const struct got_error *
2403 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2404 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2405 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2406 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2408 const struct got_error *err = NULL;
2409 struct got_commit_object *commit = NULL;
2410 struct got_tree_object *tree = NULL;
2411 struct got_fileindex_diff_tree_cb diff_cb;
2412 struct diff_cb_arg arg;
2414 err = ref_base_commit(worktree, repo);
2415 if (err) {
2416 if (!(err->code == GOT_ERR_ERRNO &&
2417 (errno == EACCES || errno == EROFS)))
2418 goto done;
2419 err = (*progress_cb)(progress_arg,
2420 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2421 if (err)
2422 return err;
2425 err = got_object_open_as_commit(&commit, repo,
2426 worktree->base_commit_id);
2427 if (err)
2428 goto done;
2430 err = got_object_open_as_tree(&tree, repo, tree_id);
2431 if (err)
2432 goto done;
2434 if (entry_name &&
2435 got_object_tree_find_entry(tree, entry_name) == NULL) {
2436 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2437 goto done;
2440 diff_cb.diff_old_new = diff_old_new;
2441 diff_cb.diff_old = diff_old;
2442 diff_cb.diff_new = diff_new;
2443 arg.fileindex = fileindex;
2444 arg.worktree = worktree;
2445 arg.repo = repo;
2446 arg.progress_cb = progress_cb;
2447 arg.progress_arg = progress_arg;
2448 arg.cancel_cb = cancel_cb;
2449 arg.cancel_arg = cancel_arg;
2450 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2451 entry_name, repo, &diff_cb, &arg);
2452 done:
2453 if (tree)
2454 got_object_tree_close(tree);
2455 if (commit)
2456 got_object_commit_close(commit);
2457 return err;
2460 const struct got_error *
2461 got_worktree_checkout_files(struct got_worktree *worktree,
2462 struct got_pathlist_head *paths, struct got_repository *repo,
2463 got_worktree_checkout_cb progress_cb, void *progress_arg,
2464 got_cancel_cb cancel_cb, void *cancel_arg)
2466 const struct got_error *err = NULL, *sync_err, *unlockerr;
2467 struct got_commit_object *commit = NULL;
2468 struct got_tree_object *tree = NULL;
2469 struct got_fileindex *fileindex = NULL;
2470 char *fileindex_path = NULL;
2471 struct got_pathlist_entry *pe;
2472 struct tree_path_data {
2473 STAILQ_ENTRY(tree_path_data) entry;
2474 struct got_object_id *tree_id;
2475 int entry_type;
2476 char *relpath;
2477 char *entry_name;
2478 } *tpd = NULL;
2479 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2481 STAILQ_INIT(&tree_paths);
2483 err = lock_worktree(worktree, LOCK_EX);
2484 if (err)
2485 return err;
2487 /* Map all specified paths to in-repository trees. */
2488 TAILQ_FOREACH(pe, paths, entry) {
2489 tpd = malloc(sizeof(*tpd));
2490 if (tpd == NULL) {
2491 err = got_error_from_errno("malloc");
2492 goto done;
2495 err = find_tree_entry_for_checkout(&tpd->entry_type,
2496 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2497 if (err) {
2498 free(tpd);
2499 goto done;
2502 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2503 err = got_path_basename(&tpd->entry_name, pe->path);
2504 if (err) {
2505 free(tpd->relpath);
2506 free(tpd->tree_id);
2507 free(tpd);
2508 goto done;
2510 } else
2511 tpd->entry_name = NULL;
2513 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2517 * Read the file index.
2518 * Checking out files is supposed to be an idempotent operation.
2519 * If the on-disk file index is incomplete we will try to complete it.
2521 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2522 if (err)
2523 goto done;
2525 tpd = STAILQ_FIRST(&tree_paths);
2526 TAILQ_FOREACH(pe, paths, entry) {
2527 struct bump_base_commit_id_arg bbc_arg;
2529 err = checkout_files(worktree, fileindex, tpd->relpath,
2530 tpd->tree_id, tpd->entry_name, repo,
2531 progress_cb, progress_arg, cancel_cb, cancel_arg);
2532 if (err)
2533 break;
2535 bbc_arg.base_commit_id = worktree->base_commit_id;
2536 bbc_arg.entry_name = tpd->entry_name;
2537 bbc_arg.path = pe->path;
2538 bbc_arg.path_len = pe->path_len;
2539 bbc_arg.progress_cb = progress_cb;
2540 bbc_arg.progress_arg = progress_arg;
2541 err = got_fileindex_for_each_entry_safe(fileindex,
2542 bump_base_commit_id, &bbc_arg);
2543 if (err)
2544 break;
2546 tpd = STAILQ_NEXT(tpd, entry);
2548 sync_err = sync_fileindex(fileindex, fileindex_path);
2549 if (sync_err && err == NULL)
2550 err = sync_err;
2551 done:
2552 free(fileindex_path);
2553 if (tree)
2554 got_object_tree_close(tree);
2555 if (commit)
2556 got_object_commit_close(commit);
2557 if (fileindex)
2558 got_fileindex_free(fileindex);
2559 while (!STAILQ_EMPTY(&tree_paths)) {
2560 tpd = STAILQ_FIRST(&tree_paths);
2561 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2562 free(tpd->relpath);
2563 free(tpd->tree_id);
2564 free(tpd);
2566 unlockerr = lock_worktree(worktree, LOCK_SH);
2567 if (unlockerr && err == NULL)
2568 err = unlockerr;
2569 return err;
2572 struct merge_file_cb_arg {
2573 struct got_worktree *worktree;
2574 struct got_fileindex *fileindex;
2575 got_worktree_checkout_cb progress_cb;
2576 void *progress_arg;
2577 got_cancel_cb cancel_cb;
2578 void *cancel_arg;
2579 const char *label_orig;
2580 struct got_object_id *commit_id2;
2581 int allow_bad_symlinks;
2584 static const struct got_error *
2585 merge_file_cb(void *arg, struct got_blob_object *blob1,
2586 struct got_blob_object *blob2, struct got_object_id *id1,
2587 struct got_object_id *id2, const char *path1, const char *path2,
2588 mode_t mode1, mode_t mode2, struct got_repository *repo)
2590 static const struct got_error *err = NULL;
2591 struct merge_file_cb_arg *a = arg;
2592 struct got_fileindex_entry *ie;
2593 char *ondisk_path = NULL;
2594 struct stat sb;
2595 unsigned char status;
2596 int local_changes_subsumed;
2597 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2598 char *id_str = NULL, *label_deriv2 = NULL;
2600 if (blob1 && blob2) {
2601 ie = got_fileindex_entry_get(a->fileindex, path2,
2602 strlen(path2));
2603 if (ie == NULL)
2604 return (*a->progress_cb)(a->progress_arg,
2605 GOT_STATUS_MISSING, path2);
2607 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2608 path2) == -1)
2609 return got_error_from_errno("asprintf");
2611 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2612 repo);
2613 if (err)
2614 goto done;
2616 if (status == GOT_STATUS_DELETE) {
2617 err = (*a->progress_cb)(a->progress_arg,
2618 GOT_STATUS_MERGE, path2);
2619 goto done;
2621 if (status != GOT_STATUS_NO_CHANGE &&
2622 status != GOT_STATUS_MODIFY &&
2623 status != GOT_STATUS_CONFLICT &&
2624 status != GOT_STATUS_ADD) {
2625 err = (*a->progress_cb)(a->progress_arg, status, path2);
2626 goto done;
2629 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2630 char *link_target2;
2631 err = got_object_blob_read_to_str(&link_target2, blob2);
2632 if (err)
2633 goto done;
2634 err = merge_symlink(a->worktree, blob1, ondisk_path,
2635 path2, a->label_orig, link_target2, a->commit_id2,
2636 repo, a->progress_cb, a->progress_arg);
2637 free(link_target2);
2638 } else {
2639 int fd;
2641 f_orig = got_opentemp();
2642 if (f_orig == NULL) {
2643 err = got_error_from_errno("got_opentemp");
2644 goto done;
2646 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2647 f_orig, blob1);
2648 if (err)
2649 goto done;
2651 f_deriv2 = got_opentemp();
2652 if (f_deriv2 == NULL)
2653 goto done;
2654 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2655 f_deriv2, blob2);
2656 if (err)
2657 goto done;
2659 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2660 if (fd == -1) {
2661 err = got_error_from_errno2("open",
2662 ondisk_path);
2663 goto done;
2665 f_deriv = fdopen(fd, "r");
2666 if (f_deriv == NULL) {
2667 err = got_error_from_errno2("fdopen",
2668 ondisk_path);
2669 close(fd);
2670 goto done;
2672 err = got_object_id_str(&id_str, a->commit_id2);
2673 if (err)
2674 goto done;
2675 if (asprintf(&label_deriv2, "%s: commit %s",
2676 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2677 err = got_error_from_errno("asprintf");
2678 goto done;
2680 err = merge_file(&local_changes_subsumed, a->worktree,
2681 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2682 sb.st_mode, a->label_orig, NULL, label_deriv2,
2683 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2684 a->progress_cb, a->progress_arg);
2686 } else if (blob1) {
2687 ie = got_fileindex_entry_get(a->fileindex, path1,
2688 strlen(path1));
2689 if (ie == NULL)
2690 return (*a->progress_cb)(a->progress_arg,
2691 GOT_STATUS_MISSING, path1);
2693 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2694 path1) == -1)
2695 return got_error_from_errno("asprintf");
2697 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2698 repo);
2699 if (err)
2700 goto done;
2702 switch (status) {
2703 case GOT_STATUS_NO_CHANGE:
2704 err = (*a->progress_cb)(a->progress_arg,
2705 GOT_STATUS_DELETE, path1);
2706 if (err)
2707 goto done;
2708 err = remove_ondisk_file(a->worktree->root_path, path1);
2709 if (err)
2710 goto done;
2711 if (ie)
2712 got_fileindex_entry_mark_deleted_from_disk(ie);
2713 break;
2714 case GOT_STATUS_DELETE:
2715 case GOT_STATUS_MISSING:
2716 err = (*a->progress_cb)(a->progress_arg,
2717 GOT_STATUS_DELETE, path1);
2718 if (err)
2719 goto done;
2720 if (ie)
2721 got_fileindex_entry_mark_deleted_from_disk(ie);
2722 break;
2723 case GOT_STATUS_ADD: {
2724 struct got_object_id *id;
2725 FILE *blob1_f;
2727 * Delete the added file only if its content already
2728 * exists in the repository.
2730 err = got_object_blob_file_create(&id, &blob1_f, path1);
2731 if (err)
2732 goto done;
2733 if (got_object_id_cmp(id, id1) == 0) {
2734 err = (*a->progress_cb)(a->progress_arg,
2735 GOT_STATUS_DELETE, path1);
2736 if (err)
2737 goto done;
2738 err = remove_ondisk_file(a->worktree->root_path,
2739 path1);
2740 if (err)
2741 goto done;
2742 if (ie)
2743 got_fileindex_entry_remove(a->fileindex,
2744 ie);
2745 } else {
2746 err = (*a->progress_cb)(a->progress_arg,
2747 GOT_STATUS_CANNOT_DELETE, path1);
2749 if (fclose(blob1_f) == EOF && err == NULL)
2750 err = got_error_from_errno("fclose");
2751 free(id);
2752 if (err)
2753 goto done;
2754 break;
2756 case GOT_STATUS_MODIFY:
2757 case GOT_STATUS_CONFLICT:
2758 err = (*a->progress_cb)(a->progress_arg,
2759 GOT_STATUS_CANNOT_DELETE, path1);
2760 if (err)
2761 goto done;
2762 break;
2763 case GOT_STATUS_OBSTRUCTED:
2764 err = (*a->progress_cb)(a->progress_arg, status, path1);
2765 if (err)
2766 goto done;
2767 break;
2768 default:
2769 break;
2771 } else if (blob2) {
2772 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2773 path2) == -1)
2774 return got_error_from_errno("asprintf");
2775 ie = got_fileindex_entry_get(a->fileindex, path2,
2776 strlen(path2));
2777 if (ie) {
2778 err = get_file_status(&status, &sb, ie, ondisk_path,
2779 -1, NULL, repo);
2780 if (err)
2781 goto done;
2782 if (status != GOT_STATUS_NO_CHANGE &&
2783 status != GOT_STATUS_MODIFY &&
2784 status != GOT_STATUS_CONFLICT &&
2785 status != GOT_STATUS_ADD) {
2786 err = (*a->progress_cb)(a->progress_arg,
2787 status, path2);
2788 goto done;
2790 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2791 char *link_target2;
2792 err = got_object_blob_read_to_str(&link_target2,
2793 blob2);
2794 if (err)
2795 goto done;
2796 err = merge_symlink(a->worktree, NULL,
2797 ondisk_path, path2, a->label_orig,
2798 link_target2, a->commit_id2, repo,
2799 a->progress_cb, a->progress_arg);
2800 free(link_target2);
2801 } else if (S_ISREG(sb.st_mode)) {
2802 err = merge_blob(&local_changes_subsumed,
2803 a->worktree, NULL, ondisk_path, path2,
2804 sb.st_mode, a->label_orig, blob2,
2805 a->commit_id2, repo, a->progress_cb,
2806 a->progress_arg);
2807 } else {
2808 err = got_error_path(ondisk_path,
2809 GOT_ERR_FILE_OBSTRUCTED);
2811 if (err)
2812 goto done;
2813 if (status == GOT_STATUS_DELETE) {
2814 err = got_fileindex_entry_update(ie,
2815 a->worktree->root_fd, path2, blob2->id.sha1,
2816 a->worktree->base_commit_id->sha1, 0);
2817 if (err)
2818 goto done;
2820 } else {
2821 int is_bad_symlink = 0;
2822 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2823 if (S_ISLNK(mode2)) {
2824 err = install_symlink(&is_bad_symlink,
2825 a->worktree, ondisk_path, path2, blob2, 0,
2826 0, 1, a->allow_bad_symlinks, repo,
2827 a->progress_cb, a->progress_arg);
2828 } else {
2829 err = install_blob(a->worktree, ondisk_path, path2,
2830 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2831 a->progress_cb, a->progress_arg);
2833 if (err)
2834 goto done;
2835 err = got_fileindex_entry_alloc(&ie, path2);
2836 if (err)
2837 goto done;
2838 err = got_fileindex_entry_update(ie,
2839 a->worktree->root_fd, path2, NULL, NULL, 1);
2840 if (err) {
2841 got_fileindex_entry_free(ie);
2842 goto done;
2844 err = got_fileindex_entry_add(a->fileindex, ie);
2845 if (err) {
2846 got_fileindex_entry_free(ie);
2847 goto done;
2849 if (is_bad_symlink) {
2850 got_fileindex_entry_filetype_set(ie,
2851 GOT_FILEIDX_MODE_BAD_SYMLINK);
2855 done:
2856 if (f_orig && fclose(f_orig) == EOF && err == NULL)
2857 err = got_error_from_errno("fclose");
2858 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
2859 err = got_error_from_errno("fclose");
2860 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
2861 err = got_error_from_errno("fclose");
2862 free(id_str);
2863 free(label_deriv2);
2864 free(ondisk_path);
2865 return err;
2868 static const struct got_error *
2869 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
2871 struct got_worktree *worktree = arg;
2873 /* Reject merges into a work tree with mixed base commits. */
2874 if (got_fileindex_entry_has_commit(ie) &&
2875 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
2876 SHA1_DIGEST_LENGTH) != 0)
2877 return got_error(GOT_ERR_MIXED_COMMITS);
2879 return NULL;
2882 struct check_merge_conflicts_arg {
2883 struct got_worktree *worktree;
2884 struct got_fileindex *fileindex;
2885 struct got_repository *repo;
2888 static const struct got_error *
2889 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
2890 struct got_blob_object *blob2, struct got_object_id *id1,
2891 struct got_object_id *id2, const char *path1, const char *path2,
2892 mode_t mode1, mode_t mode2, struct got_repository *repo)
2894 const struct got_error *err = NULL;
2895 struct check_merge_conflicts_arg *a = arg;
2896 unsigned char status;
2897 struct stat sb;
2898 struct got_fileindex_entry *ie;
2899 const char *path = path2 ? path2 : path1;
2900 struct got_object_id *id = id2 ? id2 : id1;
2901 char *ondisk_path;
2903 if (id == NULL)
2904 return NULL;
2906 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
2907 if (ie == NULL)
2908 return NULL;
2910 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2911 == -1)
2912 return got_error_from_errno("asprintf");
2914 /* Reject merges into a work tree with conflicted files. */
2915 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2916 free(ondisk_path);
2917 if (err)
2918 return err;
2919 if (status == GOT_STATUS_CONFLICT)
2920 return got_error(GOT_ERR_CONFLICTS);
2922 return NULL;
2925 static const struct got_error *
2926 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2927 const char *fileindex_path, struct got_object_id *commit_id1,
2928 struct got_object_id *commit_id2, struct got_repository *repo,
2929 got_worktree_checkout_cb progress_cb, void *progress_arg,
2930 got_cancel_cb cancel_cb, void *cancel_arg)
2932 const struct got_error *err = NULL, *sync_err;
2933 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2934 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2935 struct check_merge_conflicts_arg cmc_arg;
2936 struct merge_file_cb_arg arg;
2937 char *label_orig = NULL;
2939 if (commit_id1) {
2940 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2941 worktree->path_prefix);
2942 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2943 goto done;
2945 if (tree_id1) {
2946 char *id_str;
2948 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2949 if (err)
2950 goto done;
2952 err = got_object_id_str(&id_str, commit_id1);
2953 if (err)
2954 goto done;
2956 if (asprintf(&label_orig, "%s: commit %s",
2957 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2958 err = got_error_from_errno("asprintf");
2959 free(id_str);
2960 goto done;
2962 free(id_str);
2965 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2966 worktree->path_prefix);
2967 if (err)
2968 goto done;
2970 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2971 if (err)
2972 goto done;
2974 cmc_arg.worktree = worktree;
2975 cmc_arg.fileindex = fileindex;
2976 cmc_arg.repo = repo;
2977 err = got_diff_tree(tree1, tree2, "", "", repo,
2978 check_merge_conflicts, &cmc_arg, 0);
2979 if (err)
2980 goto done;
2982 arg.worktree = worktree;
2983 arg.fileindex = fileindex;
2984 arg.progress_cb = progress_cb;
2985 arg.progress_arg = progress_arg;
2986 arg.cancel_cb = cancel_cb;
2987 arg.cancel_arg = cancel_arg;
2988 arg.label_orig = label_orig;
2989 arg.commit_id2 = commit_id2;
2990 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
2991 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2992 sync_err = sync_fileindex(fileindex, fileindex_path);
2993 if (sync_err && err == NULL)
2994 err = sync_err;
2995 done:
2996 if (tree1)
2997 got_object_tree_close(tree1);
2998 if (tree2)
2999 got_object_tree_close(tree2);
3000 free(label_orig);
3001 return err;
3004 const struct got_error *
3005 got_worktree_merge_files(struct got_worktree *worktree,
3006 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3007 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3008 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3010 const struct got_error *err, *unlockerr;
3011 char *fileindex_path = NULL;
3012 struct got_fileindex *fileindex = NULL;
3014 err = lock_worktree(worktree, LOCK_EX);
3015 if (err)
3016 return err;
3018 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3019 if (err)
3020 goto done;
3022 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3023 worktree);
3024 if (err)
3025 goto done;
3027 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3028 commit_id2, repo, progress_cb, progress_arg,
3029 cancel_cb, cancel_arg);
3030 done:
3031 if (fileindex)
3032 got_fileindex_free(fileindex);
3033 free(fileindex_path);
3034 unlockerr = lock_worktree(worktree, LOCK_SH);
3035 if (unlockerr && err == NULL)
3036 err = unlockerr;
3037 return err;
3040 struct diff_dir_cb_arg {
3041 struct got_fileindex *fileindex;
3042 struct got_worktree *worktree;
3043 const char *status_path;
3044 size_t status_path_len;
3045 struct got_repository *repo;
3046 got_worktree_status_cb status_cb;
3047 void *status_arg;
3048 got_cancel_cb cancel_cb;
3049 void *cancel_arg;
3050 /* A pathlist containing per-directory pathlists of ignore patterns. */
3051 struct got_pathlist_head *ignores;
3052 int report_unchanged;
3053 int no_ignores;
3056 static const struct got_error *
3057 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3058 int dirfd, const char *de_name,
3059 got_worktree_status_cb status_cb, void *status_arg,
3060 struct got_repository *repo, int report_unchanged)
3062 const struct got_error *err = NULL;
3063 unsigned char status = GOT_STATUS_NO_CHANGE;
3064 unsigned char staged_status = get_staged_status(ie);
3065 struct stat sb;
3066 struct got_object_id blob_id, commit_id, staged_blob_id;
3067 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3068 struct got_object_id *staged_blob_idp = NULL;
3070 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3071 if (err)
3072 return err;
3074 if (status == GOT_STATUS_NO_CHANGE &&
3075 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3076 return NULL;
3078 if (got_fileindex_entry_has_blob(ie)) {
3079 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3080 blob_idp = &blob_id;
3082 if (got_fileindex_entry_has_commit(ie)) {
3083 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3084 commit_idp = &commit_id;
3086 if (staged_status == GOT_STATUS_ADD ||
3087 staged_status == GOT_STATUS_MODIFY) {
3088 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3089 SHA1_DIGEST_LENGTH);
3090 staged_blob_idp = &staged_blob_id;
3093 return (*status_cb)(status_arg, status, staged_status,
3094 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3097 static const struct got_error *
3098 status_old_new(void *arg, struct got_fileindex_entry *ie,
3099 struct dirent *de, const char *parent_path, int dirfd)
3101 const struct got_error *err = NULL;
3102 struct diff_dir_cb_arg *a = arg;
3103 char *abspath;
3105 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3106 return got_error(GOT_ERR_CANCELLED);
3108 if (got_path_cmp(parent_path, a->status_path,
3109 strlen(parent_path), a->status_path_len) != 0 &&
3110 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3111 return NULL;
3113 if (parent_path[0]) {
3114 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3115 parent_path, de->d_name) == -1)
3116 return got_error_from_errno("asprintf");
3117 } else {
3118 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3119 de->d_name) == -1)
3120 return got_error_from_errno("asprintf");
3123 err = report_file_status(ie, abspath, dirfd, de->d_name,
3124 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3125 free(abspath);
3126 return err;
3129 static const struct got_error *
3130 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3132 struct diff_dir_cb_arg *a = arg;
3133 struct got_object_id blob_id, commit_id;
3134 unsigned char status;
3136 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3137 return got_error(GOT_ERR_CANCELLED);
3139 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3140 return NULL;
3142 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3143 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3144 if (got_fileindex_entry_has_file_on_disk(ie))
3145 status = GOT_STATUS_MISSING;
3146 else
3147 status = GOT_STATUS_DELETE;
3148 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3149 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3152 void
3153 free_ignorelist(struct got_pathlist_head *ignorelist)
3155 struct got_pathlist_entry *pe;
3157 TAILQ_FOREACH(pe, ignorelist, entry)
3158 free((char *)pe->path);
3159 got_pathlist_free(ignorelist);
3162 void
3163 free_ignores(struct got_pathlist_head *ignores)
3165 struct got_pathlist_entry *pe;
3167 TAILQ_FOREACH(pe, ignores, entry) {
3168 struct got_pathlist_head *ignorelist = pe->data;
3169 free_ignorelist(ignorelist);
3170 free((char *)pe->path);
3172 got_pathlist_free(ignores);
3175 static const struct got_error *
3176 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3178 const struct got_error *err = NULL;
3179 struct got_pathlist_entry *pe = NULL;
3180 struct got_pathlist_head *ignorelist;
3181 char *line = NULL, *pattern, *dirpath = NULL;
3182 size_t linesize = 0;
3183 ssize_t linelen;
3185 ignorelist = calloc(1, sizeof(*ignorelist));
3186 if (ignorelist == NULL)
3187 return got_error_from_errno("calloc");
3188 TAILQ_INIT(ignorelist);
3190 while ((linelen = getline(&line, &linesize, f)) != -1) {
3191 if (linelen > 0 && line[linelen - 1] == '\n')
3192 line[linelen - 1] = '\0';
3194 /* Git's ignores may contain comments. */
3195 if (line[0] == '#')
3196 continue;
3198 /* Git's negated patterns are not (yet?) supported. */
3199 if (line[0] == '!')
3200 continue;
3202 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3203 line) == -1) {
3204 err = got_error_from_errno("asprintf");
3205 goto done;
3207 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3208 if (err)
3209 goto done;
3211 if (ferror(f)) {
3212 err = got_error_from_errno("getline");
3213 goto done;
3216 dirpath = strdup(path);
3217 if (dirpath == NULL) {
3218 err = got_error_from_errno("strdup");
3219 goto done;
3221 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3222 done:
3223 free(line);
3224 if (err || pe == NULL) {
3225 free(dirpath);
3226 free_ignorelist(ignorelist);
3228 return err;
3231 int
3232 match_ignores(struct got_pathlist_head *ignores, const char *path)
3234 struct got_pathlist_entry *pe;
3236 /* Handle patterns which match in all directories. */
3237 TAILQ_FOREACH(pe, ignores, entry) {
3238 struct got_pathlist_head *ignorelist = pe->data;
3239 struct got_pathlist_entry *pi;
3241 TAILQ_FOREACH(pi, ignorelist, entry) {
3242 const char *p, *pattern = pi->path;
3244 if (strncmp(pattern, "**/", 3) != 0)
3245 continue;
3246 pattern += 3;
3247 p = path;
3248 while (*p) {
3249 if (fnmatch(pattern, p,
3250 FNM_PATHNAME | FNM_LEADING_DIR)) {
3251 /* Retry in next directory. */
3252 while (*p && *p != '/')
3253 p++;
3254 while (*p == '/')
3255 p++;
3256 continue;
3258 return 1;
3264 * The ignores pathlist contains ignore lists from children before
3265 * parents, so we can find the most specific ignorelist by walking
3266 * ignores backwards.
3268 pe = TAILQ_LAST(ignores, got_pathlist_head);
3269 while (pe) {
3270 if (got_path_is_child(path, pe->path, pe->path_len)) {
3271 struct got_pathlist_head *ignorelist = pe->data;
3272 struct got_pathlist_entry *pi;
3273 TAILQ_FOREACH(pi, ignorelist, entry) {
3274 const char *pattern = pi->path;
3275 int flags = FNM_LEADING_DIR;
3276 if (strstr(pattern, "/**/") == NULL)
3277 flags |= FNM_PATHNAME;
3278 if (fnmatch(pattern, path, flags))
3279 continue;
3280 return 1;
3283 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3286 return 0;
3289 static const struct got_error *
3290 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3291 const char *path, int dirfd, const char *ignores_filename)
3293 const struct got_error *err = NULL;
3294 char *ignorespath;
3295 int fd = -1;
3296 FILE *ignoresfile = NULL;
3298 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3299 path[0] ? "/" : "", ignores_filename) == -1)
3300 return got_error_from_errno("asprintf");
3302 if (dirfd != -1) {
3303 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3304 if (fd == -1) {
3305 if (errno != ENOENT && errno != EACCES)
3306 err = got_error_from_errno2("openat",
3307 ignorespath);
3308 } else {
3309 ignoresfile = fdopen(fd, "r");
3310 if (ignoresfile == NULL)
3311 err = got_error_from_errno2("fdopen",
3312 ignorespath);
3313 else {
3314 fd = -1;
3315 err = read_ignores(ignores, path, ignoresfile);
3318 } else {
3319 ignoresfile = fopen(ignorespath, "r");
3320 if (ignoresfile == NULL) {
3321 if (errno != ENOENT && errno != EACCES)
3322 err = got_error_from_errno2("fopen",
3323 ignorespath);
3324 } else
3325 err = read_ignores(ignores, path, ignoresfile);
3328 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3329 err = got_error_from_errno2("fclose", path);
3330 if (fd != -1 && close(fd) == -1 && err == NULL)
3331 err = got_error_from_errno2("close", path);
3332 free(ignorespath);
3333 return err;
3336 static const struct got_error *
3337 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3338 int dirfd)
3340 const struct got_error *err = NULL;
3341 struct diff_dir_cb_arg *a = arg;
3342 char *path = NULL;
3344 if (ignore != NULL)
3345 *ignore = 0;
3347 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3348 return got_error(GOT_ERR_CANCELLED);
3350 if (parent_path[0]) {
3351 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3352 return got_error_from_errno("asprintf");
3353 } else {
3354 path = de->d_name;
3357 if (de->d_type == DT_DIR) {
3358 if (!a->no_ignores && ignore != NULL &&
3359 match_ignores(a->ignores, path))
3360 *ignore = 1;
3361 } else if (!match_ignores(a->ignores, path) &&
3362 got_path_is_child(path, a->status_path, a->status_path_len))
3363 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3364 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3365 if (parent_path[0])
3366 free(path);
3367 return err;
3370 static const struct got_error *
3371 status_traverse(void *arg, const char *path, int dirfd)
3373 const struct got_error *err = NULL;
3374 struct diff_dir_cb_arg *a = arg;
3376 if (a->no_ignores)
3377 return NULL;
3379 err = add_ignores(a->ignores, a->worktree->root_path,
3380 path, dirfd, ".cvsignore");
3381 if (err)
3382 return err;
3384 err = add_ignores(a->ignores, a->worktree->root_path, path,
3385 dirfd, ".gitignore");
3387 return err;
3390 static const struct got_error *
3391 report_single_file_status(const char *path, const char *ondisk_path,
3392 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3393 void *status_arg, struct got_repository *repo, int report_unchanged,
3394 struct got_pathlist_head *ignores, int no_ignores)
3396 struct got_fileindex_entry *ie;
3397 struct stat sb;
3399 if (!no_ignores && match_ignores(ignores, path))
3400 return NULL;
3402 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3403 if (ie)
3404 return report_file_status(ie, ondisk_path, -1, NULL,
3405 status_cb, status_arg, repo, report_unchanged);
3407 if (lstat(ondisk_path, &sb) == -1) {
3408 if (errno != ENOENT)
3409 return got_error_from_errno2("lstat", ondisk_path);
3410 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3411 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3412 return NULL;
3415 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3416 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3417 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3419 return NULL;
3422 static const struct got_error *
3423 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3424 const char *root_path, const char *path)
3426 const struct got_error *err;
3427 char *parent_path, *next_parent_path = NULL;
3429 err = add_ignores(ignores, root_path, "", -1,
3430 ".cvsignore");
3431 if (err)
3432 return err;
3434 err = add_ignores(ignores, root_path, "", -1,
3435 ".gitignore");
3436 if (err)
3437 return err;
3439 err = got_path_dirname(&parent_path, path);
3440 if (err) {
3441 if (err->code == GOT_ERR_BAD_PATH)
3442 return NULL; /* cannot traverse parent */
3443 return err;
3445 for (;;) {
3446 err = add_ignores(ignores, root_path, parent_path, -1,
3447 ".cvsignore");
3448 if (err)
3449 break;
3450 err = add_ignores(ignores, root_path, parent_path, -1,
3451 ".gitignore");
3452 if (err)
3453 break;
3454 err = got_path_dirname(&next_parent_path, parent_path);
3455 if (err) {
3456 if (err->code == GOT_ERR_BAD_PATH)
3457 err = NULL; /* traversed everything */
3458 break;
3460 if (got_path_is_root_dir(parent_path))
3461 break;
3462 free(parent_path);
3463 parent_path = next_parent_path;
3464 next_parent_path = NULL;
3467 free(parent_path);
3468 free(next_parent_path);
3469 return err;
3472 static const struct got_error *
3473 worktree_status(struct got_worktree *worktree, const char *path,
3474 struct got_fileindex *fileindex, struct got_repository *repo,
3475 got_worktree_status_cb status_cb, void *status_arg,
3476 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3477 int report_unchanged)
3479 const struct got_error *err = NULL;
3480 int fd = -1;
3481 struct got_fileindex_diff_dir_cb fdiff_cb;
3482 struct diff_dir_cb_arg arg;
3483 char *ondisk_path = NULL;
3484 struct got_pathlist_head ignores;
3486 TAILQ_INIT(&ignores);
3488 if (asprintf(&ondisk_path, "%s%s%s",
3489 worktree->root_path, path[0] ? "/" : "", path) == -1)
3490 return got_error_from_errno("asprintf");
3492 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3493 if (fd == -1) {
3494 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3495 !got_err_open_nofollow_on_symlink())
3496 err = got_error_from_errno2("open", ondisk_path);
3497 else {
3498 if (!no_ignores) {
3499 err = add_ignores_from_parent_paths(&ignores,
3500 worktree->root_path, ondisk_path);
3501 if (err)
3502 goto done;
3504 err = report_single_file_status(path, ondisk_path,
3505 fileindex, status_cb, status_arg, repo,
3506 report_unchanged, &ignores, no_ignores);
3508 } else {
3509 fdiff_cb.diff_old_new = status_old_new;
3510 fdiff_cb.diff_old = status_old;
3511 fdiff_cb.diff_new = status_new;
3512 fdiff_cb.diff_traverse = status_traverse;
3513 arg.fileindex = fileindex;
3514 arg.worktree = worktree;
3515 arg.status_path = path;
3516 arg.status_path_len = strlen(path);
3517 arg.repo = repo;
3518 arg.status_cb = status_cb;
3519 arg.status_arg = status_arg;
3520 arg.cancel_cb = cancel_cb;
3521 arg.cancel_arg = cancel_arg;
3522 arg.report_unchanged = report_unchanged;
3523 arg.no_ignores = no_ignores;
3524 if (!no_ignores) {
3525 err = add_ignores_from_parent_paths(&ignores,
3526 worktree->root_path, path);
3527 if (err)
3528 goto done;
3530 arg.ignores = &ignores;
3531 err = got_fileindex_diff_dir(fileindex, fd,
3532 worktree->root_path, path, repo, &fdiff_cb, &arg);
3534 done:
3535 free_ignores(&ignores);
3536 if (fd != -1 && close(fd) == -1 && err == NULL)
3537 err = got_error_from_errno("close");
3538 free(ondisk_path);
3539 return err;
3542 const struct got_error *
3543 got_worktree_status(struct got_worktree *worktree,
3544 struct got_pathlist_head *paths, struct got_repository *repo,
3545 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3546 got_cancel_cb cancel_cb, void *cancel_arg)
3548 const struct got_error *err = NULL;
3549 char *fileindex_path = NULL;
3550 struct got_fileindex *fileindex = NULL;
3551 struct got_pathlist_entry *pe;
3553 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3554 if (err)
3555 return err;
3557 TAILQ_FOREACH(pe, paths, entry) {
3558 err = worktree_status(worktree, pe->path, fileindex, repo,
3559 status_cb, status_arg, cancel_cb, cancel_arg,
3560 no_ignores, 0);
3561 if (err)
3562 break;
3564 free(fileindex_path);
3565 got_fileindex_free(fileindex);
3566 return err;
3569 const struct got_error *
3570 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3571 const char *arg)
3573 const struct got_error *err = NULL;
3574 char *resolved = NULL, *cwd = NULL, *path = NULL;
3575 size_t len;
3576 struct stat sb;
3577 char *abspath = NULL;
3578 char canonpath[PATH_MAX];
3580 *wt_path = NULL;
3582 cwd = getcwd(NULL, 0);
3583 if (cwd == NULL)
3584 return got_error_from_errno("getcwd");
3586 if (lstat(arg, &sb) == -1) {
3587 if (errno != ENOENT) {
3588 err = got_error_from_errno2("lstat", arg);
3589 goto done;
3591 sb.st_mode = 0;
3593 if (S_ISLNK(sb.st_mode)) {
3595 * We cannot use realpath(3) with symlinks since we want to
3596 * operate on the symlink itself.
3597 * But we can make the path absolute, assuming it is relative
3598 * to the current working directory, and then canonicalize it.
3600 if (!got_path_is_absolute(arg)) {
3601 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3602 err = got_error_from_errno("asprintf");
3603 goto done;
3607 err = got_canonpath(abspath ? abspath : arg, canonpath,
3608 sizeof(canonpath));
3609 if (err)
3610 goto done;
3611 resolved = strdup(canonpath);
3612 if (resolved == NULL) {
3613 err = got_error_from_errno("strdup");
3614 goto done;
3616 } else {
3617 resolved = realpath(arg, NULL);
3618 if (resolved == NULL) {
3619 if (errno != ENOENT) {
3620 err = got_error_from_errno2("realpath", arg);
3621 goto done;
3623 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3624 err = got_error_from_errno("asprintf");
3625 goto done;
3627 err = got_canonpath(abspath, canonpath,
3628 sizeof(canonpath));
3629 if (err)
3630 goto done;
3631 resolved = strdup(canonpath);
3632 if (resolved == NULL) {
3633 err = got_error_from_errno("strdup");
3634 goto done;
3639 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3640 strlen(got_worktree_get_root_path(worktree)))) {
3641 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3642 goto done;
3645 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3646 err = got_path_skip_common_ancestor(&path,
3647 got_worktree_get_root_path(worktree), resolved);
3648 if (err)
3649 goto done;
3650 } else {
3651 path = strdup("");
3652 if (path == NULL) {
3653 err = got_error_from_errno("strdup");
3654 goto done;
3658 /* XXX status walk can't deal with trailing slash! */
3659 len = strlen(path);
3660 while (len > 0 && path[len - 1] == '/') {
3661 path[len - 1] = '\0';
3662 len--;
3664 done:
3665 free(abspath);
3666 free(resolved);
3667 free(cwd);
3668 if (err == NULL)
3669 *wt_path = path;
3670 else
3671 free(path);
3672 return err;
3675 struct schedule_addition_args {
3676 struct got_worktree *worktree;
3677 struct got_fileindex *fileindex;
3678 got_worktree_checkout_cb progress_cb;
3679 void *progress_arg;
3680 struct got_repository *repo;
3683 static const struct got_error *
3684 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3685 const char *relpath, struct got_object_id *blob_id,
3686 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3687 int dirfd, const char *de_name)
3689 struct schedule_addition_args *a = arg;
3690 const struct got_error *err = NULL;
3691 struct got_fileindex_entry *ie;
3692 struct stat sb;
3693 char *ondisk_path;
3695 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3696 relpath) == -1)
3697 return got_error_from_errno("asprintf");
3699 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3700 if (ie) {
3701 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3702 de_name, a->repo);
3703 if (err)
3704 goto done;
3705 /* Re-adding an existing entry is a no-op. */
3706 if (status == GOT_STATUS_ADD)
3707 goto done;
3708 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3709 if (err)
3710 goto done;
3713 if (status != GOT_STATUS_UNVERSIONED) {
3714 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3715 goto done;
3718 err = got_fileindex_entry_alloc(&ie, relpath);
3719 if (err)
3720 goto done;
3721 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3722 relpath, NULL, NULL, 1);
3723 if (err) {
3724 got_fileindex_entry_free(ie);
3725 goto done;
3727 err = got_fileindex_entry_add(a->fileindex, ie);
3728 if (err) {
3729 got_fileindex_entry_free(ie);
3730 goto done;
3732 done:
3733 free(ondisk_path);
3734 if (err)
3735 return err;
3736 if (status == GOT_STATUS_ADD)
3737 return NULL;
3738 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3741 const struct got_error *
3742 got_worktree_schedule_add(struct got_worktree *worktree,
3743 struct got_pathlist_head *paths,
3744 got_worktree_checkout_cb progress_cb, void *progress_arg,
3745 struct got_repository *repo, int no_ignores)
3747 struct got_fileindex *fileindex = NULL;
3748 char *fileindex_path = NULL;
3749 const struct got_error *err = NULL, *sync_err, *unlockerr;
3750 struct got_pathlist_entry *pe;
3751 struct schedule_addition_args saa;
3753 err = lock_worktree(worktree, LOCK_EX);
3754 if (err)
3755 return err;
3757 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3758 if (err)
3759 goto done;
3761 saa.worktree = worktree;
3762 saa.fileindex = fileindex;
3763 saa.progress_cb = progress_cb;
3764 saa.progress_arg = progress_arg;
3765 saa.repo = repo;
3767 TAILQ_FOREACH(pe, paths, entry) {
3768 err = worktree_status(worktree, pe->path, fileindex, repo,
3769 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3770 if (err)
3771 break;
3773 sync_err = sync_fileindex(fileindex, fileindex_path);
3774 if (sync_err && err == NULL)
3775 err = sync_err;
3776 done:
3777 free(fileindex_path);
3778 if (fileindex)
3779 got_fileindex_free(fileindex);
3780 unlockerr = lock_worktree(worktree, LOCK_SH);
3781 if (unlockerr && err == NULL)
3782 err = unlockerr;
3783 return err;
3786 struct schedule_deletion_args {
3787 struct got_worktree *worktree;
3788 struct got_fileindex *fileindex;
3789 got_worktree_delete_cb progress_cb;
3790 void *progress_arg;
3791 struct got_repository *repo;
3792 int delete_local_mods;
3793 int keep_on_disk;
3794 const char *status_codes;
3797 static const struct got_error *
3798 schedule_for_deletion(void *arg, unsigned char status,
3799 unsigned char staged_status, const char *relpath,
3800 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3801 struct got_object_id *commit_id, int dirfd, const char *de_name)
3803 struct schedule_deletion_args *a = arg;
3804 const struct got_error *err = NULL;
3805 struct got_fileindex_entry *ie = NULL;
3806 struct stat sb;
3807 char *ondisk_path;
3809 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3810 if (ie == NULL)
3811 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3813 staged_status = get_staged_status(ie);
3814 if (staged_status != GOT_STATUS_NO_CHANGE) {
3815 if (staged_status == GOT_STATUS_DELETE)
3816 return NULL;
3817 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3820 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3821 relpath) == -1)
3822 return got_error_from_errno("asprintf");
3824 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3825 a->repo);
3826 if (err)
3827 goto done;
3829 if (a->status_codes) {
3830 size_t ncodes = strlen(a->status_codes);
3831 int i;
3832 for (i = 0; i < ncodes ; i++) {
3833 if (status == a->status_codes[i])
3834 break;
3836 if (i == ncodes) {
3837 /* Do not delete files in non-matching status. */
3838 free(ondisk_path);
3839 return NULL;
3841 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3842 a->status_codes[i] != GOT_STATUS_MISSING) {
3843 static char msg[64];
3844 snprintf(msg, sizeof(msg),
3845 "invalid status code '%c'", a->status_codes[i]);
3846 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3847 goto done;
3851 if (status != GOT_STATUS_NO_CHANGE) {
3852 if (status == GOT_STATUS_DELETE)
3853 goto done;
3854 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3855 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3856 goto done;
3858 if (status != GOT_STATUS_MODIFY &&
3859 status != GOT_STATUS_MISSING) {
3860 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3861 goto done;
3865 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3866 size_t root_len;
3868 if (dirfd != -1) {
3869 if (unlinkat(dirfd, de_name, 0) != 0) {
3870 err = got_error_from_errno2("unlinkat",
3871 ondisk_path);
3872 goto done;
3874 } else if (unlink(ondisk_path) != 0) {
3875 err = got_error_from_errno2("unlink", ondisk_path);
3876 goto done;
3879 root_len = strlen(a->worktree->root_path);
3880 do {
3881 char *parent;
3882 err = got_path_dirname(&parent, ondisk_path);
3883 if (err)
3884 goto done;
3885 free(ondisk_path);
3886 ondisk_path = parent;
3887 if (rmdir(ondisk_path) == -1) {
3888 if (errno != ENOTEMPTY)
3889 err = got_error_from_errno2("rmdir",
3890 ondisk_path);
3891 break;
3893 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3894 strlen(ondisk_path), root_len) != 0);
3897 got_fileindex_entry_mark_deleted_from_disk(ie);
3898 done:
3899 free(ondisk_path);
3900 if (err)
3901 return err;
3902 if (status == GOT_STATUS_DELETE)
3903 return NULL;
3904 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3905 staged_status, relpath);
3908 const struct got_error *
3909 got_worktree_schedule_delete(struct got_worktree *worktree,
3910 struct got_pathlist_head *paths, int delete_local_mods,
3911 const char *status_codes,
3912 got_worktree_delete_cb progress_cb, void *progress_arg,
3913 struct got_repository *repo, int keep_on_disk)
3915 struct got_fileindex *fileindex = NULL;
3916 char *fileindex_path = NULL;
3917 const struct got_error *err = NULL, *sync_err, *unlockerr;
3918 struct got_pathlist_entry *pe;
3919 struct schedule_deletion_args sda;
3921 err = lock_worktree(worktree, LOCK_EX);
3922 if (err)
3923 return err;
3925 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3926 if (err)
3927 goto done;
3929 sda.worktree = worktree;
3930 sda.fileindex = fileindex;
3931 sda.progress_cb = progress_cb;
3932 sda.progress_arg = progress_arg;
3933 sda.repo = repo;
3934 sda.delete_local_mods = delete_local_mods;
3935 sda.keep_on_disk = keep_on_disk;
3936 sda.status_codes = status_codes;
3938 TAILQ_FOREACH(pe, paths, entry) {
3939 err = worktree_status(worktree, pe->path, fileindex, repo,
3940 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
3941 if (err)
3942 break;
3944 sync_err = sync_fileindex(fileindex, fileindex_path);
3945 if (sync_err && err == NULL)
3946 err = sync_err;
3947 done:
3948 free(fileindex_path);
3949 if (fileindex)
3950 got_fileindex_free(fileindex);
3951 unlockerr = lock_worktree(worktree, LOCK_SH);
3952 if (unlockerr && err == NULL)
3953 err = unlockerr;
3954 return err;
3957 static const struct got_error *
3958 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3960 const struct got_error *err = NULL;
3961 char *line = NULL;
3962 size_t linesize = 0, n;
3963 ssize_t linelen;
3965 linelen = getline(&line, &linesize, infile);
3966 if (linelen == -1) {
3967 if (ferror(infile)) {
3968 err = got_error_from_errno("getline");
3969 goto done;
3971 return NULL;
3973 if (outfile) {
3974 n = fwrite(line, 1, linelen, outfile);
3975 if (n != linelen) {
3976 err = got_ferror(outfile, GOT_ERR_IO);
3977 goto done;
3980 if (rejectfile) {
3981 n = fwrite(line, 1, linelen, rejectfile);
3982 if (n != linelen)
3983 err = got_ferror(outfile, GOT_ERR_IO);
3985 done:
3986 free(line);
3987 return err;
3990 static const struct got_error *
3991 skip_one_line(FILE *f)
3993 char *line = NULL;
3994 size_t linesize = 0;
3995 ssize_t linelen;
3997 linelen = getline(&line, &linesize, f);
3998 if (linelen == -1) {
3999 if (ferror(f))
4000 return got_error_from_errno("getline");
4001 return NULL;
4003 free(line);
4004 return NULL;
4007 static const struct got_error *
4008 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4009 int start_old, int end_old, int start_new, int end_new,
4010 FILE *outfile, FILE *rejectfile)
4012 const struct got_error *err;
4014 /* Copy old file's lines leading up to patch. */
4015 while (!feof(f1) && *line_cur1 < start_old) {
4016 err = copy_one_line(f1, outfile, NULL);
4017 if (err)
4018 return err;
4019 (*line_cur1)++;
4021 /* Skip new file's lines leading up to patch. */
4022 while (!feof(f2) && *line_cur2 < start_new) {
4023 if (rejectfile)
4024 err = copy_one_line(f2, NULL, rejectfile);
4025 else
4026 err = skip_one_line(f2);
4027 if (err)
4028 return err;
4029 (*line_cur2)++;
4031 /* Copy patched lines. */
4032 while (!feof(f2) && *line_cur2 <= end_new) {
4033 err = copy_one_line(f2, outfile, NULL);
4034 if (err)
4035 return err;
4036 (*line_cur2)++;
4038 /* Skip over old file's replaced lines. */
4039 while (!feof(f1) && *line_cur1 <= end_old) {
4040 if (rejectfile)
4041 err = copy_one_line(f1, NULL, rejectfile);
4042 else
4043 err = skip_one_line(f1);
4044 if (err)
4045 return err;
4046 (*line_cur1)++;
4049 return NULL;
4052 static const struct got_error *
4053 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4054 FILE *outfile, FILE *rejectfile)
4056 const struct got_error *err;
4058 if (outfile) {
4059 /* Copy old file's lines until EOF. */
4060 while (!feof(f1)) {
4061 err = copy_one_line(f1, outfile, NULL);
4062 if (err)
4063 return err;
4064 (*line_cur1)++;
4067 if (rejectfile) {
4068 /* Copy new file's lines until EOF. */
4069 while (!feof(f2)) {
4070 err = copy_one_line(f2, NULL, rejectfile);
4071 if (err)
4072 return err;
4073 (*line_cur2)++;
4077 return NULL;
4080 static const struct got_error *
4081 apply_or_reject_change(int *choice, int *nchunks_used,
4082 struct diff_result *diff_result, int n,
4083 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4084 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4085 got_worktree_patch_cb patch_cb, void *patch_arg)
4087 const struct got_error *err = NULL;
4088 struct diff_chunk_context cc = {};
4089 int start_old, end_old, start_new, end_new;
4090 FILE *hunkfile;
4091 struct diff_output_unidiff_state *diff_state;
4092 struct diff_input_info diff_info;
4093 int rc;
4095 *choice = GOT_PATCH_CHOICE_NONE;
4097 /* Get changed line numbers without context lines for copy_change(). */
4098 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4099 start_old = cc.left.start;
4100 end_old = cc.left.end;
4101 start_new = cc.right.start;
4102 end_new = cc.right.end;
4104 /* Get the same change with context lines for display. */
4105 memset(&cc, 0, sizeof(cc));
4106 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4108 memset(&diff_info, 0, sizeof(diff_info));
4109 diff_info.left_path = relpath;
4110 diff_info.right_path = relpath;
4112 diff_state = diff_output_unidiff_state_alloc();
4113 if (diff_state == NULL)
4114 return got_error_set_errno(ENOMEM,
4115 "diff_output_unidiff_state_alloc");
4117 hunkfile = got_opentemp();
4118 if (hunkfile == NULL) {
4119 err = got_error_from_errno("got_opentemp");
4120 goto done;
4123 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4124 diff_result, &cc);
4125 if (rc != DIFF_RC_OK) {
4126 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4127 goto done;
4130 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4131 err = got_ferror(hunkfile, GOT_ERR_IO);
4132 goto done;
4135 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4136 hunkfile, changeno, nchanges);
4137 if (err)
4138 goto done;
4140 switch (*choice) {
4141 case GOT_PATCH_CHOICE_YES:
4142 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4143 end_old, start_new, end_new, outfile, rejectfile);
4144 break;
4145 case GOT_PATCH_CHOICE_NO:
4146 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4147 end_old, start_new, end_new, rejectfile, outfile);
4148 break;
4149 case GOT_PATCH_CHOICE_QUIT:
4150 break;
4151 default:
4152 err = got_error(GOT_ERR_PATCH_CHOICE);
4153 break;
4155 done:
4156 diff_output_unidiff_state_free(diff_state);
4157 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4158 err = got_error_from_errno("fclose");
4159 return err;
4162 struct revert_file_args {
4163 struct got_worktree *worktree;
4164 struct got_fileindex *fileindex;
4165 got_worktree_checkout_cb progress_cb;
4166 void *progress_arg;
4167 got_worktree_patch_cb patch_cb;
4168 void *patch_arg;
4169 struct got_repository *repo;
4170 int unlink_added_files;
4173 static const struct got_error *
4174 create_patched_content(char **path_outfile, int reverse_patch,
4175 struct got_object_id *blob_id, const char *path2,
4176 int dirfd2, const char *de_name2,
4177 const char *relpath, struct got_repository *repo,
4178 got_worktree_patch_cb patch_cb, void *patch_arg)
4180 const struct got_error *err, *free_err;
4181 struct got_blob_object *blob = NULL;
4182 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4183 int fd2 = -1;
4184 char link_target[PATH_MAX];
4185 ssize_t link_len = 0;
4186 char *path1 = NULL, *id_str = NULL;
4187 struct stat sb2;
4188 struct got_diffreg_result *diffreg_result = NULL;
4189 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4190 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4192 *path_outfile = NULL;
4194 err = got_object_id_str(&id_str, blob_id);
4195 if (err)
4196 return err;
4198 if (dirfd2 != -1) {
4199 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4200 if (fd2 == -1) {
4201 if (!got_err_open_nofollow_on_symlink()) {
4202 err = got_error_from_errno2("openat", path2);
4203 goto done;
4205 link_len = readlinkat(dirfd2, de_name2,
4206 link_target, sizeof(link_target));
4207 if (link_len == -1)
4208 return got_error_from_errno2("readlinkat", path2);
4209 sb2.st_mode = S_IFLNK;
4210 sb2.st_size = link_len;
4212 } else {
4213 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4214 if (fd2 == -1) {
4215 if (!got_err_open_nofollow_on_symlink()) {
4216 err = got_error_from_errno2("open", path2);
4217 goto done;
4219 link_len = readlink(path2, link_target,
4220 sizeof(link_target));
4221 if (link_len == -1)
4222 return got_error_from_errno2("readlink", path2);
4223 sb2.st_mode = S_IFLNK;
4224 sb2.st_size = link_len;
4227 if (fd2 != -1) {
4228 if (fstat(fd2, &sb2) == -1) {
4229 err = got_error_from_errno2("fstat", path2);
4230 goto done;
4233 f2 = fdopen(fd2, "r");
4234 if (f2 == NULL) {
4235 err = got_error_from_errno2("fdopen", path2);
4236 goto done;
4238 fd2 = -1;
4239 } else {
4240 size_t n;
4241 f2 = got_opentemp();
4242 if (f2 == NULL) {
4243 err = got_error_from_errno2("got_opentemp", path2);
4244 goto done;
4246 n = fwrite(link_target, 1, link_len, f2);
4247 if (n != link_len) {
4248 err = got_ferror(f2, GOT_ERR_IO);
4249 goto done;
4251 if (fflush(f2) == EOF) {
4252 err = got_error_from_errno("fflush");
4253 goto done;
4255 rewind(f2);
4258 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4259 if (err)
4260 goto done;
4262 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4263 if (err)
4264 goto done;
4266 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4267 if (err)
4268 goto done;
4270 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4271 NULL);
4272 if (err)
4273 goto done;
4275 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4276 if (err)
4277 goto done;
4279 if (fseek(f1, 0L, SEEK_SET) == -1)
4280 return got_ferror(f1, GOT_ERR_IO);
4281 if (fseek(f2, 0L, SEEK_SET) == -1)
4282 return got_ferror(f2, GOT_ERR_IO);
4284 /* Count the number of actual changes in the diff result. */
4285 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4286 struct diff_chunk_context cc = {};
4287 diff_chunk_context_load_change(&cc, &nchunks_used,
4288 diffreg_result->result, n, 0);
4289 nchanges++;
4291 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4292 int choice;
4293 err = apply_or_reject_change(&choice, &nchunks_used,
4294 diffreg_result->result, n, relpath, f1, f2,
4295 &line_cur1, &line_cur2,
4296 reverse_patch ? NULL : outfile,
4297 reverse_patch ? outfile : NULL,
4298 ++i, nchanges, patch_cb, patch_arg);
4299 if (err)
4300 goto done;
4301 if (choice == GOT_PATCH_CHOICE_YES)
4302 have_content = 1;
4303 else if (choice == GOT_PATCH_CHOICE_QUIT)
4304 break;
4306 if (have_content) {
4307 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4308 reverse_patch ? NULL : outfile,
4309 reverse_patch ? outfile : NULL);
4310 if (err)
4311 goto done;
4313 if (!S_ISLNK(sb2.st_mode)) {
4314 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4315 err = got_error_from_errno2("fchmod", path2);
4316 goto done;
4320 done:
4321 free(id_str);
4322 if (blob)
4323 got_object_blob_close(blob);
4324 free_err = got_diffreg_result_free(diffreg_result);
4325 if (err == NULL)
4326 err = free_err;
4327 if (f1 && fclose(f1) == EOF && err == NULL)
4328 err = got_error_from_errno2("fclose", path1);
4329 if (f2 && fclose(f2) == EOF && err == NULL)
4330 err = got_error_from_errno2("fclose", path2);
4331 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4332 err = got_error_from_errno2("close", path2);
4333 if (outfile && fclose(outfile) == EOF && err == NULL)
4334 err = got_error_from_errno2("fclose", *path_outfile);
4335 if (path1 && unlink(path1) == -1 && err == NULL)
4336 err = got_error_from_errno2("unlink", path1);
4337 if (err || !have_content) {
4338 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4339 err = got_error_from_errno2("unlink", *path_outfile);
4340 free(*path_outfile);
4341 *path_outfile = NULL;
4343 free(path1);
4344 return err;
4347 static const struct got_error *
4348 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4349 const char *relpath, struct got_object_id *blob_id,
4350 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4351 int dirfd, const char *de_name)
4353 struct revert_file_args *a = arg;
4354 const struct got_error *err = NULL;
4355 char *parent_path = NULL;
4356 struct got_fileindex_entry *ie;
4357 struct got_tree_object *tree = NULL;
4358 struct got_object_id *tree_id = NULL;
4359 const struct got_tree_entry *te = NULL;
4360 char *tree_path = NULL, *te_name;
4361 char *ondisk_path = NULL, *path_content = NULL;
4362 struct got_blob_object *blob = NULL;
4364 /* Reverting a staged deletion is a no-op. */
4365 if (status == GOT_STATUS_DELETE &&
4366 staged_status != GOT_STATUS_NO_CHANGE)
4367 return NULL;
4369 if (status == GOT_STATUS_UNVERSIONED)
4370 return (*a->progress_cb)(a->progress_arg,
4371 GOT_STATUS_UNVERSIONED, relpath);
4373 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4374 if (ie == NULL)
4375 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4377 /* Construct in-repository path of tree which contains this blob. */
4378 err = got_path_dirname(&parent_path, ie->path);
4379 if (err) {
4380 if (err->code != GOT_ERR_BAD_PATH)
4381 goto done;
4382 parent_path = strdup("/");
4383 if (parent_path == NULL) {
4384 err = got_error_from_errno("strdup");
4385 goto done;
4388 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4389 tree_path = strdup(parent_path);
4390 if (tree_path == NULL) {
4391 err = got_error_from_errno("strdup");
4392 goto done;
4394 } else {
4395 if (got_path_is_root_dir(parent_path)) {
4396 tree_path = strdup(a->worktree->path_prefix);
4397 if (tree_path == NULL) {
4398 err = got_error_from_errno("strdup");
4399 goto done;
4401 } else {
4402 if (asprintf(&tree_path, "%s/%s",
4403 a->worktree->path_prefix, parent_path) == -1) {
4404 err = got_error_from_errno("asprintf");
4405 goto done;
4410 err = got_object_id_by_path(&tree_id, a->repo,
4411 a->worktree->base_commit_id, tree_path);
4412 if (err) {
4413 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4414 (status == GOT_STATUS_ADD ||
4415 staged_status == GOT_STATUS_ADD)))
4416 goto done;
4417 } else {
4418 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4419 if (err)
4420 goto done;
4422 err = got_path_basename(&te_name, ie->path);
4423 if (err)
4424 goto done;
4426 te = got_object_tree_find_entry(tree, te_name);
4427 free(te_name);
4428 if (te == NULL && status != GOT_STATUS_ADD &&
4429 staged_status != GOT_STATUS_ADD) {
4430 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4431 goto done;
4435 switch (status) {
4436 case GOT_STATUS_ADD:
4437 if (a->patch_cb) {
4438 int choice = GOT_PATCH_CHOICE_NONE;
4439 err = (*a->patch_cb)(&choice, a->patch_arg,
4440 status, ie->path, NULL, 1, 1);
4441 if (err)
4442 goto done;
4443 if (choice != GOT_PATCH_CHOICE_YES)
4444 break;
4446 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4447 ie->path);
4448 if (err)
4449 goto done;
4450 got_fileindex_entry_remove(a->fileindex, ie);
4451 if (a->unlink_added_files) {
4452 if (asprintf(&ondisk_path, "%s/%s",
4453 got_worktree_get_root_path(a->worktree),
4454 relpath) == -1) {
4455 err = got_error_from_errno("asprintf");
4456 goto done;
4458 if (unlink(ondisk_path) == -1) {
4459 err = got_error_from_errno2("unlink",
4460 ondisk_path);
4461 break;
4464 break;
4465 case GOT_STATUS_DELETE:
4466 if (a->patch_cb) {
4467 int choice = GOT_PATCH_CHOICE_NONE;
4468 err = (*a->patch_cb)(&choice, a->patch_arg,
4469 status, ie->path, NULL, 1, 1);
4470 if (err)
4471 goto done;
4472 if (choice != GOT_PATCH_CHOICE_YES)
4473 break;
4475 /* fall through */
4476 case GOT_STATUS_MODIFY:
4477 case GOT_STATUS_MODE_CHANGE:
4478 case GOT_STATUS_CONFLICT:
4479 case GOT_STATUS_MISSING: {
4480 struct got_object_id id;
4481 if (staged_status == GOT_STATUS_ADD ||
4482 staged_status == GOT_STATUS_MODIFY) {
4483 memcpy(id.sha1, ie->staged_blob_sha1,
4484 SHA1_DIGEST_LENGTH);
4485 } else
4486 memcpy(id.sha1, ie->blob_sha1,
4487 SHA1_DIGEST_LENGTH);
4488 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4489 if (err)
4490 goto done;
4492 if (asprintf(&ondisk_path, "%s/%s",
4493 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4494 err = got_error_from_errno("asprintf");
4495 goto done;
4498 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4499 status == GOT_STATUS_CONFLICT)) {
4500 int is_bad_symlink = 0;
4501 err = create_patched_content(&path_content, 1, &id,
4502 ondisk_path, dirfd, de_name, ie->path, a->repo,
4503 a->patch_cb, a->patch_arg);
4504 if (err || path_content == NULL)
4505 break;
4506 if (te && S_ISLNK(te->mode)) {
4507 if (unlink(path_content) == -1) {
4508 err = got_error_from_errno2("unlink",
4509 path_content);
4510 break;
4512 err = install_symlink(&is_bad_symlink,
4513 a->worktree, ondisk_path, ie->path,
4514 blob, 0, 1, 0, 0, a->repo,
4515 a->progress_cb, a->progress_arg);
4516 } else {
4517 if (rename(path_content, ondisk_path) == -1) {
4518 err = got_error_from_errno3("rename",
4519 path_content, ondisk_path);
4520 goto done;
4523 } else {
4524 int is_bad_symlink = 0;
4525 if (te && S_ISLNK(te->mode)) {
4526 err = install_symlink(&is_bad_symlink,
4527 a->worktree, ondisk_path, ie->path,
4528 blob, 0, 1, 0, 0, a->repo,
4529 a->progress_cb, a->progress_arg);
4530 } else {
4531 err = install_blob(a->worktree, ondisk_path,
4532 ie->path,
4533 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4534 got_fileindex_perms_to_st(ie), blob,
4535 0, 1, 0, 0, a->repo,
4536 a->progress_cb, a->progress_arg);
4538 if (err)
4539 goto done;
4540 if (status == GOT_STATUS_DELETE ||
4541 status == GOT_STATUS_MODE_CHANGE) {
4542 err = got_fileindex_entry_update(ie,
4543 a->worktree->root_fd, relpath,
4544 blob->id.sha1,
4545 a->worktree->base_commit_id->sha1, 1);
4546 if (err)
4547 goto done;
4549 if (is_bad_symlink) {
4550 got_fileindex_entry_filetype_set(ie,
4551 GOT_FILEIDX_MODE_BAD_SYMLINK);
4554 break;
4556 default:
4557 break;
4559 done:
4560 free(ondisk_path);
4561 free(path_content);
4562 free(parent_path);
4563 free(tree_path);
4564 if (blob)
4565 got_object_blob_close(blob);
4566 if (tree)
4567 got_object_tree_close(tree);
4568 free(tree_id);
4569 return err;
4572 const struct got_error *
4573 got_worktree_revert(struct got_worktree *worktree,
4574 struct got_pathlist_head *paths,
4575 got_worktree_checkout_cb progress_cb, void *progress_arg,
4576 got_worktree_patch_cb patch_cb, void *patch_arg,
4577 struct got_repository *repo)
4579 struct got_fileindex *fileindex = NULL;
4580 char *fileindex_path = NULL;
4581 const struct got_error *err = NULL, *unlockerr = NULL;
4582 const struct got_error *sync_err = NULL;
4583 struct got_pathlist_entry *pe;
4584 struct revert_file_args rfa;
4586 err = lock_worktree(worktree, LOCK_EX);
4587 if (err)
4588 return err;
4590 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4591 if (err)
4592 goto done;
4594 rfa.worktree = worktree;
4595 rfa.fileindex = fileindex;
4596 rfa.progress_cb = progress_cb;
4597 rfa.progress_arg = progress_arg;
4598 rfa.patch_cb = patch_cb;
4599 rfa.patch_arg = patch_arg;
4600 rfa.repo = repo;
4601 rfa.unlink_added_files = 0;
4602 TAILQ_FOREACH(pe, paths, entry) {
4603 err = worktree_status(worktree, pe->path, fileindex, repo,
4604 revert_file, &rfa, NULL, NULL, 1, 0);
4605 if (err)
4606 break;
4608 sync_err = sync_fileindex(fileindex, fileindex_path);
4609 if (sync_err && err == NULL)
4610 err = sync_err;
4611 done:
4612 free(fileindex_path);
4613 if (fileindex)
4614 got_fileindex_free(fileindex);
4615 unlockerr = lock_worktree(worktree, LOCK_SH);
4616 if (unlockerr && err == NULL)
4617 err = unlockerr;
4618 return err;
4621 static void
4622 free_commitable(struct got_commitable *ct)
4624 free(ct->path);
4625 free(ct->in_repo_path);
4626 free(ct->ondisk_path);
4627 free(ct->blob_id);
4628 free(ct->base_blob_id);
4629 free(ct->staged_blob_id);
4630 free(ct->base_commit_id);
4631 free(ct);
4634 struct collect_commitables_arg {
4635 struct got_pathlist_head *commitable_paths;
4636 struct got_repository *repo;
4637 struct got_worktree *worktree;
4638 struct got_fileindex *fileindex;
4639 int have_staged_files;
4640 int allow_bad_symlinks;
4643 static const struct got_error *
4644 collect_commitables(void *arg, unsigned char status,
4645 unsigned char staged_status, const char *relpath,
4646 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4647 struct got_object_id *commit_id, int dirfd, const char *de_name)
4649 struct collect_commitables_arg *a = arg;
4650 const struct got_error *err = NULL;
4651 struct got_commitable *ct = NULL;
4652 struct got_pathlist_entry *new = NULL;
4653 char *parent_path = NULL, *path = NULL;
4654 struct stat sb;
4656 if (a->have_staged_files) {
4657 if (staged_status != GOT_STATUS_MODIFY &&
4658 staged_status != GOT_STATUS_ADD &&
4659 staged_status != GOT_STATUS_DELETE)
4660 return NULL;
4661 } else {
4662 if (status == GOT_STATUS_CONFLICT)
4663 return got_error(GOT_ERR_COMMIT_CONFLICT);
4665 if (status != GOT_STATUS_MODIFY &&
4666 status != GOT_STATUS_MODE_CHANGE &&
4667 status != GOT_STATUS_ADD &&
4668 status != GOT_STATUS_DELETE)
4669 return NULL;
4672 if (asprintf(&path, "/%s", relpath) == -1) {
4673 err = got_error_from_errno("asprintf");
4674 goto done;
4676 if (strcmp(path, "/") == 0) {
4677 parent_path = strdup("");
4678 if (parent_path == NULL)
4679 return got_error_from_errno("strdup");
4680 } else {
4681 err = got_path_dirname(&parent_path, path);
4682 if (err)
4683 return err;
4686 ct = calloc(1, sizeof(*ct));
4687 if (ct == NULL) {
4688 err = got_error_from_errno("calloc");
4689 goto done;
4692 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4693 relpath) == -1) {
4694 err = got_error_from_errno("asprintf");
4695 goto done;
4698 if (staged_status == GOT_STATUS_ADD ||
4699 staged_status == GOT_STATUS_MODIFY) {
4700 struct got_fileindex_entry *ie;
4701 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4702 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4703 case GOT_FILEIDX_MODE_REGULAR_FILE:
4704 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4705 ct->mode = S_IFREG;
4706 break;
4707 case GOT_FILEIDX_MODE_SYMLINK:
4708 ct->mode = S_IFLNK;
4709 break;
4710 default:
4711 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4712 goto done;
4714 ct->mode |= got_fileindex_entry_perms_get(ie);
4715 } else if (status != GOT_STATUS_DELETE &&
4716 staged_status != GOT_STATUS_DELETE) {
4717 if (dirfd != -1) {
4718 if (fstatat(dirfd, de_name, &sb,
4719 AT_SYMLINK_NOFOLLOW) == -1) {
4720 err = got_error_from_errno2("fstatat",
4721 ct->ondisk_path);
4722 goto done;
4724 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4725 err = got_error_from_errno2("lstat", ct->ondisk_path);
4726 goto done;
4728 ct->mode = sb.st_mode;
4731 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4732 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4733 relpath) == -1) {
4734 err = got_error_from_errno("asprintf");
4735 goto done;
4738 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4739 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4740 int is_bad_symlink;
4741 char target_path[PATH_MAX];
4742 ssize_t target_len;
4743 target_len = readlink(ct->ondisk_path, target_path,
4744 sizeof(target_path));
4745 if (target_len == -1) {
4746 err = got_error_from_errno2("readlink",
4747 ct->ondisk_path);
4748 goto done;
4750 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4751 target_len, ct->ondisk_path, a->worktree->root_path);
4752 if (err)
4753 goto done;
4754 if (is_bad_symlink) {
4755 err = got_error_path(ct->ondisk_path,
4756 GOT_ERR_BAD_SYMLINK);
4757 goto done;
4762 ct->status = status;
4763 ct->staged_status = staged_status;
4764 ct->blob_id = NULL; /* will be filled in when blob gets created */
4765 if (ct->status != GOT_STATUS_ADD &&
4766 ct->staged_status != GOT_STATUS_ADD) {
4767 ct->base_blob_id = got_object_id_dup(blob_id);
4768 if (ct->base_blob_id == NULL) {
4769 err = got_error_from_errno("got_object_id_dup");
4770 goto done;
4772 ct->base_commit_id = got_object_id_dup(commit_id);
4773 if (ct->base_commit_id == NULL) {
4774 err = got_error_from_errno("got_object_id_dup");
4775 goto done;
4778 if (ct->staged_status == GOT_STATUS_ADD ||
4779 ct->staged_status == GOT_STATUS_MODIFY) {
4780 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4781 if (ct->staged_blob_id == NULL) {
4782 err = got_error_from_errno("got_object_id_dup");
4783 goto done;
4786 ct->path = strdup(path);
4787 if (ct->path == NULL) {
4788 err = got_error_from_errno("strdup");
4789 goto done;
4791 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4792 done:
4793 if (ct && (err || new == NULL))
4794 free_commitable(ct);
4795 free(parent_path);
4796 free(path);
4797 return err;
4800 static const struct got_error *write_tree(struct got_object_id **, int *,
4801 struct got_tree_object *, const char *, struct got_pathlist_head *,
4802 got_worktree_status_cb status_cb, void *status_arg,
4803 struct got_repository *);
4805 static const struct got_error *
4806 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4807 struct got_tree_entry *te, const char *parent_path,
4808 struct got_pathlist_head *commitable_paths,
4809 got_worktree_status_cb status_cb, void *status_arg,
4810 struct got_repository *repo)
4812 const struct got_error *err = NULL;
4813 struct got_tree_object *subtree;
4814 char *subpath;
4816 if (asprintf(&subpath, "%s%s%s", parent_path,
4817 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4818 return got_error_from_errno("asprintf");
4820 err = got_object_open_as_tree(&subtree, repo, &te->id);
4821 if (err)
4822 return err;
4824 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4825 commitable_paths, status_cb, status_arg, repo);
4826 got_object_tree_close(subtree);
4827 free(subpath);
4828 return err;
4831 static const struct got_error *
4832 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4834 const struct got_error *err = NULL;
4835 char *ct_parent_path = NULL;
4837 *match = 0;
4839 if (strchr(ct->in_repo_path, '/') == NULL) {
4840 *match = got_path_is_root_dir(path);
4841 return NULL;
4844 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4845 if (err)
4846 return err;
4847 *match = (strcmp(path, ct_parent_path) == 0);
4848 free(ct_parent_path);
4849 return err;
4852 static mode_t
4853 get_ct_file_mode(struct got_commitable *ct)
4855 if (S_ISLNK(ct->mode))
4856 return S_IFLNK;
4858 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4861 static const struct got_error *
4862 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4863 struct got_tree_entry *te, struct got_commitable *ct)
4865 const struct got_error *err = NULL;
4867 *new_te = NULL;
4869 err = got_object_tree_entry_dup(new_te, te);
4870 if (err)
4871 goto done;
4873 (*new_te)->mode = get_ct_file_mode(ct);
4875 if (ct->staged_status == GOT_STATUS_MODIFY)
4876 memcpy(&(*new_te)->id, ct->staged_blob_id,
4877 sizeof((*new_te)->id));
4878 else
4879 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4880 done:
4881 if (err && *new_te) {
4882 free(*new_te);
4883 *new_te = NULL;
4885 return err;
4888 static const struct got_error *
4889 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4890 struct got_commitable *ct)
4892 const struct got_error *err = NULL;
4893 char *ct_name = NULL;
4895 *new_te = NULL;
4897 *new_te = calloc(1, sizeof(**new_te));
4898 if (*new_te == NULL)
4899 return got_error_from_errno("calloc");
4901 err = got_path_basename(&ct_name, ct->path);
4902 if (err)
4903 goto done;
4904 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4905 sizeof((*new_te)->name)) {
4906 err = got_error(GOT_ERR_NO_SPACE);
4907 goto done;
4910 (*new_te)->mode = get_ct_file_mode(ct);
4912 if (ct->staged_status == GOT_STATUS_ADD)
4913 memcpy(&(*new_te)->id, ct->staged_blob_id,
4914 sizeof((*new_te)->id));
4915 else
4916 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4917 done:
4918 free(ct_name);
4919 if (err && *new_te) {
4920 free(*new_te);
4921 *new_te = NULL;
4923 return err;
4926 static const struct got_error *
4927 insert_tree_entry(struct got_tree_entry *new_te,
4928 struct got_pathlist_head *paths)
4930 const struct got_error *err = NULL;
4931 struct got_pathlist_entry *new_pe;
4933 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4934 if (err)
4935 return err;
4936 if (new_pe == NULL)
4937 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4938 return NULL;
4941 static const struct got_error *
4942 report_ct_status(struct got_commitable *ct,
4943 got_worktree_status_cb status_cb, void *status_arg)
4945 const char *ct_path = ct->path;
4946 unsigned char status;
4948 if (status_cb == NULL) /* no commit progress output desired */
4949 return NULL;
4951 while (ct_path[0] == '/')
4952 ct_path++;
4954 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4955 status = ct->staged_status;
4956 else
4957 status = ct->status;
4959 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4960 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4963 static const struct got_error *
4964 match_modified_subtree(int *modified, struct got_tree_entry *te,
4965 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4967 const struct got_error *err = NULL;
4968 struct got_pathlist_entry *pe;
4969 char *te_path;
4971 *modified = 0;
4973 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4974 got_path_is_root_dir(base_tree_path) ? "" : "/",
4975 te->name) == -1)
4976 return got_error_from_errno("asprintf");
4978 TAILQ_FOREACH(pe, commitable_paths, entry) {
4979 struct got_commitable *ct = pe->data;
4980 *modified = got_path_is_child(ct->in_repo_path, te_path,
4981 strlen(te_path));
4982 if (*modified)
4983 break;
4986 free(te_path);
4987 return err;
4990 static const struct got_error *
4991 match_deleted_or_modified_ct(struct got_commitable **ctp,
4992 struct got_tree_entry *te, const char *base_tree_path,
4993 struct got_pathlist_head *commitable_paths)
4995 const struct got_error *err = NULL;
4996 struct got_pathlist_entry *pe;
4998 *ctp = NULL;
5000 TAILQ_FOREACH(pe, commitable_paths, entry) {
5001 struct got_commitable *ct = pe->data;
5002 char *ct_name = NULL;
5003 int path_matches;
5005 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5006 if (ct->status != GOT_STATUS_MODIFY &&
5007 ct->status != GOT_STATUS_MODE_CHANGE &&
5008 ct->status != GOT_STATUS_DELETE)
5009 continue;
5010 } else {
5011 if (ct->staged_status != GOT_STATUS_MODIFY &&
5012 ct->staged_status != GOT_STATUS_DELETE)
5013 continue;
5016 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5017 continue;
5019 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5020 if (err)
5021 return err;
5022 if (!path_matches)
5023 continue;
5025 err = got_path_basename(&ct_name, pe->path);
5026 if (err)
5027 return err;
5029 if (strcmp(te->name, ct_name) != 0) {
5030 free(ct_name);
5031 continue;
5033 free(ct_name);
5035 *ctp = ct;
5036 break;
5039 return err;
5042 static const struct got_error *
5043 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5044 const char *child_path, const char *path_base_tree,
5045 struct got_pathlist_head *commitable_paths,
5046 got_worktree_status_cb status_cb, void *status_arg,
5047 struct got_repository *repo)
5049 const struct got_error *err = NULL;
5050 struct got_tree_entry *new_te;
5051 char *subtree_path;
5052 struct got_object_id *id = NULL;
5053 int nentries;
5055 *new_tep = NULL;
5057 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5058 got_path_is_root_dir(path_base_tree) ? "" : "/",
5059 child_path) == -1)
5060 return got_error_from_errno("asprintf");
5062 new_te = calloc(1, sizeof(*new_te));
5063 if (new_te == NULL)
5064 return got_error_from_errno("calloc");
5065 new_te->mode = S_IFDIR;
5067 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5068 sizeof(new_te->name)) {
5069 err = got_error(GOT_ERR_NO_SPACE);
5070 goto done;
5072 err = write_tree(&id, &nentries, NULL, subtree_path,
5073 commitable_paths, status_cb, status_arg, repo);
5074 if (err) {
5075 free(new_te);
5076 goto done;
5078 memcpy(&new_te->id, id, sizeof(new_te->id));
5079 done:
5080 free(id);
5081 free(subtree_path);
5082 if (err == NULL)
5083 *new_tep = new_te;
5084 return err;
5087 static const struct got_error *
5088 write_tree(struct got_object_id **new_tree_id, int *nentries,
5089 struct got_tree_object *base_tree, const char *path_base_tree,
5090 struct got_pathlist_head *commitable_paths,
5091 got_worktree_status_cb status_cb, void *status_arg,
5092 struct got_repository *repo)
5094 const struct got_error *err = NULL;
5095 struct got_pathlist_head paths;
5096 struct got_tree_entry *te, *new_te = NULL;
5097 struct got_pathlist_entry *pe;
5099 TAILQ_INIT(&paths);
5100 *nentries = 0;
5102 /* Insert, and recurse into, newly added entries first. */
5103 TAILQ_FOREACH(pe, commitable_paths, entry) {
5104 struct got_commitable *ct = pe->data;
5105 char *child_path = NULL, *slash;
5107 if ((ct->status != GOT_STATUS_ADD &&
5108 ct->staged_status != GOT_STATUS_ADD) ||
5109 (ct->flags & GOT_COMMITABLE_ADDED))
5110 continue;
5112 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5113 strlen(path_base_tree)))
5114 continue;
5116 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5117 ct->in_repo_path);
5118 if (err)
5119 goto done;
5121 slash = strchr(child_path, '/');
5122 if (slash == NULL) {
5123 err = alloc_added_blob_tree_entry(&new_te, ct);
5124 if (err)
5125 goto done;
5126 err = report_ct_status(ct, status_cb, status_arg);
5127 if (err)
5128 goto done;
5129 ct->flags |= GOT_COMMITABLE_ADDED;
5130 err = insert_tree_entry(new_te, &paths);
5131 if (err)
5132 goto done;
5133 (*nentries)++;
5134 } else {
5135 *slash = '\0'; /* trim trailing path components */
5136 if (base_tree == NULL ||
5137 got_object_tree_find_entry(base_tree, child_path)
5138 == NULL) {
5139 err = make_subtree_for_added_blob(&new_te,
5140 child_path, path_base_tree,
5141 commitable_paths, status_cb, status_arg,
5142 repo);
5143 if (err)
5144 goto done;
5145 err = insert_tree_entry(new_te, &paths);
5146 if (err)
5147 goto done;
5148 (*nentries)++;
5153 if (base_tree) {
5154 int i, nbase_entries;
5155 /* Handle modified and deleted entries. */
5156 nbase_entries = got_object_tree_get_nentries(base_tree);
5157 for (i = 0; i < nbase_entries; i++) {
5158 struct got_commitable *ct = NULL;
5160 te = got_object_tree_get_entry(base_tree, i);
5161 if (got_object_tree_entry_is_submodule(te)) {
5162 /* Entry is a submodule; just copy it. */
5163 err = got_object_tree_entry_dup(&new_te, te);
5164 if (err)
5165 goto done;
5166 err = insert_tree_entry(new_te, &paths);
5167 if (err)
5168 goto done;
5169 (*nentries)++;
5170 continue;
5173 if (S_ISDIR(te->mode)) {
5174 int modified;
5175 err = got_object_tree_entry_dup(&new_te, te);
5176 if (err)
5177 goto done;
5178 err = match_modified_subtree(&modified, te,
5179 path_base_tree, commitable_paths);
5180 if (err)
5181 goto done;
5182 /* Avoid recursion into unmodified subtrees. */
5183 if (modified) {
5184 struct got_object_id *new_id;
5185 int nsubentries;
5186 err = write_subtree(&new_id,
5187 &nsubentries, te,
5188 path_base_tree, commitable_paths,
5189 status_cb, status_arg, repo);
5190 if (err)
5191 goto done;
5192 if (nsubentries == 0) {
5193 /* All entries were deleted. */
5194 free(new_id);
5195 continue;
5197 memcpy(&new_te->id, new_id,
5198 sizeof(new_te->id));
5199 free(new_id);
5201 err = insert_tree_entry(new_te, &paths);
5202 if (err)
5203 goto done;
5204 (*nentries)++;
5205 continue;
5208 err = match_deleted_or_modified_ct(&ct, te,
5209 path_base_tree, commitable_paths);
5210 if (err)
5211 goto done;
5212 if (ct) {
5213 /* NB: Deleted entries get dropped here. */
5214 if (ct->status == GOT_STATUS_MODIFY ||
5215 ct->status == GOT_STATUS_MODE_CHANGE ||
5216 ct->staged_status == GOT_STATUS_MODIFY) {
5217 err = alloc_modified_blob_tree_entry(
5218 &new_te, te, ct);
5219 if (err)
5220 goto done;
5221 err = insert_tree_entry(new_te, &paths);
5222 if (err)
5223 goto done;
5224 (*nentries)++;
5226 err = report_ct_status(ct, status_cb,
5227 status_arg);
5228 if (err)
5229 goto done;
5230 } else {
5231 /* Entry is unchanged; just copy it. */
5232 err = got_object_tree_entry_dup(&new_te, te);
5233 if (err)
5234 goto done;
5235 err = insert_tree_entry(new_te, &paths);
5236 if (err)
5237 goto done;
5238 (*nentries)++;
5243 /* Write new list of entries; deleted entries have been dropped. */
5244 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5245 done:
5246 got_pathlist_free(&paths);
5247 return err;
5250 static const struct got_error *
5251 update_fileindex_after_commit(struct got_worktree *worktree,
5252 struct got_pathlist_head *commitable_paths,
5253 struct got_object_id *new_base_commit_id,
5254 struct got_fileindex *fileindex, int have_staged_files)
5256 const struct got_error *err = NULL;
5257 struct got_pathlist_entry *pe;
5258 char *relpath = NULL;
5260 TAILQ_FOREACH(pe, commitable_paths, entry) {
5261 struct got_fileindex_entry *ie;
5262 struct got_commitable *ct = pe->data;
5264 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5266 err = got_path_skip_common_ancestor(&relpath,
5267 worktree->root_path, ct->ondisk_path);
5268 if (err)
5269 goto done;
5271 if (ie) {
5272 if (ct->status == GOT_STATUS_DELETE ||
5273 ct->staged_status == GOT_STATUS_DELETE) {
5274 got_fileindex_entry_remove(fileindex, ie);
5275 } else if (ct->staged_status == GOT_STATUS_ADD ||
5276 ct->staged_status == GOT_STATUS_MODIFY) {
5277 got_fileindex_entry_stage_set(ie,
5278 GOT_FILEIDX_STAGE_NONE);
5279 got_fileindex_entry_staged_filetype_set(ie, 0);
5281 err = got_fileindex_entry_update(ie,
5282 worktree->root_fd, relpath,
5283 ct->staged_blob_id->sha1,
5284 new_base_commit_id->sha1,
5285 !have_staged_files);
5286 } else
5287 err = got_fileindex_entry_update(ie,
5288 worktree->root_fd, relpath,
5289 ct->blob_id->sha1,
5290 new_base_commit_id->sha1,
5291 !have_staged_files);
5292 } else {
5293 err = got_fileindex_entry_alloc(&ie, pe->path);
5294 if (err)
5295 goto done;
5296 err = got_fileindex_entry_update(ie,
5297 worktree->root_fd, relpath, ct->blob_id->sha1,
5298 new_base_commit_id->sha1, 1);
5299 if (err) {
5300 got_fileindex_entry_free(ie);
5301 goto done;
5303 err = got_fileindex_entry_add(fileindex, ie);
5304 if (err) {
5305 got_fileindex_entry_free(ie);
5306 goto done;
5309 free(relpath);
5310 relpath = NULL;
5312 done:
5313 free(relpath);
5314 return err;
5318 static const struct got_error *
5319 check_out_of_date(const char *in_repo_path, unsigned char status,
5320 unsigned char staged_status, struct got_object_id *base_blob_id,
5321 struct got_object_id *base_commit_id,
5322 struct got_object_id *head_commit_id, struct got_repository *repo,
5323 int ood_errcode)
5325 const struct got_error *err = NULL;
5326 struct got_object_id *id = NULL;
5328 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5329 /* Trivial case: base commit == head commit */
5330 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5331 return NULL;
5333 * Ensure file content which local changes were based
5334 * on matches file content in the branch head.
5336 err = got_object_id_by_path(&id, repo, head_commit_id,
5337 in_repo_path);
5338 if (err) {
5339 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5340 err = got_error(ood_errcode);
5341 goto done;
5342 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5343 err = got_error(ood_errcode);
5344 } else {
5345 /* Require that added files don't exist in the branch head. */
5346 err = got_object_id_by_path(&id, repo, head_commit_id,
5347 in_repo_path);
5348 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5349 goto done;
5350 err = id ? got_error(ood_errcode) : NULL;
5352 done:
5353 free(id);
5354 return err;
5357 const struct got_error *
5358 commit_worktree(struct got_object_id **new_commit_id,
5359 struct got_pathlist_head *commitable_paths,
5360 struct got_object_id *head_commit_id,
5361 struct got_object_id *parent_id2,
5362 struct got_worktree *worktree,
5363 const char *author, const char *committer,
5364 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5365 got_worktree_status_cb status_cb, void *status_arg,
5366 struct got_repository *repo)
5368 const struct got_error *err = NULL, *unlockerr = NULL;
5369 struct got_pathlist_entry *pe;
5370 const char *head_ref_name = NULL;
5371 struct got_commit_object *head_commit = NULL;
5372 struct got_reference *head_ref2 = NULL;
5373 struct got_object_id *head_commit_id2 = NULL;
5374 struct got_tree_object *head_tree = NULL;
5375 struct got_object_id *new_tree_id = NULL;
5376 int nentries, nparents = 0;
5377 struct got_object_id_queue parent_ids;
5378 struct got_object_qid *pid = NULL;
5379 char *logmsg = NULL;
5381 *new_commit_id = NULL;
5383 STAILQ_INIT(&parent_ids);
5385 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5386 if (err)
5387 goto done;
5389 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5390 if (err)
5391 goto done;
5393 if (commit_msg_cb != NULL) {
5394 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5395 if (err)
5396 goto done;
5399 if (logmsg == NULL || strlen(logmsg) == 0) {
5400 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5401 goto done;
5404 /* Create blobs from added and modified files and record their IDs. */
5405 TAILQ_FOREACH(pe, commitable_paths, entry) {
5406 struct got_commitable *ct = pe->data;
5407 char *ondisk_path;
5409 /* Blobs for staged files already exist. */
5410 if (ct->staged_status == GOT_STATUS_ADD ||
5411 ct->staged_status == GOT_STATUS_MODIFY)
5412 continue;
5414 if (ct->status != GOT_STATUS_ADD &&
5415 ct->status != GOT_STATUS_MODIFY &&
5416 ct->status != GOT_STATUS_MODE_CHANGE)
5417 continue;
5419 if (asprintf(&ondisk_path, "%s/%s",
5420 worktree->root_path, pe->path) == -1) {
5421 err = got_error_from_errno("asprintf");
5422 goto done;
5424 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5425 free(ondisk_path);
5426 if (err)
5427 goto done;
5430 /* Recursively write new tree objects. */
5431 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5432 commitable_paths, status_cb, status_arg, repo);
5433 if (err)
5434 goto done;
5436 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5437 if (err)
5438 goto done;
5439 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5440 nparents++;
5441 if (parent_id2) {
5442 err = got_object_qid_alloc(&pid, parent_id2);
5443 if (err)
5444 goto done;
5445 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5446 nparents++;
5448 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5449 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5450 if (logmsg != NULL)
5451 free(logmsg);
5452 if (err)
5453 goto done;
5455 /* Check if a concurrent commit to our branch has occurred. */
5456 head_ref_name = got_worktree_get_head_ref_name(worktree);
5457 if (head_ref_name == NULL) {
5458 err = got_error_from_errno("got_worktree_get_head_ref_name");
5459 goto done;
5461 /* Lock the reference here to prevent concurrent modification. */
5462 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5463 if (err)
5464 goto done;
5465 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5466 if (err)
5467 goto done;
5468 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5469 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5470 goto done;
5472 /* Update branch head in repository. */
5473 err = got_ref_change_ref(head_ref2, *new_commit_id);
5474 if (err)
5475 goto done;
5476 err = got_ref_write(head_ref2, repo);
5477 if (err)
5478 goto done;
5480 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5481 if (err)
5482 goto done;
5484 err = ref_base_commit(worktree, repo);
5485 if (err)
5486 goto done;
5487 done:
5488 got_object_id_queue_free(&parent_ids);
5489 if (head_tree)
5490 got_object_tree_close(head_tree);
5491 if (head_commit)
5492 got_object_commit_close(head_commit);
5493 free(head_commit_id2);
5494 if (head_ref2) {
5495 unlockerr = got_ref_unlock(head_ref2);
5496 if (unlockerr && err == NULL)
5497 err = unlockerr;
5498 got_ref_close(head_ref2);
5500 return err;
5503 static const struct got_error *
5504 check_path_is_commitable(const char *path,
5505 struct got_pathlist_head *commitable_paths)
5507 struct got_pathlist_entry *cpe = NULL;
5508 size_t path_len = strlen(path);
5510 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5511 struct got_commitable *ct = cpe->data;
5512 const char *ct_path = ct->path;
5514 while (ct_path[0] == '/')
5515 ct_path++;
5517 if (strcmp(path, ct_path) == 0 ||
5518 got_path_is_child(ct_path, path, path_len))
5519 break;
5522 if (cpe == NULL)
5523 return got_error_path(path, GOT_ERR_BAD_PATH);
5525 return NULL;
5528 static const struct got_error *
5529 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5531 int *have_staged_files = arg;
5533 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5534 *have_staged_files = 1;
5535 return got_error(GOT_ERR_CANCELLED);
5538 return NULL;
5541 static const struct got_error *
5542 check_non_staged_files(struct got_fileindex *fileindex,
5543 struct got_pathlist_head *paths)
5545 struct got_pathlist_entry *pe;
5546 struct got_fileindex_entry *ie;
5548 TAILQ_FOREACH(pe, paths, entry) {
5549 if (pe->path[0] == '\0')
5550 continue;
5551 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5552 if (ie == NULL)
5553 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5554 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5555 return got_error_path(pe->path,
5556 GOT_ERR_FILE_NOT_STAGED);
5559 return NULL;
5562 const struct got_error *
5563 got_worktree_commit(struct got_object_id **new_commit_id,
5564 struct got_worktree *worktree, struct got_pathlist_head *paths,
5565 const char *author, const char *committer, int allow_bad_symlinks,
5566 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5567 got_worktree_status_cb status_cb, void *status_arg,
5568 struct got_repository *repo)
5570 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5571 struct got_fileindex *fileindex = NULL;
5572 char *fileindex_path = NULL;
5573 struct got_pathlist_head commitable_paths;
5574 struct collect_commitables_arg cc_arg;
5575 struct got_pathlist_entry *pe;
5576 struct got_reference *head_ref = NULL;
5577 struct got_object_id *head_commit_id = NULL;
5578 int have_staged_files = 0;
5580 *new_commit_id = NULL;
5582 TAILQ_INIT(&commitable_paths);
5584 err = lock_worktree(worktree, LOCK_EX);
5585 if (err)
5586 goto done;
5588 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5589 if (err)
5590 goto done;
5592 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5593 if (err)
5594 goto done;
5596 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5597 if (err)
5598 goto done;
5600 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5601 &have_staged_files);
5602 if (err && err->code != GOT_ERR_CANCELLED)
5603 goto done;
5604 if (have_staged_files) {
5605 err = check_non_staged_files(fileindex, paths);
5606 if (err)
5607 goto done;
5610 cc_arg.commitable_paths = &commitable_paths;
5611 cc_arg.worktree = worktree;
5612 cc_arg.fileindex = fileindex;
5613 cc_arg.repo = repo;
5614 cc_arg.have_staged_files = have_staged_files;
5615 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5616 TAILQ_FOREACH(pe, paths, entry) {
5617 err = worktree_status(worktree, pe->path, fileindex, repo,
5618 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5619 if (err)
5620 goto done;
5623 if (TAILQ_EMPTY(&commitable_paths)) {
5624 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5625 goto done;
5628 TAILQ_FOREACH(pe, paths, entry) {
5629 err = check_path_is_commitable(pe->path, &commitable_paths);
5630 if (err)
5631 goto done;
5634 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5635 struct got_commitable *ct = pe->data;
5636 const char *ct_path = ct->in_repo_path;
5638 while (ct_path[0] == '/')
5639 ct_path++;
5640 err = check_out_of_date(ct_path, ct->status,
5641 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5642 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5643 if (err)
5644 goto done;
5648 err = commit_worktree(new_commit_id, &commitable_paths,
5649 head_commit_id, NULL, worktree, author, committer,
5650 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5651 if (err)
5652 goto done;
5654 err = update_fileindex_after_commit(worktree, &commitable_paths,
5655 *new_commit_id, fileindex, have_staged_files);
5656 sync_err = sync_fileindex(fileindex, fileindex_path);
5657 if (sync_err && err == NULL)
5658 err = sync_err;
5659 done:
5660 if (fileindex)
5661 got_fileindex_free(fileindex);
5662 free(fileindex_path);
5663 unlockerr = lock_worktree(worktree, LOCK_SH);
5664 if (unlockerr && err == NULL)
5665 err = unlockerr;
5666 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5667 struct got_commitable *ct = pe->data;
5668 free_commitable(ct);
5670 got_pathlist_free(&commitable_paths);
5671 return err;
5674 const char *
5675 got_commitable_get_path(struct got_commitable *ct)
5677 return ct->path;
5680 unsigned int
5681 got_commitable_get_status(struct got_commitable *ct)
5683 return ct->status;
5686 struct check_rebase_ok_arg {
5687 struct got_worktree *worktree;
5688 struct got_repository *repo;
5691 static const struct got_error *
5692 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5694 const struct got_error *err = NULL;
5695 struct check_rebase_ok_arg *a = arg;
5696 unsigned char status;
5697 struct stat sb;
5698 char *ondisk_path;
5700 /* Reject rebase of a work tree with mixed base commits. */
5701 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5702 SHA1_DIGEST_LENGTH))
5703 return got_error(GOT_ERR_MIXED_COMMITS);
5705 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5706 == -1)
5707 return got_error_from_errno("asprintf");
5709 /* Reject rebase of a work tree with modified or staged files. */
5710 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5711 free(ondisk_path);
5712 if (err)
5713 return err;
5715 if (status != GOT_STATUS_NO_CHANGE)
5716 return got_error(GOT_ERR_MODIFIED);
5717 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5718 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5720 return NULL;
5723 const struct got_error *
5724 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5725 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5726 struct got_worktree *worktree, struct got_reference *branch,
5727 struct got_repository *repo)
5729 const struct got_error *err = NULL;
5730 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5731 char *branch_ref_name = NULL;
5732 char *fileindex_path = NULL;
5733 struct check_rebase_ok_arg ok_arg;
5734 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5735 struct got_object_id *wt_branch_tip = NULL;
5737 *new_base_branch_ref = NULL;
5738 *tmp_branch = NULL;
5739 *fileindex = NULL;
5741 err = lock_worktree(worktree, LOCK_EX);
5742 if (err)
5743 return err;
5745 err = open_fileindex(fileindex, &fileindex_path, worktree);
5746 if (err)
5747 goto done;
5749 ok_arg.worktree = worktree;
5750 ok_arg.repo = repo;
5751 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5752 &ok_arg);
5753 if (err)
5754 goto done;
5756 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5757 if (err)
5758 goto done;
5760 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5761 if (err)
5762 goto done;
5764 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5765 if (err)
5766 goto done;
5768 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5769 0);
5770 if (err)
5771 goto done;
5773 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5774 if (err)
5775 goto done;
5776 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5777 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5778 goto done;
5781 err = got_ref_alloc_symref(new_base_branch_ref,
5782 new_base_branch_ref_name, wt_branch);
5783 if (err)
5784 goto done;
5785 err = got_ref_write(*new_base_branch_ref, repo);
5786 if (err)
5787 goto done;
5789 /* TODO Lock original branch's ref while rebasing? */
5791 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5792 if (err)
5793 goto done;
5795 err = got_ref_write(branch_ref, repo);
5796 if (err)
5797 goto done;
5799 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5800 worktree->base_commit_id);
5801 if (err)
5802 goto done;
5803 err = got_ref_write(*tmp_branch, repo);
5804 if (err)
5805 goto done;
5807 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5808 if (err)
5809 goto done;
5810 done:
5811 free(fileindex_path);
5812 free(tmp_branch_name);
5813 free(new_base_branch_ref_name);
5814 free(branch_ref_name);
5815 if (branch_ref)
5816 got_ref_close(branch_ref);
5817 if (wt_branch)
5818 got_ref_close(wt_branch);
5819 free(wt_branch_tip);
5820 if (err) {
5821 if (*new_base_branch_ref) {
5822 got_ref_close(*new_base_branch_ref);
5823 *new_base_branch_ref = NULL;
5825 if (*tmp_branch) {
5826 got_ref_close(*tmp_branch);
5827 *tmp_branch = NULL;
5829 if (*fileindex) {
5830 got_fileindex_free(*fileindex);
5831 *fileindex = NULL;
5833 lock_worktree(worktree, LOCK_SH);
5835 return err;
5838 const struct got_error *
5839 got_worktree_rebase_continue(struct got_object_id **commit_id,
5840 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5841 struct got_reference **branch, struct got_fileindex **fileindex,
5842 struct got_worktree *worktree, struct got_repository *repo)
5844 const struct got_error *err;
5845 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5846 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5847 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5848 char *fileindex_path = NULL;
5849 int have_staged_files = 0;
5851 *commit_id = NULL;
5852 *new_base_branch = NULL;
5853 *tmp_branch = NULL;
5854 *branch = NULL;
5855 *fileindex = NULL;
5857 err = lock_worktree(worktree, LOCK_EX);
5858 if (err)
5859 return err;
5861 err = open_fileindex(fileindex, &fileindex_path, worktree);
5862 if (err)
5863 goto done;
5865 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5866 &have_staged_files);
5867 if (err && err->code != GOT_ERR_CANCELLED)
5868 goto done;
5869 if (have_staged_files) {
5870 err = got_error(GOT_ERR_STAGED_PATHS);
5871 goto done;
5874 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5875 if (err)
5876 goto done;
5878 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5879 if (err)
5880 goto done;
5882 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5883 if (err)
5884 goto done;
5886 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5887 if (err)
5888 goto done;
5890 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5891 if (err)
5892 goto done;
5894 err = got_ref_open(branch, repo,
5895 got_ref_get_symref_target(branch_ref), 0);
5896 if (err)
5897 goto done;
5899 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5900 if (err)
5901 goto done;
5903 err = got_ref_resolve(commit_id, repo, commit_ref);
5904 if (err)
5905 goto done;
5907 err = got_ref_open(new_base_branch, repo,
5908 new_base_branch_ref_name, 0);
5909 if (err)
5910 goto done;
5912 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5913 if (err)
5914 goto done;
5915 done:
5916 free(commit_ref_name);
5917 free(branch_ref_name);
5918 free(fileindex_path);
5919 if (commit_ref)
5920 got_ref_close(commit_ref);
5921 if (branch_ref)
5922 got_ref_close(branch_ref);
5923 if (err) {
5924 free(*commit_id);
5925 *commit_id = NULL;
5926 if (*tmp_branch) {
5927 got_ref_close(*tmp_branch);
5928 *tmp_branch = NULL;
5930 if (*new_base_branch) {
5931 got_ref_close(*new_base_branch);
5932 *new_base_branch = NULL;
5934 if (*branch) {
5935 got_ref_close(*branch);
5936 *branch = NULL;
5938 if (*fileindex) {
5939 got_fileindex_free(*fileindex);
5940 *fileindex = NULL;
5942 lock_worktree(worktree, LOCK_SH);
5944 return err;
5947 const struct got_error *
5948 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5950 const struct got_error *err;
5951 char *tmp_branch_name = NULL;
5953 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5954 if (err)
5955 return err;
5957 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5958 free(tmp_branch_name);
5959 return NULL;
5962 static const struct got_error *
5963 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5964 char **logmsg, void *arg)
5966 *logmsg = arg;
5967 return NULL;
5970 static const struct got_error *
5971 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5972 const char *path, struct got_object_id *blob_id,
5973 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5974 int dirfd, const char *de_name)
5976 return NULL;
5979 struct collect_merged_paths_arg {
5980 got_worktree_checkout_cb progress_cb;
5981 void *progress_arg;
5982 struct got_pathlist_head *merged_paths;
5985 static const struct got_error *
5986 collect_merged_paths(void *arg, unsigned char status, const char *path)
5988 const struct got_error *err;
5989 struct collect_merged_paths_arg *a = arg;
5990 char *p;
5991 struct got_pathlist_entry *new;
5993 err = (*a->progress_cb)(a->progress_arg, status, path);
5994 if (err)
5995 return err;
5997 if (status != GOT_STATUS_MERGE &&
5998 status != GOT_STATUS_ADD &&
5999 status != GOT_STATUS_DELETE &&
6000 status != GOT_STATUS_CONFLICT)
6001 return NULL;
6003 p = strdup(path);
6004 if (p == NULL)
6005 return got_error_from_errno("strdup");
6007 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6008 if (err || new == NULL)
6009 free(p);
6010 return err;
6013 void
6014 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6016 struct got_pathlist_entry *pe;
6018 TAILQ_FOREACH(pe, merged_paths, entry)
6019 free((char *)pe->path);
6021 got_pathlist_free(merged_paths);
6024 static const struct got_error *
6025 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6026 int is_rebase, struct got_repository *repo)
6028 const struct got_error *err;
6029 struct got_reference *commit_ref = NULL;
6031 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6032 if (err) {
6033 if (err->code != GOT_ERR_NOT_REF)
6034 goto done;
6035 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6036 if (err)
6037 goto done;
6038 err = got_ref_write(commit_ref, repo);
6039 if (err)
6040 goto done;
6041 } else if (is_rebase) {
6042 struct got_object_id *stored_id;
6043 int cmp;
6045 err = got_ref_resolve(&stored_id, repo, commit_ref);
6046 if (err)
6047 goto done;
6048 cmp = got_object_id_cmp(commit_id, stored_id);
6049 free(stored_id);
6050 if (cmp != 0) {
6051 err = got_error(GOT_ERR_REBASE_COMMITID);
6052 goto done;
6055 done:
6056 if (commit_ref)
6057 got_ref_close(commit_ref);
6058 return err;
6061 static const struct got_error *
6062 rebase_merge_files(struct got_pathlist_head *merged_paths,
6063 const char *commit_ref_name, struct got_worktree *worktree,
6064 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6065 struct got_object_id *commit_id, struct got_repository *repo,
6066 got_worktree_checkout_cb progress_cb, void *progress_arg,
6067 got_cancel_cb cancel_cb, void *cancel_arg)
6069 const struct got_error *err;
6070 struct got_reference *commit_ref = NULL;
6071 struct collect_merged_paths_arg cmp_arg;
6072 char *fileindex_path;
6074 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6076 err = get_fileindex_path(&fileindex_path, worktree);
6077 if (err)
6078 return err;
6080 cmp_arg.progress_cb = progress_cb;
6081 cmp_arg.progress_arg = progress_arg;
6082 cmp_arg.merged_paths = merged_paths;
6083 err = merge_files(worktree, fileindex, fileindex_path,
6084 parent_commit_id, commit_id, repo, collect_merged_paths,
6085 &cmp_arg, cancel_cb, cancel_arg);
6086 if (commit_ref)
6087 got_ref_close(commit_ref);
6088 return err;
6091 const struct got_error *
6092 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6093 struct got_worktree *worktree, struct got_fileindex *fileindex,
6094 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6095 struct got_repository *repo,
6096 got_worktree_checkout_cb progress_cb, void *progress_arg,
6097 got_cancel_cb cancel_cb, void *cancel_arg)
6099 const struct got_error *err;
6100 char *commit_ref_name;
6102 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6103 if (err)
6104 return err;
6106 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6107 if (err)
6108 goto done;
6110 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6111 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6112 progress_arg, cancel_cb, cancel_arg);
6113 done:
6114 free(commit_ref_name);
6115 return err;
6118 const struct got_error *
6119 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6120 struct got_worktree *worktree, struct got_fileindex *fileindex,
6121 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6122 struct got_repository *repo,
6123 got_worktree_checkout_cb progress_cb, void *progress_arg,
6124 got_cancel_cb cancel_cb, void *cancel_arg)
6126 const struct got_error *err;
6127 char *commit_ref_name;
6129 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6130 if (err)
6131 return err;
6133 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6134 if (err)
6135 goto done;
6137 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6138 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6139 progress_arg, cancel_cb, cancel_arg);
6140 done:
6141 free(commit_ref_name);
6142 return err;
6145 static const struct got_error *
6146 rebase_commit(struct got_object_id **new_commit_id,
6147 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6148 struct got_worktree *worktree, struct got_fileindex *fileindex,
6149 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6150 const char *new_logmsg, struct got_repository *repo)
6152 const struct got_error *err, *sync_err;
6153 struct got_pathlist_head commitable_paths;
6154 struct collect_commitables_arg cc_arg;
6155 char *fileindex_path = NULL;
6156 struct got_reference *head_ref = NULL;
6157 struct got_object_id *head_commit_id = NULL;
6158 char *logmsg = NULL;
6160 TAILQ_INIT(&commitable_paths);
6161 *new_commit_id = NULL;
6163 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6165 err = get_fileindex_path(&fileindex_path, worktree);
6166 if (err)
6167 return err;
6169 cc_arg.commitable_paths = &commitable_paths;
6170 cc_arg.worktree = worktree;
6171 cc_arg.repo = repo;
6172 cc_arg.have_staged_files = 0;
6174 * If possible get the status of individual files directly to
6175 * avoid crawling the entire work tree once per rebased commit.
6177 * Ideally, merged_paths would contain a list of commitables
6178 * we could use so we could skip worktree_status() entirely.
6179 * However, we would then need carefully keep track of cumulative
6180 * effects of operations such as file additions and deletions
6181 * in 'got histedit -f' (folding multiple commits into one),
6182 * and this extra complexity is not really worth it.
6184 if (merged_paths) {
6185 struct got_pathlist_entry *pe;
6186 TAILQ_FOREACH(pe, merged_paths, entry) {
6187 err = worktree_status(worktree, pe->path, fileindex,
6188 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6189 0);
6190 if (err)
6191 goto done;
6193 } else {
6194 err = worktree_status(worktree, "", fileindex, repo,
6195 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6196 if (err)
6197 goto done;
6200 if (TAILQ_EMPTY(&commitable_paths)) {
6201 /* No-op change; commit will be elided. */
6202 err = got_ref_delete(commit_ref, repo);
6203 if (err)
6204 goto done;
6205 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6206 goto done;
6209 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6210 if (err)
6211 goto done;
6213 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6214 if (err)
6215 goto done;
6217 if (new_logmsg) {
6218 logmsg = strdup(new_logmsg);
6219 if (logmsg == NULL) {
6220 err = got_error_from_errno("strdup");
6221 goto done;
6223 } else {
6224 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6225 if (err)
6226 goto done;
6229 /* NB: commit_worktree will call free(logmsg) */
6230 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6231 NULL, worktree, got_object_commit_get_author(orig_commit),
6232 got_object_commit_get_committer(orig_commit),
6233 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6234 if (err)
6235 goto done;
6237 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6238 if (err)
6239 goto done;
6241 err = got_ref_delete(commit_ref, repo);
6242 if (err)
6243 goto done;
6245 err = update_fileindex_after_commit(worktree, &commitable_paths,
6246 *new_commit_id, fileindex, 0);
6247 sync_err = sync_fileindex(fileindex, fileindex_path);
6248 if (sync_err && err == NULL)
6249 err = sync_err;
6250 done:
6251 free(fileindex_path);
6252 free(head_commit_id);
6253 if (head_ref)
6254 got_ref_close(head_ref);
6255 if (err) {
6256 free(*new_commit_id);
6257 *new_commit_id = NULL;
6259 return err;
6262 const struct got_error *
6263 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6264 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6265 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6266 struct got_commit_object *orig_commit,
6267 struct got_object_id *orig_commit_id, struct got_repository *repo)
6269 const struct got_error *err;
6270 char *commit_ref_name;
6271 struct got_reference *commit_ref = NULL;
6272 struct got_object_id *commit_id = NULL;
6274 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6275 if (err)
6276 return err;
6278 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6279 if (err)
6280 goto done;
6281 err = got_ref_resolve(&commit_id, repo, commit_ref);
6282 if (err)
6283 goto done;
6284 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6285 err = got_error(GOT_ERR_REBASE_COMMITID);
6286 goto done;
6289 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6290 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6291 done:
6292 if (commit_ref)
6293 got_ref_close(commit_ref);
6294 free(commit_ref_name);
6295 free(commit_id);
6296 return err;
6299 const struct got_error *
6300 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6301 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6302 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6303 struct got_commit_object *orig_commit,
6304 struct got_object_id *orig_commit_id, const char *new_logmsg,
6305 struct got_repository *repo)
6307 const struct got_error *err;
6308 char *commit_ref_name;
6309 struct got_reference *commit_ref = NULL;
6311 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6312 if (err)
6313 return err;
6315 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6316 if (err)
6317 goto done;
6319 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6320 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6321 done:
6322 if (commit_ref)
6323 got_ref_close(commit_ref);
6324 free(commit_ref_name);
6325 return err;
6328 const struct got_error *
6329 got_worktree_rebase_postpone(struct got_worktree *worktree,
6330 struct got_fileindex *fileindex)
6332 if (fileindex)
6333 got_fileindex_free(fileindex);
6334 return lock_worktree(worktree, LOCK_SH);
6337 static const struct got_error *
6338 delete_ref(const char *name, struct got_repository *repo)
6340 const struct got_error *err;
6341 struct got_reference *ref;
6343 err = got_ref_open(&ref, repo, name, 0);
6344 if (err) {
6345 if (err->code == GOT_ERR_NOT_REF)
6346 return NULL;
6347 return err;
6350 err = got_ref_delete(ref, repo);
6351 got_ref_close(ref);
6352 return err;
6355 static const struct got_error *
6356 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6358 const struct got_error *err;
6359 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6360 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6362 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6363 if (err)
6364 goto done;
6365 err = delete_ref(tmp_branch_name, repo);
6366 if (err)
6367 goto done;
6369 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6370 if (err)
6371 goto done;
6372 err = delete_ref(new_base_branch_ref_name, repo);
6373 if (err)
6374 goto done;
6376 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6377 if (err)
6378 goto done;
6379 err = delete_ref(branch_ref_name, repo);
6380 if (err)
6381 goto done;
6383 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6384 if (err)
6385 goto done;
6386 err = delete_ref(commit_ref_name, repo);
6387 if (err)
6388 goto done;
6390 done:
6391 free(tmp_branch_name);
6392 free(new_base_branch_ref_name);
6393 free(branch_ref_name);
6394 free(commit_ref_name);
6395 return err;
6398 const struct got_error *
6399 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6400 struct got_object_id *new_commit_id, struct got_repository *repo)
6402 const struct got_error *err;
6403 struct got_reference *ref = NULL;
6404 struct got_object_id *old_commit_id = NULL;
6405 const char *branch_name = NULL;
6406 char *new_id_str = NULL;
6407 char *refname = NULL;
6409 branch_name = got_ref_get_name(branch);
6410 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6411 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6412 branch_name += 11;
6414 err = got_object_id_str(&new_id_str, new_commit_id);
6415 if (err)
6416 return err;
6418 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6419 new_id_str) == -1) {
6420 err = got_error_from_errno("asprintf");
6421 goto done;
6424 err = got_ref_resolve(&old_commit_id, repo, branch);
6425 if (err)
6426 goto done;
6428 err = got_ref_alloc(&ref, refname, old_commit_id);
6429 if (err)
6430 goto done;
6432 err = got_ref_write(ref, repo);
6433 done:
6434 free(new_id_str);
6435 free(refname);
6436 free(old_commit_id);
6437 if (ref)
6438 got_ref_close(ref);
6439 return err;
6442 const struct got_error *
6443 got_worktree_rebase_complete(struct got_worktree *worktree,
6444 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6445 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6446 struct got_repository *repo, int create_backup)
6448 const struct got_error *err, *unlockerr, *sync_err;
6449 struct got_object_id *new_head_commit_id = NULL;
6450 char *fileindex_path = NULL;
6452 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6453 if (err)
6454 return err;
6456 if (create_backup) {
6457 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6458 rebased_branch, new_head_commit_id, repo);
6459 if (err)
6460 goto done;
6463 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6464 if (err)
6465 goto done;
6467 err = got_ref_write(rebased_branch, repo);
6468 if (err)
6469 goto done;
6471 err = got_worktree_set_head_ref(worktree, rebased_branch);
6472 if (err)
6473 goto done;
6475 err = delete_rebase_refs(worktree, repo);
6476 if (err)
6477 goto done;
6479 err = get_fileindex_path(&fileindex_path, worktree);
6480 if (err)
6481 goto done;
6482 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6483 sync_err = sync_fileindex(fileindex, fileindex_path);
6484 if (sync_err && err == NULL)
6485 err = sync_err;
6486 done:
6487 got_fileindex_free(fileindex);
6488 free(fileindex_path);
6489 free(new_head_commit_id);
6490 unlockerr = lock_worktree(worktree, LOCK_SH);
6491 if (unlockerr && err == NULL)
6492 err = unlockerr;
6493 return err;
6496 const struct got_error *
6497 got_worktree_rebase_abort(struct got_worktree *worktree,
6498 struct got_fileindex *fileindex, struct got_repository *repo,
6499 struct got_reference *new_base_branch,
6500 got_worktree_checkout_cb progress_cb, void *progress_arg)
6502 const struct got_error *err, *unlockerr, *sync_err;
6503 struct got_reference *resolved = NULL;
6504 struct got_object_id *commit_id = NULL;
6505 char *fileindex_path = NULL;
6506 struct revert_file_args rfa;
6507 struct got_object_id *tree_id = NULL;
6509 err = lock_worktree(worktree, LOCK_EX);
6510 if (err)
6511 return err;
6513 err = got_ref_open(&resolved, repo,
6514 got_ref_get_symref_target(new_base_branch), 0);
6515 if (err)
6516 goto done;
6518 err = got_worktree_set_head_ref(worktree, resolved);
6519 if (err)
6520 goto done;
6523 * XXX commits to the base branch could have happened while
6524 * we were busy rebasing; should we store the original commit ID
6525 * when rebase begins and read it back here?
6527 err = got_ref_resolve(&commit_id, repo, resolved);
6528 if (err)
6529 goto done;
6531 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6532 if (err)
6533 goto done;
6535 err = got_object_id_by_path(&tree_id, repo,
6536 worktree->base_commit_id, worktree->path_prefix);
6537 if (err)
6538 goto done;
6540 err = delete_rebase_refs(worktree, repo);
6541 if (err)
6542 goto done;
6544 err = get_fileindex_path(&fileindex_path, worktree);
6545 if (err)
6546 goto done;
6548 rfa.worktree = worktree;
6549 rfa.fileindex = fileindex;
6550 rfa.progress_cb = progress_cb;
6551 rfa.progress_arg = progress_arg;
6552 rfa.patch_cb = NULL;
6553 rfa.patch_arg = NULL;
6554 rfa.repo = repo;
6555 rfa.unlink_added_files = 0;
6556 err = worktree_status(worktree, "", fileindex, repo,
6557 revert_file, &rfa, NULL, NULL, 1, 0);
6558 if (err)
6559 goto sync;
6561 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6562 repo, progress_cb, progress_arg, NULL, NULL);
6563 sync:
6564 sync_err = sync_fileindex(fileindex, fileindex_path);
6565 if (sync_err && err == NULL)
6566 err = sync_err;
6567 done:
6568 got_ref_close(resolved);
6569 free(tree_id);
6570 free(commit_id);
6571 if (fileindex)
6572 got_fileindex_free(fileindex);
6573 free(fileindex_path);
6575 unlockerr = lock_worktree(worktree, LOCK_SH);
6576 if (unlockerr && err == NULL)
6577 err = unlockerr;
6578 return err;
6581 const struct got_error *
6582 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6583 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6584 struct got_fileindex **fileindex, struct got_worktree *worktree,
6585 struct got_repository *repo)
6587 const struct got_error *err = NULL;
6588 char *tmp_branch_name = NULL;
6589 char *branch_ref_name = NULL;
6590 char *base_commit_ref_name = NULL;
6591 char *fileindex_path = NULL;
6592 struct check_rebase_ok_arg ok_arg;
6593 struct got_reference *wt_branch = NULL;
6594 struct got_reference *base_commit_ref = NULL;
6596 *tmp_branch = NULL;
6597 *branch_ref = NULL;
6598 *base_commit_id = NULL;
6599 *fileindex = NULL;
6601 err = lock_worktree(worktree, LOCK_EX);
6602 if (err)
6603 return err;
6605 err = open_fileindex(fileindex, &fileindex_path, worktree);
6606 if (err)
6607 goto done;
6609 ok_arg.worktree = worktree;
6610 ok_arg.repo = repo;
6611 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6612 &ok_arg);
6613 if (err)
6614 goto done;
6616 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6617 if (err)
6618 goto done;
6620 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6621 if (err)
6622 goto done;
6624 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6625 worktree);
6626 if (err)
6627 goto done;
6629 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6630 0);
6631 if (err)
6632 goto done;
6634 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6635 if (err)
6636 goto done;
6638 err = got_ref_write(*branch_ref, repo);
6639 if (err)
6640 goto done;
6642 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6643 worktree->base_commit_id);
6644 if (err)
6645 goto done;
6646 err = got_ref_write(base_commit_ref, repo);
6647 if (err)
6648 goto done;
6649 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6650 if (*base_commit_id == NULL) {
6651 err = got_error_from_errno("got_object_id_dup");
6652 goto done;
6655 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6656 worktree->base_commit_id);
6657 if (err)
6658 goto done;
6659 err = got_ref_write(*tmp_branch, repo);
6660 if (err)
6661 goto done;
6663 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6664 if (err)
6665 goto done;
6666 done:
6667 free(fileindex_path);
6668 free(tmp_branch_name);
6669 free(branch_ref_name);
6670 free(base_commit_ref_name);
6671 if (wt_branch)
6672 got_ref_close(wt_branch);
6673 if (err) {
6674 if (*branch_ref) {
6675 got_ref_close(*branch_ref);
6676 *branch_ref = NULL;
6678 if (*tmp_branch) {
6679 got_ref_close(*tmp_branch);
6680 *tmp_branch = NULL;
6682 free(*base_commit_id);
6683 if (*fileindex) {
6684 got_fileindex_free(*fileindex);
6685 *fileindex = NULL;
6687 lock_worktree(worktree, LOCK_SH);
6689 return err;
6692 const struct got_error *
6693 got_worktree_histedit_postpone(struct got_worktree *worktree,
6694 struct got_fileindex *fileindex)
6696 if (fileindex)
6697 got_fileindex_free(fileindex);
6698 return lock_worktree(worktree, LOCK_SH);
6701 const struct got_error *
6702 got_worktree_histedit_in_progress(int *in_progress,
6703 struct got_worktree *worktree)
6705 const struct got_error *err;
6706 char *tmp_branch_name = NULL;
6708 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6709 if (err)
6710 return err;
6712 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6713 free(tmp_branch_name);
6714 return NULL;
6717 const struct got_error *
6718 got_worktree_histedit_continue(struct got_object_id **commit_id,
6719 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6720 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6721 struct got_worktree *worktree, struct got_repository *repo)
6723 const struct got_error *err;
6724 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6725 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6726 struct got_reference *commit_ref = NULL;
6727 struct got_reference *base_commit_ref = NULL;
6728 char *fileindex_path = NULL;
6729 int have_staged_files = 0;
6731 *commit_id = NULL;
6732 *tmp_branch = NULL;
6733 *base_commit_id = NULL;
6734 *fileindex = NULL;
6736 err = lock_worktree(worktree, LOCK_EX);
6737 if (err)
6738 return err;
6740 err = open_fileindex(fileindex, &fileindex_path, worktree);
6741 if (err)
6742 goto done;
6744 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6745 &have_staged_files);
6746 if (err && err->code != GOT_ERR_CANCELLED)
6747 goto done;
6748 if (have_staged_files) {
6749 err = got_error(GOT_ERR_STAGED_PATHS);
6750 goto done;
6753 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6754 if (err)
6755 goto done;
6757 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6758 if (err)
6759 goto done;
6761 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6762 if (err)
6763 goto done;
6765 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6766 worktree);
6767 if (err)
6768 goto done;
6770 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6771 if (err)
6772 goto done;
6774 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6775 if (err)
6776 goto done;
6777 err = got_ref_resolve(commit_id, repo, commit_ref);
6778 if (err)
6779 goto done;
6781 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6782 if (err)
6783 goto done;
6784 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6785 if (err)
6786 goto done;
6788 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6789 if (err)
6790 goto done;
6791 done:
6792 free(commit_ref_name);
6793 free(branch_ref_name);
6794 free(fileindex_path);
6795 if (commit_ref)
6796 got_ref_close(commit_ref);
6797 if (base_commit_ref)
6798 got_ref_close(base_commit_ref);
6799 if (err) {
6800 free(*commit_id);
6801 *commit_id = NULL;
6802 free(*base_commit_id);
6803 *base_commit_id = NULL;
6804 if (*tmp_branch) {
6805 got_ref_close(*tmp_branch);
6806 *tmp_branch = NULL;
6808 if (*fileindex) {
6809 got_fileindex_free(*fileindex);
6810 *fileindex = NULL;
6812 lock_worktree(worktree, LOCK_EX);
6814 return err;
6817 static const struct got_error *
6818 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6820 const struct got_error *err;
6821 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6822 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6824 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6825 if (err)
6826 goto done;
6827 err = delete_ref(tmp_branch_name, repo);
6828 if (err)
6829 goto done;
6831 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6832 worktree);
6833 if (err)
6834 goto done;
6835 err = delete_ref(base_commit_ref_name, repo);
6836 if (err)
6837 goto done;
6839 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6840 if (err)
6841 goto done;
6842 err = delete_ref(branch_ref_name, repo);
6843 if (err)
6844 goto done;
6846 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6847 if (err)
6848 goto done;
6849 err = delete_ref(commit_ref_name, repo);
6850 if (err)
6851 goto done;
6852 done:
6853 free(tmp_branch_name);
6854 free(base_commit_ref_name);
6855 free(branch_ref_name);
6856 free(commit_ref_name);
6857 return err;
6860 const struct got_error *
6861 got_worktree_histedit_abort(struct got_worktree *worktree,
6862 struct got_fileindex *fileindex, struct got_repository *repo,
6863 struct got_reference *branch, struct got_object_id *base_commit_id,
6864 got_worktree_checkout_cb progress_cb, void *progress_arg)
6866 const struct got_error *err, *unlockerr, *sync_err;
6867 struct got_reference *resolved = NULL;
6868 char *fileindex_path = NULL;
6869 struct got_object_id *tree_id = NULL;
6870 struct revert_file_args rfa;
6872 err = lock_worktree(worktree, LOCK_EX);
6873 if (err)
6874 return err;
6876 err = got_ref_open(&resolved, repo,
6877 got_ref_get_symref_target(branch), 0);
6878 if (err)
6879 goto done;
6881 err = got_worktree_set_head_ref(worktree, resolved);
6882 if (err)
6883 goto done;
6885 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6886 if (err)
6887 goto done;
6889 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6890 worktree->path_prefix);
6891 if (err)
6892 goto done;
6894 err = delete_histedit_refs(worktree, repo);
6895 if (err)
6896 goto done;
6898 err = get_fileindex_path(&fileindex_path, worktree);
6899 if (err)
6900 goto done;
6902 rfa.worktree = worktree;
6903 rfa.fileindex = fileindex;
6904 rfa.progress_cb = progress_cb;
6905 rfa.progress_arg = progress_arg;
6906 rfa.patch_cb = NULL;
6907 rfa.patch_arg = NULL;
6908 rfa.repo = repo;
6909 rfa.unlink_added_files = 0;
6910 err = worktree_status(worktree, "", fileindex, repo,
6911 revert_file, &rfa, NULL, NULL, 1, 0);
6912 if (err)
6913 goto sync;
6915 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6916 repo, progress_cb, progress_arg, NULL, NULL);
6917 sync:
6918 sync_err = sync_fileindex(fileindex, fileindex_path);
6919 if (sync_err && err == NULL)
6920 err = sync_err;
6921 done:
6922 got_ref_close(resolved);
6923 free(tree_id);
6924 free(fileindex_path);
6926 unlockerr = lock_worktree(worktree, LOCK_SH);
6927 if (unlockerr && err == NULL)
6928 err = unlockerr;
6929 return err;
6932 const struct got_error *
6933 got_worktree_histedit_complete(struct got_worktree *worktree,
6934 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6935 struct got_reference *edited_branch, struct got_repository *repo)
6937 const struct got_error *err, *unlockerr, *sync_err;
6938 struct got_object_id *new_head_commit_id = NULL;
6939 struct got_reference *resolved = NULL;
6940 char *fileindex_path = NULL;
6942 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6943 if (err)
6944 return err;
6946 err = got_ref_open(&resolved, repo,
6947 got_ref_get_symref_target(edited_branch), 0);
6948 if (err)
6949 goto done;
6951 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
6952 resolved, new_head_commit_id, repo);
6953 if (err)
6954 goto done;
6956 err = got_ref_change_ref(resolved, new_head_commit_id);
6957 if (err)
6958 goto done;
6960 err = got_ref_write(resolved, repo);
6961 if (err)
6962 goto done;
6964 err = got_worktree_set_head_ref(worktree, resolved);
6965 if (err)
6966 goto done;
6968 err = delete_histedit_refs(worktree, repo);
6969 if (err)
6970 goto done;
6972 err = get_fileindex_path(&fileindex_path, worktree);
6973 if (err)
6974 goto done;
6975 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6976 sync_err = sync_fileindex(fileindex, fileindex_path);
6977 if (sync_err && err == NULL)
6978 err = sync_err;
6979 done:
6980 got_fileindex_free(fileindex);
6981 free(fileindex_path);
6982 free(new_head_commit_id);
6983 unlockerr = lock_worktree(worktree, LOCK_SH);
6984 if (unlockerr && err == NULL)
6985 err = unlockerr;
6986 return err;
6989 const struct got_error *
6990 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6991 struct got_object_id *commit_id, struct got_repository *repo)
6993 const struct got_error *err;
6994 char *commit_ref_name;
6996 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6997 if (err)
6998 return err;
7000 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7001 if (err)
7002 goto done;
7004 err = delete_ref(commit_ref_name, repo);
7005 done:
7006 free(commit_ref_name);
7007 return err;
7010 const struct got_error *
7011 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7012 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7013 struct got_worktree *worktree, const char *refname,
7014 struct got_repository *repo)
7016 const struct got_error *err = NULL;
7017 char *fileindex_path = NULL;
7018 struct check_rebase_ok_arg ok_arg;
7020 *fileindex = NULL;
7021 *branch_ref = NULL;
7022 *base_branch_ref = NULL;
7024 err = lock_worktree(worktree, LOCK_EX);
7025 if (err)
7026 return err;
7028 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7029 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7030 "cannot integrate a branch into itself; "
7031 "update -b or different branch name required");
7032 goto done;
7035 err = open_fileindex(fileindex, &fileindex_path, worktree);
7036 if (err)
7037 goto done;
7039 /* Preconditions are the same as for rebase. */
7040 ok_arg.worktree = worktree;
7041 ok_arg.repo = repo;
7042 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7043 &ok_arg);
7044 if (err)
7045 goto done;
7047 err = got_ref_open(branch_ref, repo, refname, 1);
7048 if (err)
7049 goto done;
7051 err = got_ref_open(base_branch_ref, repo,
7052 got_worktree_get_head_ref_name(worktree), 1);
7053 done:
7054 if (err) {
7055 if (*branch_ref) {
7056 got_ref_close(*branch_ref);
7057 *branch_ref = NULL;
7059 if (*base_branch_ref) {
7060 got_ref_close(*base_branch_ref);
7061 *base_branch_ref = NULL;
7063 if (*fileindex) {
7064 got_fileindex_free(*fileindex);
7065 *fileindex = NULL;
7067 lock_worktree(worktree, LOCK_SH);
7069 return err;
7072 const struct got_error *
7073 got_worktree_integrate_continue(struct got_worktree *worktree,
7074 struct got_fileindex *fileindex, struct got_repository *repo,
7075 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7076 got_worktree_checkout_cb progress_cb, void *progress_arg,
7077 got_cancel_cb cancel_cb, void *cancel_arg)
7079 const struct got_error *err = NULL, *sync_err, *unlockerr;
7080 char *fileindex_path = NULL;
7081 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7083 err = get_fileindex_path(&fileindex_path, worktree);
7084 if (err)
7085 goto done;
7087 err = got_ref_resolve(&commit_id, repo, branch_ref);
7088 if (err)
7089 goto done;
7091 err = got_object_id_by_path(&tree_id, repo, commit_id,
7092 worktree->path_prefix);
7093 if (err)
7094 goto done;
7096 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7097 if (err)
7098 goto done;
7100 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7101 progress_cb, progress_arg, cancel_cb, cancel_arg);
7102 if (err)
7103 goto sync;
7105 err = got_ref_change_ref(base_branch_ref, commit_id);
7106 if (err)
7107 goto sync;
7109 err = got_ref_write(base_branch_ref, repo);
7110 if (err)
7111 goto sync;
7113 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7114 sync:
7115 sync_err = sync_fileindex(fileindex, fileindex_path);
7116 if (sync_err && err == NULL)
7117 err = sync_err;
7119 done:
7120 unlockerr = got_ref_unlock(branch_ref);
7121 if (unlockerr && err == NULL)
7122 err = unlockerr;
7123 got_ref_close(branch_ref);
7125 unlockerr = got_ref_unlock(base_branch_ref);
7126 if (unlockerr && err == NULL)
7127 err = unlockerr;
7128 got_ref_close(base_branch_ref);
7130 got_fileindex_free(fileindex);
7131 free(fileindex_path);
7132 free(tree_id);
7134 unlockerr = lock_worktree(worktree, LOCK_SH);
7135 if (unlockerr && err == NULL)
7136 err = unlockerr;
7137 return err;
7140 const struct got_error *
7141 got_worktree_integrate_abort(struct got_worktree *worktree,
7142 struct got_fileindex *fileindex, struct got_repository *repo,
7143 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7145 const struct got_error *err = NULL, *unlockerr = NULL;
7147 got_fileindex_free(fileindex);
7149 err = lock_worktree(worktree, LOCK_SH);
7151 unlockerr = got_ref_unlock(branch_ref);
7152 if (unlockerr && err == NULL)
7153 err = unlockerr;
7154 got_ref_close(branch_ref);
7156 unlockerr = got_ref_unlock(base_branch_ref);
7157 if (unlockerr && err == NULL)
7158 err = unlockerr;
7159 got_ref_close(base_branch_ref);
7161 return err;
7164 const struct got_error *
7165 got_worktree_merge_postpone(struct got_worktree *worktree,
7166 struct got_fileindex *fileindex)
7168 const struct got_error *err, *sync_err;
7169 char *fileindex_path = NULL;
7171 err = get_fileindex_path(&fileindex_path, worktree);
7172 if (err)
7173 goto done;
7175 sync_err = sync_fileindex(fileindex, fileindex_path);
7177 err = lock_worktree(worktree, LOCK_SH);
7178 if (sync_err && err == NULL)
7179 err = sync_err;
7180 done:
7181 got_fileindex_free(fileindex);
7182 free(fileindex_path);
7183 return err;
7186 static const struct got_error *
7187 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7189 const struct got_error *err;
7190 char *branch_refname = NULL, *commit_refname = NULL;
7192 err = get_merge_branch_ref_name(&branch_refname, worktree);
7193 if (err)
7194 goto done;
7195 err = delete_ref(branch_refname, repo);
7196 if (err)
7197 goto done;
7199 err = get_merge_commit_ref_name(&commit_refname, worktree);
7200 if (err)
7201 goto done;
7202 err = delete_ref(commit_refname, repo);
7203 if (err)
7204 goto done;
7206 done:
7207 free(branch_refname);
7208 free(commit_refname);
7209 return err;
7212 struct merge_commit_msg_arg {
7213 struct got_worktree *worktree;
7214 const char *branch_name;
7217 static const struct got_error *
7218 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7219 void *arg)
7221 struct merge_commit_msg_arg *a = arg;
7223 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7224 got_worktree_get_head_ref_name(a->worktree)) == -1)
7225 return got_error_from_errno("asprintf");
7227 return NULL;
7231 const struct got_error *
7232 got_worktree_merge_branch(struct got_worktree *worktree,
7233 struct got_fileindex *fileindex,
7234 struct got_object_id *yca_commit_id,
7235 struct got_object_id *branch_tip,
7236 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7237 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7239 const struct got_error *err;
7240 char *fileindex_path = NULL;
7242 err = get_fileindex_path(&fileindex_path, worktree);
7243 if (err)
7244 goto done;
7246 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7247 worktree);
7248 if (err)
7249 goto done;
7251 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7252 branch_tip, repo, progress_cb, progress_arg,
7253 cancel_cb, cancel_arg);
7254 done:
7255 free(fileindex_path);
7256 return err;
7259 const struct got_error *
7260 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7261 struct got_worktree *worktree, struct got_fileindex *fileindex,
7262 const char *author, const char *committer, int allow_bad_symlinks,
7263 struct got_object_id *branch_tip, const char *branch_name,
7264 struct got_repository *repo,
7265 got_worktree_status_cb status_cb, void *status_arg)
7268 const struct got_error *err = NULL, *sync_err;
7269 struct got_pathlist_head commitable_paths;
7270 struct collect_commitables_arg cc_arg;
7271 struct got_pathlist_entry *pe;
7272 struct got_reference *head_ref = NULL;
7273 struct got_object_id *head_commit_id = NULL;
7274 int have_staged_files = 0;
7275 struct merge_commit_msg_arg mcm_arg;
7276 char *fileindex_path = NULL;
7278 *new_commit_id = NULL;
7280 TAILQ_INIT(&commitable_paths);
7282 err = get_fileindex_path(&fileindex_path, worktree);
7283 if (err)
7284 goto done;
7286 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7287 if (err)
7288 goto done;
7290 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7291 if (err)
7292 goto done;
7294 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7295 &have_staged_files);
7296 if (err && err->code != GOT_ERR_CANCELLED)
7297 goto done;
7298 if (have_staged_files) {
7299 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7300 goto done;
7303 cc_arg.commitable_paths = &commitable_paths;
7304 cc_arg.worktree = worktree;
7305 cc_arg.fileindex = fileindex;
7306 cc_arg.repo = repo;
7307 cc_arg.have_staged_files = have_staged_files;
7308 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7309 err = worktree_status(worktree, "", fileindex, repo,
7310 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7311 if (err)
7312 goto done;
7314 if (TAILQ_EMPTY(&commitable_paths)) {
7315 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7316 "merge of %s cannot proceed", branch_name);
7317 goto done;
7320 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7321 struct got_commitable *ct = pe->data;
7322 const char *ct_path = ct->in_repo_path;
7324 while (ct_path[0] == '/')
7325 ct_path++;
7326 err = check_out_of_date(ct_path, ct->status,
7327 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7328 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7329 if (err)
7330 goto done;
7334 mcm_arg.worktree = worktree;
7335 mcm_arg.branch_name = branch_name;
7336 err = commit_worktree(new_commit_id, &commitable_paths,
7337 head_commit_id, branch_tip, worktree, author, committer,
7338 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7339 if (err)
7340 goto done;
7342 err = update_fileindex_after_commit(worktree, &commitable_paths,
7343 *new_commit_id, fileindex, have_staged_files);
7344 sync_err = sync_fileindex(fileindex, fileindex_path);
7345 if (sync_err && err == NULL)
7346 err = sync_err;
7347 done:
7348 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7349 struct got_commitable *ct = pe->data;
7350 free_commitable(ct);
7352 got_pathlist_free(&commitable_paths);
7353 free(fileindex_path);
7354 return err;
7357 const struct got_error *
7358 got_worktree_merge_complete(struct got_worktree *worktree,
7359 struct got_fileindex *fileindex, struct got_repository *repo)
7361 const struct got_error *err, *unlockerr, *sync_err;
7362 char *fileindex_path = NULL;
7364 err = delete_merge_refs(worktree, repo);
7365 if (err)
7366 goto done;
7368 err = get_fileindex_path(&fileindex_path, worktree);
7369 if (err)
7370 goto done;
7371 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7372 sync_err = sync_fileindex(fileindex, fileindex_path);
7373 if (sync_err && err == NULL)
7374 err = sync_err;
7375 done:
7376 got_fileindex_free(fileindex);
7377 free(fileindex_path);
7378 unlockerr = lock_worktree(worktree, LOCK_SH);
7379 if (unlockerr && err == NULL)
7380 err = unlockerr;
7381 return err;
7384 const struct got_error *
7385 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7386 struct got_repository *repo)
7388 const struct got_error *err;
7389 char *branch_refname = NULL;
7390 struct got_reference *branch_ref = NULL;
7392 *in_progress = 0;
7394 err = get_merge_branch_ref_name(&branch_refname, worktree);
7395 if (err)
7396 return err;
7397 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7398 free(branch_refname);
7399 if (err) {
7400 if (err->code != GOT_ERR_NOT_REF)
7401 return err;
7402 } else
7403 *in_progress = 1;
7405 return NULL;
7408 const struct got_error *got_worktree_merge_prepare(
7409 struct got_fileindex **fileindex, struct got_worktree *worktree,
7410 struct got_reference *branch, struct got_repository *repo)
7412 const struct got_error *err = NULL;
7413 char *fileindex_path = NULL;
7414 char *branch_refname = NULL, *commit_refname = NULL;
7415 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7416 struct got_reference *commit_ref = NULL;
7417 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7418 struct check_rebase_ok_arg ok_arg;
7420 *fileindex = NULL;
7422 err = lock_worktree(worktree, LOCK_EX);
7423 if (err)
7424 return err;
7426 err = open_fileindex(fileindex, &fileindex_path, worktree);
7427 if (err)
7428 goto done;
7430 /* Preconditions are the same as for rebase. */
7431 ok_arg.worktree = worktree;
7432 ok_arg.repo = repo;
7433 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7434 &ok_arg);
7435 if (err)
7436 goto done;
7438 err = get_merge_branch_ref_name(&branch_refname, worktree);
7439 if (err)
7440 return err;
7442 err = get_merge_commit_ref_name(&commit_refname, worktree);
7443 if (err)
7444 return err;
7446 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7447 0);
7448 if (err)
7449 goto done;
7451 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7452 if (err)
7453 goto done;
7455 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7456 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7457 goto done;
7460 err = got_ref_resolve(&branch_tip, repo, branch);
7461 if (err)
7462 goto done;
7464 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7465 if (err)
7466 goto done;
7467 err = got_ref_write(branch_ref, repo);
7468 if (err)
7469 goto done;
7471 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7472 if (err)
7473 goto done;
7474 err = got_ref_write(commit_ref, repo);
7475 if (err)
7476 goto done;
7478 done:
7479 free(branch_refname);
7480 free(commit_refname);
7481 free(fileindex_path);
7482 if (branch_ref)
7483 got_ref_close(branch_ref);
7484 if (commit_ref)
7485 got_ref_close(commit_ref);
7486 if (wt_branch)
7487 got_ref_close(wt_branch);
7488 free(wt_branch_tip);
7489 if (err) {
7490 if (*fileindex) {
7491 got_fileindex_free(*fileindex);
7492 *fileindex = NULL;
7494 lock_worktree(worktree, LOCK_SH);
7496 return err;
7499 const struct got_error *
7500 got_worktree_merge_continue(char **branch_name,
7501 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7502 struct got_worktree *worktree, struct got_repository *repo)
7504 const struct got_error *err;
7505 char *commit_refname = NULL, *branch_refname = NULL;
7506 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7507 char *fileindex_path = NULL;
7508 int have_staged_files = 0;
7510 *branch_name = NULL;
7511 *branch_tip = NULL;
7512 *fileindex = NULL;
7514 err = lock_worktree(worktree, LOCK_EX);
7515 if (err)
7516 return err;
7518 err = open_fileindex(fileindex, &fileindex_path, worktree);
7519 if (err)
7520 goto done;
7522 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7523 &have_staged_files);
7524 if (err && err->code != GOT_ERR_CANCELLED)
7525 goto done;
7526 if (have_staged_files) {
7527 err = got_error(GOT_ERR_STAGED_PATHS);
7528 goto done;
7531 err = get_merge_branch_ref_name(&branch_refname, worktree);
7532 if (err)
7533 goto done;
7535 err = get_merge_commit_ref_name(&commit_refname, worktree);
7536 if (err)
7537 goto done;
7539 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7540 if (err)
7541 goto done;
7543 if (!got_ref_is_symbolic(branch_ref)) {
7544 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7545 "%s is not a symbolic reference",
7546 got_ref_get_name(branch_ref));
7547 goto done;
7549 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7550 if (*branch_name == NULL) {
7551 err = got_error_from_errno("strdup");
7552 goto done;
7555 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7556 if (err)
7557 goto done;
7559 err = got_ref_resolve(branch_tip, repo, commit_ref);
7560 if (err)
7561 goto done;
7562 done:
7563 free(commit_refname);
7564 free(branch_refname);
7565 free(fileindex_path);
7566 if (commit_ref)
7567 got_ref_close(commit_ref);
7568 if (branch_ref)
7569 got_ref_close(branch_ref);
7570 if (err) {
7571 if (*branch_name) {
7572 free(*branch_name);
7573 *branch_name = NULL;
7575 free(*branch_tip);
7576 *branch_tip = NULL;
7577 if (*fileindex) {
7578 got_fileindex_free(*fileindex);
7579 *fileindex = NULL;
7581 lock_worktree(worktree, LOCK_SH);
7583 return err;
7586 const struct got_error *
7587 got_worktree_merge_abort(struct got_worktree *worktree,
7588 struct got_fileindex *fileindex, struct got_repository *repo,
7589 got_worktree_checkout_cb progress_cb, void *progress_arg)
7591 const struct got_error *err, *unlockerr, *sync_err;
7592 struct got_object_id *commit_id = NULL;
7593 char *fileindex_path = NULL;
7594 struct revert_file_args rfa;
7595 struct got_object_id *tree_id = NULL;
7597 err = got_object_id_by_path(&tree_id, repo,
7598 worktree->base_commit_id, worktree->path_prefix);
7599 if (err)
7600 goto done;
7602 err = delete_merge_refs(worktree, repo);
7603 if (err)
7604 goto done;
7606 err = get_fileindex_path(&fileindex_path, worktree);
7607 if (err)
7608 goto done;
7610 rfa.worktree = worktree;
7611 rfa.fileindex = fileindex;
7612 rfa.progress_cb = progress_cb;
7613 rfa.progress_arg = progress_arg;
7614 rfa.patch_cb = NULL;
7615 rfa.patch_arg = NULL;
7616 rfa.repo = repo;
7617 rfa.unlink_added_files = 1;
7618 err = worktree_status(worktree, "", fileindex, repo,
7619 revert_file, &rfa, NULL, NULL, 1, 0);
7620 if (err)
7621 goto sync;
7623 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7624 repo, progress_cb, progress_arg, NULL, NULL);
7625 sync:
7626 sync_err = sync_fileindex(fileindex, fileindex_path);
7627 if (sync_err && err == NULL)
7628 err = sync_err;
7629 done:
7630 free(tree_id);
7631 free(commit_id);
7632 if (fileindex)
7633 got_fileindex_free(fileindex);
7634 free(fileindex_path);
7636 unlockerr = lock_worktree(worktree, LOCK_SH);
7637 if (unlockerr && err == NULL)
7638 err = unlockerr;
7639 return err;
7642 struct check_stage_ok_arg {
7643 struct got_object_id *head_commit_id;
7644 struct got_worktree *worktree;
7645 struct got_fileindex *fileindex;
7646 struct got_repository *repo;
7647 int have_changes;
7650 const struct got_error *
7651 check_stage_ok(void *arg, unsigned char status,
7652 unsigned char staged_status, const char *relpath,
7653 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7654 struct got_object_id *commit_id, int dirfd, const char *de_name)
7656 struct check_stage_ok_arg *a = arg;
7657 const struct got_error *err = NULL;
7658 struct got_fileindex_entry *ie;
7659 struct got_object_id base_commit_id;
7660 struct got_object_id *base_commit_idp = NULL;
7661 char *in_repo_path = NULL, *p;
7663 if (status == GOT_STATUS_UNVERSIONED ||
7664 status == GOT_STATUS_NO_CHANGE)
7665 return NULL;
7666 if (status == GOT_STATUS_NONEXISTENT)
7667 return got_error_set_errno(ENOENT, relpath);
7669 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7670 if (ie == NULL)
7671 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7673 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7674 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7675 relpath) == -1)
7676 return got_error_from_errno("asprintf");
7678 if (got_fileindex_entry_has_commit(ie)) {
7679 memcpy(base_commit_id.sha1, ie->commit_sha1,
7680 SHA1_DIGEST_LENGTH);
7681 base_commit_idp = &base_commit_id;
7684 if (status == GOT_STATUS_CONFLICT) {
7685 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7686 goto done;
7687 } else if (status != GOT_STATUS_ADD &&
7688 status != GOT_STATUS_MODIFY &&
7689 status != GOT_STATUS_DELETE) {
7690 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7691 goto done;
7694 a->have_changes = 1;
7696 p = in_repo_path;
7697 while (p[0] == '/')
7698 p++;
7699 err = check_out_of_date(p, status, staged_status,
7700 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7701 GOT_ERR_STAGE_OUT_OF_DATE);
7702 done:
7703 free(in_repo_path);
7704 return err;
7707 struct stage_path_arg {
7708 struct got_worktree *worktree;
7709 struct got_fileindex *fileindex;
7710 struct got_repository *repo;
7711 got_worktree_status_cb status_cb;
7712 void *status_arg;
7713 got_worktree_patch_cb patch_cb;
7714 void *patch_arg;
7715 int staged_something;
7716 int allow_bad_symlinks;
7719 static const struct got_error *
7720 stage_path(void *arg, unsigned char status,
7721 unsigned char staged_status, const char *relpath,
7722 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7723 struct got_object_id *commit_id, int dirfd, const char *de_name)
7725 struct stage_path_arg *a = arg;
7726 const struct got_error *err = NULL;
7727 struct got_fileindex_entry *ie;
7728 char *ondisk_path = NULL, *path_content = NULL;
7729 uint32_t stage;
7730 struct got_object_id *new_staged_blob_id = NULL;
7731 struct stat sb;
7733 if (status == GOT_STATUS_UNVERSIONED)
7734 return NULL;
7736 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7737 if (ie == NULL)
7738 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7740 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7741 relpath)== -1)
7742 return got_error_from_errno("asprintf");
7744 switch (status) {
7745 case GOT_STATUS_ADD:
7746 case GOT_STATUS_MODIFY:
7747 /* XXX could sb.st_mode be passed in by our caller? */
7748 if (lstat(ondisk_path, &sb) == -1) {
7749 err = got_error_from_errno2("lstat", ondisk_path);
7750 break;
7752 if (a->patch_cb) {
7753 if (status == GOT_STATUS_ADD) {
7754 int choice = GOT_PATCH_CHOICE_NONE;
7755 err = (*a->patch_cb)(&choice, a->patch_arg,
7756 status, ie->path, NULL, 1, 1);
7757 if (err)
7758 break;
7759 if (choice != GOT_PATCH_CHOICE_YES)
7760 break;
7761 } else {
7762 err = create_patched_content(&path_content, 0,
7763 staged_blob_id ? staged_blob_id : blob_id,
7764 ondisk_path, dirfd, de_name, ie->path,
7765 a->repo, a->patch_cb, a->patch_arg);
7766 if (err || path_content == NULL)
7767 break;
7770 err = got_object_blob_create(&new_staged_blob_id,
7771 path_content ? path_content : ondisk_path, a->repo);
7772 if (err)
7773 break;
7774 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7775 SHA1_DIGEST_LENGTH);
7776 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7777 stage = GOT_FILEIDX_STAGE_ADD;
7778 else
7779 stage = GOT_FILEIDX_STAGE_MODIFY;
7780 got_fileindex_entry_stage_set(ie, stage);
7781 if (S_ISLNK(sb.st_mode)) {
7782 int is_bad_symlink = 0;
7783 if (!a->allow_bad_symlinks) {
7784 char target_path[PATH_MAX];
7785 ssize_t target_len;
7786 target_len = readlink(ondisk_path, target_path,
7787 sizeof(target_path));
7788 if (target_len == -1) {
7789 err = got_error_from_errno2("readlink",
7790 ondisk_path);
7791 break;
7793 err = is_bad_symlink_target(&is_bad_symlink,
7794 target_path, target_len, ondisk_path,
7795 a->worktree->root_path);
7796 if (err)
7797 break;
7798 if (is_bad_symlink) {
7799 err = got_error_path(ondisk_path,
7800 GOT_ERR_BAD_SYMLINK);
7801 break;
7804 if (is_bad_symlink)
7805 got_fileindex_entry_staged_filetype_set(ie,
7806 GOT_FILEIDX_MODE_BAD_SYMLINK);
7807 else
7808 got_fileindex_entry_staged_filetype_set(ie,
7809 GOT_FILEIDX_MODE_SYMLINK);
7810 } else {
7811 got_fileindex_entry_staged_filetype_set(ie,
7812 GOT_FILEIDX_MODE_REGULAR_FILE);
7814 a->staged_something = 1;
7815 if (a->status_cb == NULL)
7816 break;
7817 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7818 get_staged_status(ie), relpath, blob_id,
7819 new_staged_blob_id, NULL, dirfd, de_name);
7820 break;
7821 case GOT_STATUS_DELETE:
7822 if (staged_status == GOT_STATUS_DELETE)
7823 break;
7824 if (a->patch_cb) {
7825 int choice = GOT_PATCH_CHOICE_NONE;
7826 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7827 ie->path, NULL, 1, 1);
7828 if (err)
7829 break;
7830 if (choice == GOT_PATCH_CHOICE_NO)
7831 break;
7832 if (choice != GOT_PATCH_CHOICE_YES) {
7833 err = got_error(GOT_ERR_PATCH_CHOICE);
7834 break;
7837 stage = GOT_FILEIDX_STAGE_DELETE;
7838 got_fileindex_entry_stage_set(ie, stage);
7839 a->staged_something = 1;
7840 if (a->status_cb == NULL)
7841 break;
7842 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7843 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7844 de_name);
7845 break;
7846 case GOT_STATUS_NO_CHANGE:
7847 break;
7848 case GOT_STATUS_CONFLICT:
7849 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7850 break;
7851 case GOT_STATUS_NONEXISTENT:
7852 err = got_error_set_errno(ENOENT, relpath);
7853 break;
7854 default:
7855 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7856 break;
7859 if (path_content && unlink(path_content) == -1 && err == NULL)
7860 err = got_error_from_errno2("unlink", path_content);
7861 free(path_content);
7862 free(ondisk_path);
7863 free(new_staged_blob_id);
7864 return err;
7867 const struct got_error *
7868 got_worktree_stage(struct got_worktree *worktree,
7869 struct got_pathlist_head *paths,
7870 got_worktree_status_cb status_cb, void *status_arg,
7871 got_worktree_patch_cb patch_cb, void *patch_arg,
7872 int allow_bad_symlinks, struct got_repository *repo)
7874 const struct got_error *err = NULL, *sync_err, *unlockerr;
7875 struct got_pathlist_entry *pe;
7876 struct got_fileindex *fileindex = NULL;
7877 char *fileindex_path = NULL;
7878 struct got_reference *head_ref = NULL;
7879 struct got_object_id *head_commit_id = NULL;
7880 struct check_stage_ok_arg oka;
7881 struct stage_path_arg spa;
7883 err = lock_worktree(worktree, LOCK_EX);
7884 if (err)
7885 return err;
7887 err = got_ref_open(&head_ref, repo,
7888 got_worktree_get_head_ref_name(worktree), 0);
7889 if (err)
7890 goto done;
7891 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7892 if (err)
7893 goto done;
7894 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7895 if (err)
7896 goto done;
7898 /* Check pre-conditions before staging anything. */
7899 oka.head_commit_id = head_commit_id;
7900 oka.worktree = worktree;
7901 oka.fileindex = fileindex;
7902 oka.repo = repo;
7903 oka.have_changes = 0;
7904 TAILQ_FOREACH(pe, paths, entry) {
7905 err = worktree_status(worktree, pe->path, fileindex, repo,
7906 check_stage_ok, &oka, NULL, NULL, 1, 0);
7907 if (err)
7908 goto done;
7910 if (!oka.have_changes) {
7911 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7912 goto done;
7915 spa.worktree = worktree;
7916 spa.fileindex = fileindex;
7917 spa.repo = repo;
7918 spa.patch_cb = patch_cb;
7919 spa.patch_arg = patch_arg;
7920 spa.status_cb = status_cb;
7921 spa.status_arg = status_arg;
7922 spa.staged_something = 0;
7923 spa.allow_bad_symlinks = allow_bad_symlinks;
7924 TAILQ_FOREACH(pe, paths, entry) {
7925 err = worktree_status(worktree, pe->path, fileindex, repo,
7926 stage_path, &spa, NULL, NULL, 1, 0);
7927 if (err)
7928 goto done;
7930 if (!spa.staged_something) {
7931 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7932 goto done;
7935 sync_err = sync_fileindex(fileindex, fileindex_path);
7936 if (sync_err && err == NULL)
7937 err = sync_err;
7938 done:
7939 if (head_ref)
7940 got_ref_close(head_ref);
7941 free(head_commit_id);
7942 free(fileindex_path);
7943 if (fileindex)
7944 got_fileindex_free(fileindex);
7945 unlockerr = lock_worktree(worktree, LOCK_SH);
7946 if (unlockerr && err == NULL)
7947 err = unlockerr;
7948 return err;
7951 struct unstage_path_arg {
7952 struct got_worktree *worktree;
7953 struct got_fileindex *fileindex;
7954 struct got_repository *repo;
7955 got_worktree_checkout_cb progress_cb;
7956 void *progress_arg;
7957 got_worktree_patch_cb patch_cb;
7958 void *patch_arg;
7961 static const struct got_error *
7962 create_unstaged_content(char **path_unstaged_content,
7963 char **path_new_staged_content, struct got_object_id *blob_id,
7964 struct got_object_id *staged_blob_id, const char *relpath,
7965 struct got_repository *repo,
7966 got_worktree_patch_cb patch_cb, void *patch_arg)
7968 const struct got_error *err, *free_err;
7969 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7970 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7971 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7972 struct got_diffreg_result *diffreg_result = NULL;
7973 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7974 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7976 *path_unstaged_content = NULL;
7977 *path_new_staged_content = NULL;
7979 err = got_object_id_str(&label1, blob_id);
7980 if (err)
7981 return err;
7982 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7983 if (err)
7984 goto done;
7986 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7987 if (err)
7988 goto done;
7990 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7991 if (err)
7992 goto done;
7994 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7995 if (err)
7996 goto done;
7998 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7999 if (err)
8000 goto done;
8002 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8003 if (err)
8004 goto done;
8006 err = got_diff_files(&diffreg_result, f1, label1, f2,
8007 path2, 3, 0, 1, NULL);
8008 if (err)
8009 goto done;
8011 err = got_opentemp_named(path_unstaged_content, &outfile,
8012 "got-unstaged-content");
8013 if (err)
8014 goto done;
8015 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8016 "got-new-staged-content");
8017 if (err)
8018 goto done;
8020 if (fseek(f1, 0L, SEEK_SET) == -1) {
8021 err = got_ferror(f1, GOT_ERR_IO);
8022 goto done;
8024 if (fseek(f2, 0L, SEEK_SET) == -1) {
8025 err = got_ferror(f2, GOT_ERR_IO);
8026 goto done;
8028 /* Count the number of actual changes in the diff result. */
8029 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8030 struct diff_chunk_context cc = {};
8031 diff_chunk_context_load_change(&cc, &nchunks_used,
8032 diffreg_result->result, n, 0);
8033 nchanges++;
8035 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8036 int choice;
8037 err = apply_or_reject_change(&choice, &nchunks_used,
8038 diffreg_result->result, n, relpath, f1, f2,
8039 &line_cur1, &line_cur2,
8040 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8041 if (err)
8042 goto done;
8043 if (choice == GOT_PATCH_CHOICE_YES)
8044 have_content = 1;
8045 else
8046 have_rejected_content = 1;
8047 if (choice == GOT_PATCH_CHOICE_QUIT)
8048 break;
8050 if (have_content || have_rejected_content)
8051 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8052 outfile, rejectfile);
8053 done:
8054 free(label1);
8055 if (blob)
8056 got_object_blob_close(blob);
8057 if (staged_blob)
8058 got_object_blob_close(staged_blob);
8059 free_err = got_diffreg_result_free(diffreg_result);
8060 if (free_err && err == NULL)
8061 err = free_err;
8062 if (f1 && fclose(f1) == EOF && err == NULL)
8063 err = got_error_from_errno2("fclose", path1);
8064 if (f2 && fclose(f2) == EOF && err == NULL)
8065 err = got_error_from_errno2("fclose", path2);
8066 if (outfile && fclose(outfile) == EOF && err == NULL)
8067 err = got_error_from_errno2("fclose", *path_unstaged_content);
8068 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8069 err = got_error_from_errno2("fclose", *path_new_staged_content);
8070 if (path1 && unlink(path1) == -1 && err == NULL)
8071 err = got_error_from_errno2("unlink", path1);
8072 if (path2 && unlink(path2) == -1 && err == NULL)
8073 err = got_error_from_errno2("unlink", path2);
8074 if (err || !have_content) {
8075 if (*path_unstaged_content &&
8076 unlink(*path_unstaged_content) == -1 && err == NULL)
8077 err = got_error_from_errno2("unlink",
8078 *path_unstaged_content);
8079 free(*path_unstaged_content);
8080 *path_unstaged_content = NULL;
8082 if (err || !have_content || !have_rejected_content) {
8083 if (*path_new_staged_content &&
8084 unlink(*path_new_staged_content) == -1 && err == NULL)
8085 err = got_error_from_errno2("unlink",
8086 *path_new_staged_content);
8087 free(*path_new_staged_content);
8088 *path_new_staged_content = NULL;
8090 free(path1);
8091 free(path2);
8092 return err;
8095 static const struct got_error *
8096 unstage_hunks(struct got_object_id *staged_blob_id,
8097 struct got_blob_object *blob_base,
8098 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8099 const char *ondisk_path, const char *label_orig,
8100 struct got_worktree *worktree, struct got_repository *repo,
8101 got_worktree_patch_cb patch_cb, void *patch_arg,
8102 got_worktree_checkout_cb progress_cb, void *progress_arg)
8104 const struct got_error *err = NULL;
8105 char *path_unstaged_content = NULL;
8106 char *path_new_staged_content = NULL;
8107 char *parent = NULL, *base_path = NULL;
8108 char *blob_base_path = NULL;
8109 struct got_object_id *new_staged_blob_id = NULL;
8110 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8111 struct stat sb;
8113 err = create_unstaged_content(&path_unstaged_content,
8114 &path_new_staged_content, blob_id, staged_blob_id,
8115 ie->path, repo, patch_cb, patch_arg);
8116 if (err)
8117 return err;
8119 if (path_unstaged_content == NULL)
8120 return NULL;
8122 if (path_new_staged_content) {
8123 err = got_object_blob_create(&new_staged_blob_id,
8124 path_new_staged_content, repo);
8125 if (err)
8126 goto done;
8129 f = fopen(path_unstaged_content, "r");
8130 if (f == NULL) {
8131 err = got_error_from_errno2("fopen",
8132 path_unstaged_content);
8133 goto done;
8135 if (fstat(fileno(f), &sb) == -1) {
8136 err = got_error_from_errno2("fstat", path_unstaged_content);
8137 goto done;
8139 if (got_fileindex_entry_staged_filetype_get(ie) ==
8140 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8141 char link_target[PATH_MAX];
8142 size_t r;
8143 r = fread(link_target, 1, sizeof(link_target), f);
8144 if (r == 0 && ferror(f)) {
8145 err = got_error_from_errno("fread");
8146 goto done;
8148 if (r >= sizeof(link_target)) { /* should not happen */
8149 err = got_error(GOT_ERR_NO_SPACE);
8150 goto done;
8152 link_target[r] = '\0';
8153 err = merge_symlink(worktree, blob_base,
8154 ondisk_path, ie->path, label_orig, link_target,
8155 worktree->base_commit_id, repo, progress_cb,
8156 progress_arg);
8157 } else {
8158 int local_changes_subsumed;
8160 err = got_path_dirname(&parent, ondisk_path);
8161 if (err)
8162 return err;
8164 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8165 parent) == -1) {
8166 err = got_error_from_errno("asprintf");
8167 base_path = NULL;
8168 goto done;
8171 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8172 if (err)
8173 goto done;
8174 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8175 blob_base);
8176 if (err)
8177 goto done;
8180 * In order the run a 3-way merge with a symlink we copy the symlink's
8181 * target path into a temporary file and use that file with diff3.
8183 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8184 err = dump_symlink_target_path_to_file(&f_deriv2,
8185 ondisk_path);
8186 if (err)
8187 goto done;
8188 } else {
8189 int fd;
8190 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
8191 if (fd == -1) {
8192 err = got_error_from_errno2("open", ondisk_path);
8193 goto done;
8195 f_deriv2 = fdopen(fd, "r");
8196 if (f_deriv2 == NULL) {
8197 err = got_error_from_errno2("fdopen", ondisk_path);
8198 close(fd);
8199 goto done;
8203 err = merge_file(&local_changes_subsumed, worktree,
8204 f_base, f, f_deriv2, ondisk_path, ie->path,
8205 got_fileindex_perms_to_st(ie),
8206 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8207 repo, progress_cb, progress_arg);
8209 if (err)
8210 goto done;
8212 if (new_staged_blob_id) {
8213 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8214 SHA1_DIGEST_LENGTH);
8215 } else {
8216 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8217 got_fileindex_entry_staged_filetype_set(ie, 0);
8219 done:
8220 free(new_staged_blob_id);
8221 if (path_unstaged_content &&
8222 unlink(path_unstaged_content) == -1 && err == NULL)
8223 err = got_error_from_errno2("unlink", path_unstaged_content);
8224 if (path_new_staged_content &&
8225 unlink(path_new_staged_content) == -1 && err == NULL)
8226 err = got_error_from_errno2("unlink", path_new_staged_content);
8227 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8228 err = got_error_from_errno2("unlink", blob_base_path);
8229 if (f_base && fclose(f_base) == EOF && err == NULL)
8230 err = got_error_from_errno2("fclose", path_unstaged_content);
8231 if (f && fclose(f) == EOF && err == NULL)
8232 err = got_error_from_errno2("fclose", path_unstaged_content);
8233 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8234 err = got_error_from_errno2("fclose", ondisk_path);
8235 free(path_unstaged_content);
8236 free(path_new_staged_content);
8237 free(blob_base_path);
8238 free(parent);
8239 free(base_path);
8240 return err;
8243 static const struct got_error *
8244 unstage_path(void *arg, unsigned char status,
8245 unsigned char staged_status, const char *relpath,
8246 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8247 struct got_object_id *commit_id, int dirfd, const char *de_name)
8249 const struct got_error *err = NULL;
8250 struct unstage_path_arg *a = arg;
8251 struct got_fileindex_entry *ie;
8252 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8253 char *ondisk_path = NULL;
8254 char *id_str = NULL, *label_orig = NULL;
8255 int local_changes_subsumed;
8256 struct stat sb;
8258 if (staged_status != GOT_STATUS_ADD &&
8259 staged_status != GOT_STATUS_MODIFY &&
8260 staged_status != GOT_STATUS_DELETE)
8261 return NULL;
8263 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8264 if (ie == NULL)
8265 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8267 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8268 == -1)
8269 return got_error_from_errno("asprintf");
8271 err = got_object_id_str(&id_str,
8272 commit_id ? commit_id : a->worktree->base_commit_id);
8273 if (err)
8274 goto done;
8275 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8276 id_str) == -1) {
8277 err = got_error_from_errno("asprintf");
8278 goto done;
8281 switch (staged_status) {
8282 case GOT_STATUS_MODIFY:
8283 err = got_object_open_as_blob(&blob_base, a->repo,
8284 blob_id, 8192);
8285 if (err)
8286 break;
8287 /* fall through */
8288 case GOT_STATUS_ADD:
8289 if (a->patch_cb) {
8290 if (staged_status == GOT_STATUS_ADD) {
8291 int choice = GOT_PATCH_CHOICE_NONE;
8292 err = (*a->patch_cb)(&choice, a->patch_arg,
8293 staged_status, ie->path, NULL, 1, 1);
8294 if (err)
8295 break;
8296 if (choice != GOT_PATCH_CHOICE_YES)
8297 break;
8298 } else {
8299 err = unstage_hunks(staged_blob_id,
8300 blob_base, blob_id, ie, ondisk_path,
8301 label_orig, a->worktree, a->repo,
8302 a->patch_cb, a->patch_arg,
8303 a->progress_cb, a->progress_arg);
8304 break; /* Done with this file. */
8307 err = got_object_open_as_blob(&blob_staged, a->repo,
8308 staged_blob_id, 8192);
8309 if (err)
8310 break;
8311 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8312 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8313 case GOT_FILEIDX_MODE_REGULAR_FILE:
8314 err = merge_blob(&local_changes_subsumed, a->worktree,
8315 blob_base, ondisk_path, relpath,
8316 got_fileindex_perms_to_st(ie), label_orig,
8317 blob_staged, commit_id ? commit_id :
8318 a->worktree->base_commit_id, a->repo,
8319 a->progress_cb, a->progress_arg);
8320 break;
8321 case GOT_FILEIDX_MODE_SYMLINK:
8322 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8323 char *staged_target;
8324 err = got_object_blob_read_to_str(
8325 &staged_target, blob_staged);
8326 if (err)
8327 goto done;
8328 err = merge_symlink(a->worktree, blob_base,
8329 ondisk_path, relpath, label_orig,
8330 staged_target, commit_id ? commit_id :
8331 a->worktree->base_commit_id,
8332 a->repo, a->progress_cb, a->progress_arg);
8333 free(staged_target);
8334 } else {
8335 err = merge_blob(&local_changes_subsumed,
8336 a->worktree, blob_base, ondisk_path,
8337 relpath, got_fileindex_perms_to_st(ie),
8338 label_orig, blob_staged,
8339 commit_id ? commit_id :
8340 a->worktree->base_commit_id, a->repo,
8341 a->progress_cb, a->progress_arg);
8343 break;
8344 default:
8345 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8346 break;
8348 if (err == NULL) {
8349 got_fileindex_entry_stage_set(ie,
8350 GOT_FILEIDX_STAGE_NONE);
8351 got_fileindex_entry_staged_filetype_set(ie, 0);
8353 break;
8354 case GOT_STATUS_DELETE:
8355 if (a->patch_cb) {
8356 int choice = GOT_PATCH_CHOICE_NONE;
8357 err = (*a->patch_cb)(&choice, a->patch_arg,
8358 staged_status, ie->path, NULL, 1, 1);
8359 if (err)
8360 break;
8361 if (choice == GOT_PATCH_CHOICE_NO)
8362 break;
8363 if (choice != GOT_PATCH_CHOICE_YES) {
8364 err = got_error(GOT_ERR_PATCH_CHOICE);
8365 break;
8368 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8369 got_fileindex_entry_staged_filetype_set(ie, 0);
8370 err = get_file_status(&status, &sb, ie, ondisk_path,
8371 dirfd, de_name, a->repo);
8372 if (err)
8373 break;
8374 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8375 break;
8377 done:
8378 free(ondisk_path);
8379 if (blob_base)
8380 got_object_blob_close(blob_base);
8381 if (blob_staged)
8382 got_object_blob_close(blob_staged);
8383 free(id_str);
8384 free(label_orig);
8385 return err;
8388 const struct got_error *
8389 got_worktree_unstage(struct got_worktree *worktree,
8390 struct got_pathlist_head *paths,
8391 got_worktree_checkout_cb progress_cb, void *progress_arg,
8392 got_worktree_patch_cb patch_cb, void *patch_arg,
8393 struct got_repository *repo)
8395 const struct got_error *err = NULL, *sync_err, *unlockerr;
8396 struct got_pathlist_entry *pe;
8397 struct got_fileindex *fileindex = NULL;
8398 char *fileindex_path = NULL;
8399 struct unstage_path_arg upa;
8401 err = lock_worktree(worktree, LOCK_EX);
8402 if (err)
8403 return err;
8405 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8406 if (err)
8407 goto done;
8409 upa.worktree = worktree;
8410 upa.fileindex = fileindex;
8411 upa.repo = repo;
8412 upa.progress_cb = progress_cb;
8413 upa.progress_arg = progress_arg;
8414 upa.patch_cb = patch_cb;
8415 upa.patch_arg = patch_arg;
8416 TAILQ_FOREACH(pe, paths, entry) {
8417 err = worktree_status(worktree, pe->path, fileindex, repo,
8418 unstage_path, &upa, NULL, NULL, 1, 0);
8419 if (err)
8420 goto done;
8423 sync_err = sync_fileindex(fileindex, fileindex_path);
8424 if (sync_err && err == NULL)
8425 err = sync_err;
8426 done:
8427 free(fileindex_path);
8428 if (fileindex)
8429 got_fileindex_free(fileindex);
8430 unlockerr = lock_worktree(worktree, LOCK_SH);
8431 if (unlockerr && err == NULL)
8432 err = unlockerr;
8433 return err;
8436 struct report_file_info_arg {
8437 struct got_worktree *worktree;
8438 got_worktree_path_info_cb info_cb;
8439 void *info_arg;
8440 struct got_pathlist_head *paths;
8441 got_cancel_cb cancel_cb;
8442 void *cancel_arg;
8445 static const struct got_error *
8446 report_file_info(void *arg, struct got_fileindex_entry *ie)
8448 struct report_file_info_arg *a = arg;
8449 struct got_pathlist_entry *pe;
8450 struct got_object_id blob_id, staged_blob_id, commit_id;
8451 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8452 struct got_object_id *commit_idp = NULL;
8453 int stage;
8455 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8456 return got_error(GOT_ERR_CANCELLED);
8458 TAILQ_FOREACH(pe, a->paths, entry) {
8459 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8460 got_path_is_child(ie->path, pe->path, pe->path_len))
8461 break;
8463 if (pe == NULL) /* not found */
8464 return NULL;
8466 if (got_fileindex_entry_has_blob(ie)) {
8467 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8468 blob_idp = &blob_id;
8470 stage = got_fileindex_entry_stage_get(ie);
8471 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8472 stage == GOT_FILEIDX_STAGE_ADD) {
8473 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8474 SHA1_DIGEST_LENGTH);
8475 staged_blob_idp = &staged_blob_id;
8478 if (got_fileindex_entry_has_commit(ie)) {
8479 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8480 commit_idp = &commit_id;
8483 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8484 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8487 const struct got_error *
8488 got_worktree_path_info(struct got_worktree *worktree,
8489 struct got_pathlist_head *paths,
8490 got_worktree_path_info_cb info_cb, void *info_arg,
8491 got_cancel_cb cancel_cb, void *cancel_arg)
8494 const struct got_error *err = NULL, *unlockerr;
8495 struct got_fileindex *fileindex = NULL;
8496 char *fileindex_path = NULL;
8497 struct report_file_info_arg arg;
8499 err = lock_worktree(worktree, LOCK_SH);
8500 if (err)
8501 return err;
8503 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8504 if (err)
8505 goto done;
8507 arg.worktree = worktree;
8508 arg.info_cb = info_cb;
8509 arg.info_arg = info_arg;
8510 arg.paths = paths;
8511 arg.cancel_cb = cancel_cb;
8512 arg.cancel_arg = cancel_arg;
8513 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8514 &arg);
8515 done:
8516 free(fileindex_path);
8517 if (fileindex)
8518 got_fileindex_free(fileindex);
8519 unlockerr = lock_worktree(worktree, LOCK_UN);
8520 if (unlockerr && err == NULL)
8521 err = unlockerr;
8522 return err;