Commit 07ed472d9d0779031a6bd3467c3f59d556cae175

Stefan Sperling 2019-07-12T14:37:39

move core functionality of got_worktree_checkout_files() to a helper

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;