summaryrefslogtreecommitdiff
path: root/tree-walk.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2008-03-06 02:59:29 (GMT)
committerJunio C Hamano <gitster@pobox.com>2008-03-09 08:43:47 (GMT)
commit40d934df72eaf244c826d5c26da0896ce7185cb6 (patch)
treeee461fff98614c9a4593f16944a2ff0b05d00a78 /tree-walk.c
parent0ab9e1e8cdaefdd33bf24bb0be0ec766483f8bbe (diff)
downloadgit-40d934df72eaf244c826d5c26da0896ce7185cb6.zip
git-40d934df72eaf244c826d5c26da0896ce7185cb6.tar.gz
git-40d934df72eaf244c826d5c26da0896ce7185cb6.tar.bz2
Make 'traverse_tree()' use linked structure rather than 'const char *base'
This makes the calling convention a bit less obvious, but a lot more flexible. Instead of allocating and extending a new 'base' string, we just link the top-most name into a linked list of the 'info' structure when traversing a subdirectory, and we can generate the basename by following the list. Perhaps even more importantly, the linked list of info structures also gives us a place to naturally save off other information than just the directory name. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'tree-walk.c')
-rw-r--r--tree-walk.c35
1 files changed, 33 insertions, 2 deletions
diff --git a/tree-walk.c b/tree-walk.c
index 142205d..f9f7d22 100644
--- a/tree-walk.c
+++ b/tree-walk.c
@@ -104,7 +104,38 @@ int tree_entry(struct tree_desc *desc, struct name_entry *entry)
return 1;
}
-void traverse_trees(int n, struct tree_desc *t, const char *base, traverse_callback_t callback)
+void setup_traverse_info(struct traverse_info *info, const char *base)
+{
+ int pathlen = strlen(base);
+
+ memset(info, 0, sizeof(*info));
+ if (pathlen && base[pathlen-1] == '/')
+ pathlen--;
+ info->pathlen = pathlen ? pathlen + 1 : 0;
+ info->name.path = base;
+ info->name.sha1 = (void *)(base + pathlen + 1);
+}
+
+char *make_traverse_path(char *path, const struct traverse_info *info, const struct name_entry *n)
+{
+ int len = tree_entry_len(n->path, n->sha1);
+ int pathlen = info->pathlen;
+
+ path[pathlen + len] = 0;
+ for (;;) {
+ memcpy(path + pathlen, n->path, len);
+ if (!pathlen)
+ break;
+ path[--pathlen] = '/';
+ n = &info->name;
+ len = tree_entry_len(n->path, n->sha1);
+ info = info->prev;
+ pathlen -= len;
+ }
+ return path;
+}
+
+void traverse_trees(int n, struct tree_desc *t, struct traverse_info *info)
{
struct name_entry *entry = xmalloc(n*sizeof(*entry));
@@ -150,7 +181,7 @@ void traverse_trees(int n, struct tree_desc *t, const char *base, traverse_callb
}
entry_clear(entry + i);
}
- callback(n, mask, entry, base);
+ info->fn(n, mask, entry, info);
}
free(entry);
}