move core functionality of got_worktree_checkout_files() to a helper
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
diff --git a/lib/worktree.c b/lib/worktree.c
index a0eaf35..50dd2a7 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -1634,6 +1634,57 @@ done:
return err;
}
+static const struct got_error *
+checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
+ const char *relpath, struct got_object_id *tree_id, const char *entry_name,
+ struct got_repository *repo, got_worktree_checkout_cb progress_cb,
+ void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
+{
+ const struct got_error *err = NULL;
+ struct got_commit_object *commit = NULL;
+ struct got_tree_object *tree = NULL;
+ struct got_fileindex_diff_tree_cb diff_cb;
+ struct diff_cb_arg arg;
+
+ err = ref_base_commit(worktree, repo);
+ if (err)
+ goto done;
+
+ err = got_object_open_as_commit(&commit, repo,
+ worktree->base_commit_id);
+ if (err)
+ goto done;
+
+ err = got_object_open_as_tree(&tree, repo, tree_id);
+ if (err)
+ goto done;
+
+ if (entry_name &&
+ got_object_tree_find_entry(tree, entry_name) == NULL) {
+ err = got_error(GOT_ERR_NO_TREE_ENTRY);
+ goto done;
+ }
+
+ diff_cb.diff_old_new = diff_old_new;
+ diff_cb.diff_old = diff_old;
+ diff_cb.diff_new = diff_new;
+ arg.fileindex = fileindex;
+ arg.worktree = worktree;
+ arg.repo = repo;
+ arg.progress_cb = progress_cb;
+ arg.progress_arg = progress_arg;
+ arg.cancel_cb = cancel_cb;
+ arg.cancel_arg = cancel_arg;
+ err = got_fileindex_diff_tree(fileindex, tree, relpath,
+ entry_name, repo, &diff_cb, &arg);
+done:
+ if (tree)
+ got_object_tree_close(tree);
+ if (commit)
+ got_object_commit_close(commit);
+ return err;
+}
+
const struct got_error *
got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
struct got_repository *repo, got_worktree_checkout_cb progress_cb,
@@ -1645,8 +1696,6 @@ got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
struct got_tree_object *tree = NULL;
struct got_fileindex *fileindex = NULL;
char *fileindex_path = NULL;
- struct got_fileindex_diff_tree_cb diff_cb;
- struct diff_cb_arg arg;
char *relpath = NULL, *entry_name = NULL;
struct bump_base_commit_id_arg bbc_arg;
int entry_type;
@@ -1677,37 +1726,8 @@ got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
if (err)
goto done;
- err = ref_base_commit(worktree, repo);
- if (err)
- goto done;
-
- err = got_object_open_as_commit(&commit, repo,
- worktree->base_commit_id);
- if (err)
- goto done;
-
- err = got_object_open_as_tree(&tree, repo, tree_id);
- if (err)
- goto done;
-
- if (entry_name &&
- got_object_tree_find_entry(tree, entry_name) == NULL) {
- err = got_error(GOT_ERR_NO_TREE_ENTRY);
- goto done;
- }
-
- diff_cb.diff_old_new = diff_old_new;
- diff_cb.diff_old = diff_old;
- diff_cb.diff_new = diff_new;
- arg.fileindex = fileindex;
- arg.worktree = worktree;
- arg.repo = repo;
- arg.progress_cb = progress_cb;
- arg.progress_arg = progress_arg;
- arg.cancel_cb = cancel_cb;
- arg.cancel_arg = cancel_arg;
- err = got_fileindex_diff_tree(fileindex, tree, relpath,
- entry_name, repo, &diff_cb, &arg);
+ err = checkout_files(worktree, fileindex, relpath, tree_id, entry_name,
+ repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
if (err)
goto sync;