Commit f1587b97 by Ben Straub

Checkout: use git_index_read_tree_with_stats.

New variant of git_index_read_tree that fills in
the 'total' field of a git_indexer_stats struct
as it's walking the tree.
parent e0681f6d
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#define INCLUDE_git_index_h__ #define INCLUDE_git_index_h__
#include "common.h" #include "common.h"
#include "indexer.h"
#include "types.h" #include "types.h"
#include "oid.h" #include "oid.h"
...@@ -345,6 +346,20 @@ GIT_EXTERN(int) git_index_entry_stage(const git_index_entry *entry); ...@@ -345,6 +346,20 @@ GIT_EXTERN(int) git_index_entry_stage(const git_index_entry *entry);
*/ */
GIT_EXTERN(int) git_index_read_tree(git_index *index, git_tree *tree); GIT_EXTERN(int) git_index_read_tree(git_index *index, git_tree *tree);
/**
* Read a tree into the index file with stats
*
* The current index contents will be replaced by the specified tree. The total
* node count is collected in stats.
*
* @param index an existing index object
* @param tree tree to read
* @param stats structure that receives the total node count
* @return 0 or an error code
*/
GIT_EXTERN(int) git_index_read_tree_with_stats(git_index *index, git_tree *tree, git_indexer_stats *stats);
/** @} */ /** @} */
GIT_END_DECL GIT_END_DECL
#endif #endif
...@@ -191,8 +191,7 @@ int git_checkout_head(git_repository *repo, git_checkout_opts *opts, git_indexer ...@@ -191,8 +191,7 @@ int git_checkout_head(git_repository *repo, git_checkout_opts *opts, git_indexer
if (!git_repository_head_tree(&tree, repo)) { if (!git_repository_head_tree(&tree, repo)) {
git_index *idx; git_index *idx;
if (!(retcode = git_repository_index(&idx, repo))) { if (!(retcode = git_repository_index(&idx, repo))) {
/* TODO: Make git_index_read_tree fill in stats->total */ if (!(retcode = git_index_read_tree_with_stats(idx, tree, stats))) {
if (!(retcode = git_index_read_tree(idx, tree))) {
retcode = git_tree_walk(tree, checkout_walker, GIT_TREEWALK_POST, &payload); retcode = git_tree_walk(tree, checkout_walker, GIT_TREEWALK_POST, &payload);
} }
git_index_free(idx); git_index_free(idx);
......
...@@ -985,12 +985,19 @@ int git_index_entry_stage(const git_index_entry *entry) ...@@ -985,12 +985,19 @@ int git_index_entry_stage(const git_index_entry *entry)
return (entry->flags & GIT_IDXENTRY_STAGEMASK) >> GIT_IDXENTRY_STAGESHIFT; return (entry->flags & GIT_IDXENTRY_STAGEMASK) >> GIT_IDXENTRY_STAGESHIFT;
} }
typedef struct read_tree_data {
git_index *index;
git_indexer_stats *stats;
} read_tree_data;
static int read_tree_cb(const char *root, const git_tree_entry *tentry, void *data) static int read_tree_cb(const char *root, const git_tree_entry *tentry, void *data)
{ {
git_index *index = data; read_tree_data *rtd = data;
git_index_entry *entry = NULL; git_index_entry *entry = NULL;
git_buf path = GIT_BUF_INIT; git_buf path = GIT_BUF_INIT;
rtd->stats->total++;
if (git_tree_entry__is_tree(tentry)) if (git_tree_entry__is_tree(tentry))
return 0; return 0;
...@@ -1005,7 +1012,7 @@ static int read_tree_cb(const char *root, const git_tree_entry *tentry, void *da ...@@ -1005,7 +1012,7 @@ static int read_tree_cb(const char *root, const git_tree_entry *tentry, void *da
entry->path = git_buf_detach(&path); entry->path = git_buf_detach(&path);
git_buf_free(&path); git_buf_free(&path);
if (index_insert(index, entry, 0) < 0) { if (index_insert(rtd->index, entry, 0) < 0) {
index_entry_free(entry); index_entry_free(entry);
return -1; return -1;
} }
...@@ -1013,9 +1020,21 @@ static int read_tree_cb(const char *root, const git_tree_entry *tentry, void *da ...@@ -1013,9 +1020,21 @@ static int read_tree_cb(const char *root, const git_tree_entry *tentry, void *da
return 0; return 0;
} }
int git_index_read_tree(git_index *index, git_tree *tree) int git_index_read_tree_with_stats(git_index *index, git_tree *tree, git_indexer_stats *stats)
{ {
git_indexer_stats dummy_stats;
read_tree_data rtd = {index, NULL};
if (!stats) stats = &dummy_stats;
stats->total = 0;
rtd.stats = stats;
git_index_clear(index); git_index_clear(index);
return git_tree_walk(tree, read_tree_cb, GIT_TREEWALK_POST, index); return git_tree_walk(tree, read_tree_cb, GIT_TREEWALK_POST, &rtd);
}
int git_index_read_tree(git_index *index, git_tree *tree)
{
return git_index_read_tree_with_stats(index, tree, NULL);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment