checkout: introduce git_checkout_index()
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
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;
}