{"thread":{"id":"3154","subject":"[PATCH] Only use a single parser for tree objects","startedAt":"2006-01-26T06:13:36Z","lastAt":"2006-01-26T06:13:36Z","messageCount":1,"participants":["Daniel Barkalow"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"15134","messageId":"Pine.LNX.4.64.0601260103360.25300@iabervon.org","threadId":"3154","inReplyTo":null,"subject":"[PATCH] Only use a single parser for tree objects","fromName":"Daniel Barkalow","fromEmail":"barkalow@iabervon.org","sentAt":"2006-01-26T06:13:36Z","receivedAt":"2006-01-26T06:13:36Z","isPatch":true,"sender":{"key":"barkalow@iabervon.org","avatar":"https://avatars.githubusercontent.com/u/55364219?v=4"},"body":"This makes read_tree_recursive and read_tree take a struct tree\ninstead of a buffer. It also move the declaration of read_tree into\ntree.h (where struct tree is defined), and updates ls-tree and\ndiff-index (the only places that presently use read_tree*()) to use\nthe new versions.\n\nSigned-off-by: Daniel Barkalow <barkalow@iabervon.org>\n\n---\n\n cache.h      |    3 ---\n diff-index.c |    8 ++++----\n ls-tree.c    |    9 ++++-----\n tree.c       |   50 +++++++++++++++++++-------------------------------\n tree.h       |    9 +++++----\n 5 files changed, 32 insertions(+), 47 deletions(-)\n\n973280cda82b9ead56881d453c6101e4bf298c94\ndiff --git a/cache.h b/cache.h\nindex 6f13434..24a679b 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -210,9 +210,6 @@ extern char *write_sha1_file_prepare(voi\n \n extern int check_sha1_signature(const unsigned char *sha1, void *buf, unsigned long size, const char *type);\n \n-/* Read a tree into the cache */\n-extern int read_tree(void *buffer, unsigned long size, int stage, const char **paths);\n-\n extern int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,\n \t\t\t      size_t bufsize, size_t *bufposn);\n extern int write_sha1_to_fd(int fd, const unsigned char *sha1);\ndiff --git a/diff-index.c b/diff-index.c\nindex 87e1061..bbd873b 100644\n--- a/diff-index.c\n+++ b/diff-index.c\n@@ -1,4 +1,5 @@\n #include \"cache.h\"\n+#include \"tree.h\"\n #include \"diff.h\"\n \n static int cached_only = 0;\n@@ -174,8 +175,7 @@ int main(int argc, const char **argv)\n \tunsigned char sha1[20];\n \tconst char *prefix = setup_git_directory();\n \tconst char **pathspec = NULL;\n-\tvoid *tree;\n-\tunsigned long size;\n+\tstruct tree *tree;\n \tint ret;\n \tint allow_options = 1;\n \tint i;\n@@ -233,10 +233,10 @@ int main(int argc, const char **argv)\n \n \tmark_merge_entries();\n \n-\ttree = read_object_with_reference(sha1, \"tree\", &size, NULL);\n+\ttree = parse_tree_indirect(sha1);\n \tif (!tree)\n \t\tdie(\"bad tree object %s\", tree_name);\n-\tif (read_tree(tree, size, 1, pathspec))\n+\tif (read_tree(tree, 1, pathspec))\n \t\tdie(\"unable to read tree object %s\", tree_name);\n \n \tret = diff_cache(active_cache, active_nr, pathspec);\ndiff --git a/ls-tree.c b/ls-tree.c\nindex d585b6f..d005643 100644\n--- a/ls-tree.c\n+++ b/ls-tree.c\n@@ -84,8 +84,7 @@ static int show_tree(unsigned char *sha1\n int main(int argc, const char **argv)\n {\n \tunsigned char sha1[20];\n-\tchar *buf;\n-\tunsigned long size;\n+\tstruct tree *tree;\n \n \tprefix = setup_git_directory();\n \tif (prefix && *prefix)\n@@ -131,10 +130,10 @@ int main(int argc, const char **argv)\n \t\tusage(ls_tree_usage);\n \n \tpathspec = get_pathspec(prefix, argv + 2);\n-\tbuf = read_object_with_reference(sha1, \"tree\", &size, NULL);\n-\tif (!buf)\n+\ttree = parse_tree_indirect(sha1);\n+\tif (!tree)\n \t\tdie(\"not a tree object\");\n-\tread_tree_recursive(buf, size, \"\", 0, 0, pathspec, show_tree);\n+\tread_tree_recursive(tree, \"\", 0, 0, pathspec, show_tree);\n \n \treturn 0;\n }\ndiff --git a/tree.c b/tree.c\nindex dc1c41e..962ee89 100644\n--- a/tree.c\n+++ b/tree.c\n@@ -74,27 +74,24 @@ static int match_tree_entry(const char *\n \treturn 0;\n }\n \n-int read_tree_recursive(void *buffer, unsigned long size,\n+int read_tree_recursive(struct tree *tree,\n \t\t\tconst char *base, int baselen,\n \t\t\tint stage, const char **match,\n \t\t\tread_tree_fn_t fn)\n {\n-\twhile (size) {\n-\t\tint len = strlen(buffer)+1;\n-\t\tunsigned char *sha1 = buffer + len;\n-\t\tchar *path = strchr(buffer, ' ')+1;\n-\t\tunsigned int mode;\n-\n-\t\tif (size < len + 20 || sscanf(buffer, \"%o\", &mode) != 1)\n-\t\t\treturn -1;\n-\n-\t\tbuffer = sha1 + 20;\n-\t\tsize -= len + 20;\n-\n-\t\tif (!match_tree_entry(base, baselen, path, mode, match))\n+\tstruct tree_entry_list *list;\n+\tif (parse_tree(tree))\n+\t\treturn -1;\n+\tlist = tree->entries;\n+\twhile (list) {\n+\t\tstruct tree_entry_list *current = list;\n+\t\tlist = list->next;\n+\t\tif (!match_tree_entry(base, baselen, current->name, \n+\t\t\t\t      current->mode, match))\n \t\t\tcontinue;\n \n-\t\tswitch (fn(sha1, base, baselen, path, mode, stage)) {\n+\t\tswitch (fn(current->item.any->sha1, base, baselen, \n+\t\t\t   current->name, current->mode, stage)) {\n \t\tcase 0:\n \t\t\tcontinue;\n \t\tcase READ_TREE_RECURSIVE:\n@@ -102,28 +99,19 @@ int read_tree_recursive(void *buffer, un\n \t\tdefault:\n \t\t\treturn -1;\n \t\t}\n-\t\tif (S_ISDIR(mode)) {\n+\t\tif (current->directory) {\n \t\t\tint retval;\n-\t\t\tint pathlen = strlen(path);\n+\t\t\tint pathlen = strlen(current->name);\n \t\t\tchar *newbase;\n-\t\t\tvoid *eltbuf;\n-\t\t\tchar elttype[20];\n-\t\t\tunsigned long eltsize;\n-\n-\t\t\teltbuf = read_sha1_file(sha1, elttype, &eltsize);\n-\t\t\tif (!eltbuf || strcmp(elttype, \"tree\")) {\n-\t\t\t\tif (eltbuf) free(eltbuf);\n-\t\t\t\treturn -1;\n-\t\t\t}\n+\n \t\t\tnewbase = xmalloc(baselen + 1 + pathlen);\n \t\t\tmemcpy(newbase, base, baselen);\n-\t\t\tmemcpy(newbase + baselen, path, pathlen);\n+\t\t\tmemcpy(newbase + baselen, current->name, pathlen);\n \t\t\tnewbase[baselen + pathlen] = '/';\n-\t\t\tretval = read_tree_recursive(eltbuf, eltsize,\n+\t\t\tretval = read_tree_recursive(current->item.tree,\n \t\t\t\t\t\t     newbase,\n \t\t\t\t\t\t     baselen + pathlen + 1,\n \t\t\t\t\t\t     stage, match, fn);\n-\t\t\tfree(eltbuf);\n \t\t\tfree(newbase);\n \t\t\tif (retval)\n \t\t\t\treturn -1;\n@@ -133,9 +121,9 @@ int read_tree_recursive(void *buffer, un\n \treturn 0;\n }\n \n-int read_tree(void *buffer, unsigned long size, int stage, const char **match)\n+int read_tree(struct tree *tree, int stage, const char **match)\n {\n-\treturn read_tree_recursive(buffer, size, \"\", 0, stage, match, read_one_entry);\n+\treturn read_tree_recursive(tree, \"\", 0, stage, match, read_one_entry);\n }\n \n struct tree *lookup_tree(const unsigned char *sha1)\ndiff --git a/tree.h b/tree.h\nindex 57a5bf7..330ab64 100644\n--- a/tree.h\n+++ b/tree.h\n@@ -37,10 +37,11 @@ struct tree *parse_tree_indirect(const u\n #define READ_TREE_RECURSIVE 1\n typedef int (*read_tree_fn_t)(unsigned char *, const char *, int, const char *, unsigned int, int);\n \n-extern int read_tree_recursive(void *buffer, unsigned long size,\n-\t\t\tconst char *base, int baselen,\n-\t\t\tint stage, const char **match,\n-\t\t\tread_tree_fn_t fn);\n+extern int read_tree_recursive(struct tree *tree,\n+\t\t\t       const char *base, int baselen,\n+\t\t\t       int stage, const char **match,\n+\t\t\t       read_tree_fn_t fn);\n \n+extern int read_tree(struct tree *tree, int stage, const char **paths);\n \n #endif /* TREE_H */\n-- \n1.0.GIT\n"}]}