diff options
author | Linus Torvalds <torvalds@osdl.org> | 2006-05-30 09:45:45 -0700 |
---|---|---|
committer | Junio C Hamano <junkio@cox.net> | 2006-05-30 23:03:01 -0700 |
commit | 4c068a983150b740c3fcf6a33f342ac923abd3f4 (patch) | |
tree | a8f96b40f82e753b11b9f4bf3347bb81b222ad6a /builtin-read-tree.c | |
parent | 47df096f254ccac5a81da615b3fd25f3cf466bbf (diff) | |
download | git-4c068a983150b740c3fcf6a33f342ac923abd3f4.tar.gz |
tree_entry(): new tree-walking helper function
This adds a "tree_entry()" function that combines the common operation of
doing a "tree_entry_extract()" + "update_tree_entry()".
It also has a simplified calling convention, designed for simple loops
that traverse over a whole tree: the arguments are pointers to the tree
descriptor and a name_entry structure to fill in, and it returns a boolean
"true" if there was an entry left to be gotten in the tree.
This allows tree traversal with
struct tree_desc desc;
struct name_entry entry;
desc.buf = tree->buffer;
desc.size = tree->size;
while (tree_entry(&desc, &entry) {
... use "entry.{path, sha1, mode, pathlen}" ...
}
which is not only shorter than writing it out in full, it's hopefully less
error prone too.
[ It's actually a tad faster too - we don't need to recalculate the entry
pathlength in both extract and update, but need to do it only once.
Also, some callers can avoid doing a "strlen()" on the result, since
it's returned as part of the name_entry structure.
However, by now we're talking just 1% speedup on "git-rev-list --objects
--all", and we're definitely at the point where tree walking is no
longer the issue any more. ]
NOTE! Not everybody wants to use this new helper function, since some of
the tree walkers very much on purpose do the descriptor update separately
from the entry extraction. So the "extract + update" sequence still
remains as the core sequence, this is just a simplified interface.
We should probably add a silly two-line inline helper function for
initializing the descriptor from the "struct tree" too, just to cut down
on the noise from that common "desc" initializer.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
Diffstat (limited to 'builtin-read-tree.c')
-rw-r--r-- | builtin-read-tree.c | 36 |
1 files changed, 13 insertions, 23 deletions
diff --git a/builtin-read-tree.c b/builtin-read-tree.c index 00cdb5a6d9..10afd46968 100644 --- a/builtin-read-tree.c +++ b/builtin-read-tree.c @@ -53,28 +53,23 @@ typedef int (*merge_fn_t)(struct cache_entry **src); static struct tree_entry_list *create_tree_entry_list(struct tree *tree) { struct tree_desc desc; + struct name_entry one; struct tree_entry_list *ret = NULL; struct tree_entry_list **list_p = &ret; desc.buf = tree->buffer; desc.size = tree->size; - while (desc.size) { - unsigned mode; - const char *path; - const unsigned char *sha1; + while (tree_entry(&desc, &one)) { struct tree_entry_list *entry; - sha1 = tree_entry_extract(&desc, &path, &mode); - update_tree_entry(&desc); - entry = xmalloc(sizeof(struct tree_entry_list)); - entry->name = path; - entry->sha1 = sha1; - entry->mode = mode; - entry->directory = S_ISDIR(mode) != 0; - entry->executable = (mode & S_IXUSR) != 0; - entry->symlink = S_ISLNK(mode) != 0; + entry->name = one.path; + entry->sha1 = one.sha1; + entry->mode = one.mode; + entry->directory = S_ISDIR(one.mode) != 0; + entry->executable = (one.mode & S_IXUSR) != 0; + entry->symlink = S_ISLNK(one.mode) != 0; entry->next = NULL; *list_p = entry; @@ -820,27 +815,22 @@ static int read_cache_unmerged(void) static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree) { struct tree_desc desc; + struct name_entry entry; int cnt; memcpy(it->sha1, tree->object.sha1, 20); desc.buf = tree->buffer; desc.size = tree->size; cnt = 0; - while (desc.size) { - unsigned mode; - const char *name; - const unsigned char *sha1; - - sha1 = tree_entry_extract(&desc, &name, &mode); - update_tree_entry(&desc); - if (!S_ISDIR(mode)) + while (tree_entry(&desc, &entry)) { + if (!S_ISDIR(entry.mode)) cnt++; else { struct cache_tree_sub *sub; - struct tree *subtree = lookup_tree(sha1); + struct tree *subtree = lookup_tree(entry.sha1); if (!subtree->object.parsed) parse_tree(subtree); - sub = cache_tree_sub(it, name); + sub = cache_tree_sub(it, entry.path); sub->cache_tree = cache_tree(); prime_cache_tree_rec(sub->cache_tree, subtree); cnt += sub->cache_tree->entry_count; |