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>
struct tree_desc *tree,
const char *tree_name, const char *base)
{
struct tree_desc *tree,
const char *tree_name, const char *base)
{
- const char *path;
- const unsigned char *sha1;
+ struct name_entry entry;
char *down;
char *path_buf = xmalloc(PATH_MAX + strlen(tree_name) + 100);
char *down;
char *path_buf = xmalloc(PATH_MAX + strlen(tree_name) + 100);
}
len = strlen(path_buf);
}
len = strlen(path_buf);
- while (tree->size) {
- int pathlen;
- sha1 = tree_entry_extract(tree, &path, &mode);
- pathlen = strlen(path);
- strcpy(path_buf + len, path);
+ while (tree_entry(tree, &entry)) {
+ strcpy(path_buf + len, entry.path);
+ if (S_ISDIR(entry.mode))
/* Match "abc/" against pathspec to
* decide if we want to descend into "abc"
* directory.
*/
/* Match "abc/" against pathspec to
* decide if we want to descend into "abc"
* directory.
*/
- strcpy(path_buf + len + pathlen, "/");
+ strcpy(path_buf + len + entry.pathlen, "/");
if (!pathspec_matches(paths, down))
;
if (!pathspec_matches(paths, down))
;
- else if (S_ISREG(mode))
- hit |= grep_sha1(opt, sha1, path_buf);
- else if (S_ISDIR(mode)) {
+ else if (S_ISREG(entry.mode))
+ hit |= grep_sha1(opt, entry.sha1, path_buf);
+ else if (S_ISDIR(entry.mode)) {
char type[20];
struct tree_desc sub;
void *data;
char type[20];
struct tree_desc sub;
void *data;
- data = read_sha1_file(sha1, type, &sub.size);
+ data = read_sha1_file(entry.sha1, type, &sub.size);
if (!data)
die("unable to read tree (%s)",
if (!data)
die("unable to read tree (%s)",
+ sha1_to_hex(entry.sha1));
sub.buf = data;
hit |= grep_tree(opt, paths, &sub, tree_name, down);
free(data);
}
sub.buf = data;
hit |= grep_tree(opt, paths, &sub, tree_name, down);
free(data);
}
- update_tree_entry(tree);
static struct tree_entry_list *create_tree_entry_list(struct tree *tree)
{
struct tree_desc desc;
static struct tree_entry_list *create_tree_entry_list(struct tree *tree)
{
struct tree_desc desc;
struct tree_entry_list *ret = NULL;
struct tree_entry_list **list_p = &ret;
desc.buf = tree->buffer;
desc.size = tree->size;
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;
struct tree_entry_list *entry;
- sha1 = tree_entry_extract(&desc, &path, &mode);
- update_tree_entry(&desc);
-
entry = xmalloc(sizeof(struct tree_entry_list));
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;
entry->next = NULL;
*list_p = entry;
static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
{
struct tree_desc desc;
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;
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;
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);
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;
sub->cache_tree = cache_tree();
prime_cache_tree_rec(sub->cache_tree, subtree);
cnt += sub->cache_tree->entry_count;
{
struct object *obj = &tree->object;
struct tree_desc desc;
{
struct object *obj = &tree->object;
struct tree_desc desc;
+ struct name_entry entry;
struct name_path me;
if (!revs.tree_objects)
struct name_path me;
if (!revs.tree_objects)
desc.buf = tree->buffer;
desc.size = tree->size;
desc.buf = tree->buffer;
desc.size = tree->size;
- 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))
- p = process_tree(lookup_tree(sha1), p, &me, name);
+ while (tree_entry(&desc, &entry)) {
+ if (S_ISDIR(entry.mode))
+ p = process_tree(lookup_tree(entry.sha1), p, &me, name);
- p = process_blob(lookup_blob(sha1), p, &me, name);
+ p = process_blob(lookup_blob(entry.sha1), p, &me, name);
}
free(tree->buffer);
tree->buffer = NULL;
}
free(tree->buffer);
tree->buffer = NULL;
static void traverse_tree(struct tree_desc *tree, struct strbuf *path)
{
int pathlen = path->len;
static void traverse_tree(struct tree_desc *tree, struct strbuf *path)
{
int pathlen = path->len;
+ struct name_entry entry;
- while (tree->size) {
- const char *name;
- const unsigned char *sha1;
- unsigned mode;
+ while (tree_entry(tree, &entry)) {
void *eltbuf;
char elttype[20];
unsigned long eltsize;
void *eltbuf;
char elttype[20];
unsigned long eltsize;
- sha1 = tree_entry_extract(tree, &name, &mode);
- update_tree_entry(tree);
-
- eltbuf = read_sha1_file(sha1, elttype, &eltsize);
+ eltbuf = read_sha1_file(entry.sha1, elttype, &eltsize);
- die("cannot read %s", sha1_to_hex(sha1));
+ die("cannot read %s", sha1_to_hex(entry.sha1));
- strbuf_append_string(path, name);
- if (S_ISDIR(mode))
+ strbuf_append_string(path, entry.path);
+ if (S_ISDIR(entry.mode))
strbuf_append_string(path, "/");
strbuf_append_string(path, "/");
- write_entry(sha1, path, mode, eltbuf, eltsize);
+ write_entry(entry.sha1, path, entry.mode, eltbuf, eltsize);
+ if (S_ISDIR(entry.mode)) {
struct tree_desc subtree;
subtree.buf = eltbuf;
subtree.size = eltsize;
struct tree_desc subtree;
subtree.buf = eltbuf;
subtree.size = eltsize;
static int process_tree(struct tree *tree)
{
struct tree_desc desc;
static int process_tree(struct tree *tree)
{
struct tree_desc desc;
+ struct name_entry entry;
if (parse_tree(tree))
return -1;
desc.buf = tree->buffer;
desc.size = tree->size;
if (parse_tree(tree))
return -1;
desc.buf = tree->buffer;
desc.size = tree->size;
- 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)) {
- struct tree *tree = lookup_tree(sha1);
+ while (tree_entry(&desc, &entry)) {
+ if (S_ISDIR(entry.mode)) {
+ struct tree *tree = lookup_tree(entry.sha1);
process_tree(tree);
} else {
process_tree(tree);
} else {
- struct blob *blob = lookup_blob(sha1);
+ struct blob *blob = lookup_blob(entry.sha1);
process(&blob->object);
}
}
process(&blob->object);
}
}
{
struct object *obj = &tree->object;
struct tree_desc desc;
{
struct object *obj = &tree->object;
struct tree_desc desc;
+ struct name_entry entry;
struct name_path me;
obj->flags |= LOCAL;
struct name_path me;
obj->flags |= LOCAL;
desc.buf = tree->buffer;
desc.size = tree->size;
desc.buf = tree->buffer;
desc.size = tree->size;
- 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))
- p = process_tree(lookup_tree(sha1), p, &me, name);
+ while (tree_entry(&desc, &entry)) {
+ if (S_ISDIR(entry.mode))
+ p = process_tree(lookup_tree(entry.sha1), p, &me, name);
- p = process_blob(lookup_blob(sha1), p, &me, name);
+ p = process_blob(lookup_blob(entry.sha1), p, &me, name);
}
free(tree->buffer);
tree->buffer = NULL;
}
free(tree->buffer);
tree->buffer = NULL;
const char *name,
int cmplen)
{
const char *name,
int cmplen)
{
- while (tree->size) {
- const unsigned char *sha1;
- const char *entry_name;
- int entry_len;
- unsigned mode;
+ struct name_entry entry;
+
+ while (tree_entry(tree,&entry)) {
unsigned long size;
char type[20];
unsigned long size;
char type[20];
- sha1 = tree_entry_extract(tree, &entry_name, &mode);
- update_tree_entry(tree);
- entry_len = strlen(entry_name);
- if (entry_len != cmplen ||
- memcmp(entry_name, name, cmplen) ||
- !has_sha1_file(sha1) ||
- sha1_object_info(sha1, type, &size))
+ if (entry.pathlen != cmplen ||
+ memcmp(entry.path, name, cmplen) ||
+ !has_sha1_file(entry.sha1) ||
+ sha1_object_info(entry.sha1, type, &size))
continue;
if (name[cmplen] != '/') {
unsigned hash = name_hash(up, name);
continue;
if (name[cmplen] != '/') {
unsigned hash = name_hash(up, name);
- add_object_entry(sha1, hash, 1);
+ add_object_entry(entry.sha1, hash, 1);
return;
}
if (!strcmp(type, tree_type)) {
return;
}
if (!strcmp(type, tree_type)) {
const char *down = name+cmplen+1;
int downlen = name_cmp_len(down);
const char *down = name+cmplen+1;
int downlen = name_cmp_len(down);
- tree = pbase_tree_get(sha1);
+ tree = pbase_tree_get(entry.sha1);
if (!tree)
return;
sub.buf = tree->tree_data;
sub.size = tree->tree_size;
me.up = up;
if (!tree)
return;
sub.buf = tree->tree_data;
sub.size = tree->tree_size;
me.up = up;
- me.elem = entry_name;
- me.len = entry_len;
+ me.elem = entry.path;
+ me.len = entry.pathlen;
add_pbase_object(&sub, &me, down, downlen);
pbase_tree_put(tree);
}
add_pbase_object(&sub, &me, down, downlen);
pbase_tree_put(tree);
}
void mark_tree_uninteresting(struct tree *tree)
{
struct tree_desc desc;
void mark_tree_uninteresting(struct tree *tree)
{
struct tree_desc desc;
+ struct name_entry entry;
struct object *obj = &tree->object;
if (obj->flags & UNINTERESTING)
struct object *obj = &tree->object;
if (obj->flags & UNINTERESTING)
desc.buf = tree->buffer;
desc.size = tree->size;
desc.buf = tree->buffer;
desc.size = tree->size;
- 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))
- mark_tree_uninteresting(lookup_tree(sha1));
+ while (tree_entry(&desc, &entry)) {
+ if (S_ISDIR(entry.mode))
+ mark_tree_uninteresting(lookup_tree(entry.sha1));
- mark_blob_uninteresting(lookup_blob(sha1));
+ mark_blob_uninteresting(lookup_blob(entry.sha1));
void update_tree_entry(struct tree_desc *desc)
{
void update_tree_entry(struct tree_desc *desc)
{
+ const void *buf = desc->buf;
unsigned long size = desc->size;
int len = strlen(buf) + 1 + 20;
unsigned long size = desc->size;
int len = strlen(buf) + 1 + 20;
const unsigned char *tree_entry_extract(struct tree_desc *desc, const char **pathp, unsigned int *modep)
{
const unsigned char *tree_entry_extract(struct tree_desc *desc, const char **pathp, unsigned int *modep)
{
- void *tree = desc->buf;
+ const void *tree = desc->buf;
unsigned long size = desc->size;
int len = strlen(tree)+1;
const unsigned char *sha1 = tree + len;
unsigned long size = desc->size;
int len = strlen(tree)+1;
const unsigned char *sha1 = tree + len;
+int tree_entry(struct tree_desc *desc, struct name_entry *entry)
+{
+ const void *tree = desc->buf, *path;
+ unsigned long len, size = desc->size;
+
+ if (!size)
+ return 0;
+
+ path = get_mode(tree, &entry->mode);
+ if (!path)
+ die("corrupt tree file");
+
+ entry->path = path;
+ len = strlen(path);
+ entry->pathlen = len;
+
+ path += len + 1;
+ entry->sha1 = path;
+
+ path += 20;
+ len = path - tree;
+ if (len > size)
+ die("corrupt tree file");
+
+ desc->buf = path;
+ desc->size = size - len;
+ return 1;
+}
+
void traverse_trees(int n, struct tree_desc *t, const char *base, traverse_callback_t callback)
{
struct name_entry *entry = xmalloc(n*sizeof(*entry));
void traverse_trees(int n, struct tree_desc *t, const char *base, traverse_callback_t callback)
{
struct name_entry *entry = xmalloc(n*sizeof(*entry));
#define TREE_WALK_H
struct tree_desc {
#define TREE_WALK_H
struct tree_desc {
void update_tree_entry(struct tree_desc *);
const unsigned char *tree_entry_extract(struct tree_desc *, const char **, unsigned int *);
void update_tree_entry(struct tree_desc *);
const unsigned char *tree_entry_extract(struct tree_desc *, const char **, unsigned int *);
+/* Helper function that does both of the above and returns true for success */
+int tree_entry(struct tree_desc *, struct name_entry *);
+
void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1);
typedef void (*traverse_callback_t)(int n, unsigned long mask, struct name_entry *entry, const char *base);
void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1);
typedef void (*traverse_callback_t)(int n, unsigned long mask, struct name_entry *entry, const char *base);
read_tree_fn_t fn)
{
struct tree_desc desc;
read_tree_fn_t fn)
{
struct tree_desc desc;
+ struct name_entry entry;
if (parse_tree(tree))
return -1;
if (parse_tree(tree))
return -1;
desc.buf = tree->buffer;
desc.size = tree->size;
desc.buf = tree->buffer;
desc.size = tree->size;
- while (desc.size) {
- unsigned mode;
- const char *name;
- const unsigned char *sha1;
-
- sha1 = tree_entry_extract(&desc, &name, &mode);
- update_tree_entry(&desc);
-
- if (!match_tree_entry(base, baselen, name, mode, match))
+ while (tree_entry(&desc, &entry)) {
+ if (!match_tree_entry(base, baselen, entry.path, entry.mode, match))
- switch (fn(sha1, base, baselen, name, mode, stage)) {
+ switch (fn(entry.sha1, base, baselen, entry.path, entry.mode, stage)) {
case 0:
continue;
case READ_TREE_RECURSIVE:
case 0:
continue;
case READ_TREE_RECURSIVE:
+ if (S_ISDIR(entry.mode)) {
- int pathlen = strlen(name);
- newbase = xmalloc(baselen + 1 + pathlen);
+ newbase = xmalloc(baselen + 1 + entry.pathlen);
memcpy(newbase, base, baselen);
memcpy(newbase, base, baselen);
- memcpy(newbase + baselen, name, pathlen);
- newbase[baselen + pathlen] = '/';
- retval = read_tree_recursive(lookup_tree(sha1),
+ memcpy(newbase + baselen, entry.path, entry.pathlen);
+ newbase[baselen + entry.pathlen] = '/';
+ retval = read_tree_recursive(lookup_tree(entry.sha1),
+ baselen + entry.pathlen + 1,
stage, match, fn);
free(newbase);
if (retval)
stage, match, fn);
free(newbase);
if (retval)
int n_refs = 0, i;
struct object_refs *refs;
struct tree_desc desc;
int n_refs = 0, i;
struct object_refs *refs;
struct tree_desc desc;
+ struct name_entry entry;
/* Count how many entries there are.. */
desc.buf = item->buffer;
/* Count how many entries there are.. */
desc.buf = item->buffer;
refs = alloc_object_refs(n_refs);
desc.buf = item->buffer;
desc.size = item->size;
refs = alloc_object_refs(n_refs);
desc.buf = item->buffer;
desc.size = item->size;
- while (desc.size) {
- unsigned mode;
- const char *name;
- const unsigned char *sha1;
+ while (tree_entry(&desc, &entry)) {
- sha1 = tree_entry_extract(&desc, &name, &mode);
- update_tree_entry(&desc);
- if (S_ISDIR(mode))
- obj = &lookup_tree(sha1)->object;
+ if (S_ISDIR(entry.mode))
+ obj = &lookup_tree(entry.sha1)->object;
- obj = &lookup_blob(sha1)->object;
+ obj = &lookup_blob(entry.sha1)->object;
refs->ref[i++] = obj;
}
set_object_refs(&item->object, refs);
refs->ref[i++] = obj;
}
set_object_refs(&item->object, refs);