Commit e93af304112735f02bfeb0833b58ed8230de2371

nulltoken 2012-08-24T10:40:17

checkout: introduce git_checkout_index()

diff --git a/include/git2/checkout.h b/include/git2/checkout.h
index 21b68e3..3217ac9 100644
--- a/include/git2/checkout.h
+++ b/include/git2/checkout.h
@@ -71,6 +71,20 @@ GIT_EXTERN(int) git_checkout_reference(
 	git_indexer_stats *stats);
 
 /**
+ * Updates files in the working tree to match the content of the index.
+ *
+ * @param repo repository to check out (must be non-bare)
+ * @param opts specifies checkout options (may be NULL)
+ * @param stats structure through which progress information is reported
+ * @return 0 on success, GIT_ERROR otherwise (use giterr_last for information
+ * about the error)
+ */
+GIT_EXTERN(int) git_checkout_index(
+	git_repository *repo,
+	git_checkout_opts *opts,
+	git_indexer_stats *stats);
+
+/**
  * Updates files in the index and working tree to match the content of the
  * tree pointed at by the treeish.
  *
diff --git a/src/checkout.c b/src/checkout.c
index 663a362..6e34e50 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -220,14 +220,12 @@ static void normalize_options(git_checkout_opts *normalized, git_checkout_opts *
 		normalized->file_open_flags = O_CREAT | O_TRUNC | O_WRONLY;
 }
 
-int git_checkout_tree(
+int git_checkout_index(
 	git_repository *repo,
-	git_object *treeish,
 	git_checkout_opts *opts,
 	git_indexer_stats *stats)
 {
 	git_index *index = NULL;
-	git_tree *tree = NULL;
 	git_diff_list *diff = NULL;
 	git_indexer_stats dummy_stats;
 
@@ -239,25 +237,11 @@ int git_checkout_tree(
 
 	int error;
 
-	assert(repo && treeish);
+	assert(repo);
 
 	if ((git_repository__ensure_not_bare(repo, "checkout")) < 0)
 		return GIT_EBAREREPO;
 
-	if (git_object_peel((git_object **)&tree, treeish, GIT_OBJ_TREE) < 0) {
-		giterr_set(GITERR_INVALID, "Provided treeish cannot be peeled into a tree.");
-		return GIT_ERROR;
-	}
-
-	if ((error = git_repository_index(&index, repo)) < 0)
-		goto cleanup;
-
-	if ((error = git_index_read_tree(index, tree, NULL)) < 0)
-		goto cleanup;
-
-	if ((error = git_index_write(index)) < 0)
-		goto cleanup;
-
 	diff_opts.flags = GIT_DIFF_INCLUDE_UNTRACKED;
 
 	if (opts && opts->paths) {
@@ -277,6 +261,10 @@ int git_checkout_tree(
 		stats = &dummy_stats;
 
 	stats->processed = 0;
+
+	if ((git_repository_index(&index, repo)) < 0)
+		goto cleanup;
+
 	stats->total = git_index_entrycount(index);
 
 	memset(&data, 0, sizeof(data));
@@ -293,10 +281,44 @@ int git_checkout_tree(
 	error = git_diff_foreach(diff, &data, checkout_diff_fn, NULL, NULL);
 
 cleanup:
+	git_index_free(index);
 	git_diff_list_free(diff);
+	git_buf_free(&workdir);
+	return error;
+}
+
+int git_checkout_tree(
+	git_repository *repo,
+	git_object *treeish,
+	git_checkout_opts *opts,
+	git_indexer_stats *stats)
+{
+	git_index *index = NULL;
+	git_tree *tree = NULL;
+
+	int error;
+
+	assert(repo && treeish);
+
+	if (git_object_peel((git_object **)&tree, treeish, GIT_OBJ_TREE) < 0) {
+		giterr_set(GITERR_INVALID, "Provided treeish cannot be peeled into a tree.");
+		return GIT_ERROR;
+	}
+
+	if ((error = git_repository_index(&index, repo)) < 0)
+		goto cleanup;
+
+	if ((error = git_index_read_tree(index, tree, NULL)) < 0)
+		goto cleanup;
+
+	if ((error = git_index_write(index)) < 0)
+		goto cleanup;
+
+	error = git_checkout_index(repo, opts, stats);
+
+cleanup:
 	git_index_free(index);
 	git_tree_free(tree);
-	git_buf_free(&workdir);
 	return error;
 }