Only use a single parser for tree objects
This makes read_tree_recursive and read_tree take a struct tree instead of a buffer. It also move the declaration of read_tree into tree.h (where struct tree is defined), and updates ls-tree and diff-index (the only places that presently use read_tree*()) to use the new versions. Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Daniel Barkalow committed
Jan 26, 2006 at 01:13 UTC
521698b1538fb3c9bd818ee98f2c17d2c80c9605
5 files changed
+31
-46
cache.h
-3
@@ -209,9 +209,6 @@ extern char *write_sha1_file_prepare(void *buf,
209
210
extern int check_sha1_signature(const unsigned char *sha1, void *buf, unsigned long size, const char *type);
211
212
-/* Read a tree into the cache */
213
-extern int read_tree(void *buffer, unsigned long size, int stage, const char **paths);
214
-
212
extern int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,
213
size_t bufsize, size_t *bufposn);
214
extern int write_sha1_to_fd(int fd, const unsigned char *sha1);
diff-index.c
+4
-4
@@ -1,4 +1,5 @@
1
#include "cache.h"
2
+#include "tree.h"
3
#include "diff.h"
4
5
static int cached_only = 0;
@@ -174,8 +175,7 @@ int main(int argc, const char **argv)
175
unsigned char sha1[20];
176
const char *prefix = setup_git_directory();
177
const char **pathspec = NULL;
177
- void *tree;
178
- unsigned long size;
178
+ struct tree *tree;
179
int ret;
180
int allow_options = 1;
181
int i;
@@ -233,10 +233,10 @@ int main(int argc, const char **argv)
233
234
mark_merge_entries();
235
236
- tree = read_object_with_reference(sha1, "tree", &size, NULL);
236
+ tree = parse_tree_indirect(sha1);
237
if (!tree)
238
die("bad tree object %s", tree_name);
239
- if (read_tree(tree, size, 1, pathspec))
239
+ if (read_tree(tree, 1, pathspec))
240
die("unable to read tree object %s", tree_name);
241
242
ret = diff_cache(active_cache, active_nr, pathspec);
ls-tree.c
+4
-5
@@ -84,8 +84,7 @@ static int show_tree(unsigned char *sha1, const char *base, int baselen,
84
int main(int argc, const char **argv)
85
{
86
unsigned char sha1[20];
87
- char *buf;
88
- unsigned long size;
87
+ struct tree *tree;
88
89
prefix = setup_git_directory();
90
if (prefix && *prefix)
@@ -131,10 +130,10 @@ int main(int argc, const char **argv)
130
usage(ls_tree_usage);
131
132
pathspec = get_pathspec(prefix, argv + 2);
134
- buf = read_object_with_reference(sha1, "tree", &size, NULL);
135
- if (!buf)
133
+ tree = parse_tree_indirect(sha1);
134
+ if (!tree)
135
die("not a tree object");
137
- read_tree_recursive(buf, size, "", 0, 0, pathspec, show_tree);
136
+ read_tree_recursive(tree, "", 0, 0, pathspec, show_tree);
137
138
return 0;
139
}
tree.c
+18
-30
@@ -74,27 +74,24 @@ static int match_tree_entry(const char *base, int baselen, const char *path, uns
74
return 0;
75
}
76
77
-int read_tree_recursive(void *buffer, unsigned long size,
77
+int read_tree_recursive(struct tree *tree,
78
const char *base, int baselen,
79
int stage, const char **match,
80
read_tree_fn_t fn)
81
{
82
- while (size) {
83
- int len = strlen(buffer)+1;
84
- unsigned char *sha1 = buffer + len;
85
- char *path = strchr(buffer, ' ')+1;
86
- unsigned int mode;
87
-
88
- if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
89
- return -1;
90
-
91
- buffer = sha1 + 20;
92
- size -= len + 20;
93
-
94
- if (!match_tree_entry(base, baselen, path, mode, match))
82
+ struct tree_entry_list *list;
83
+ if (parse_tree(tree))
84
+ return -1;
85
+ list = tree->entries;
86
+ while (list) {
87
+ struct tree_entry_list *current = list;
88
+ list = list->next;
89
+ if (!match_tree_entry(base, baselen, current->name,
90
+ current->mode, match))
91
continue;
92
97
- switch (fn(sha1, base, baselen, path, mode, stage)) {
93
+ switch (fn(current->item.any->sha1, base, baselen,
94
+ current->name, current->mode, stage)) {
95
case 0:
96
continue;
97
case READ_TREE_RECURSIVE:
@@ -102,28 +99,19 @@ int read_tree_recursive(void *buffer, unsigned long size,
99
default:
100
return -1;
101
}
105
- if (S_ISDIR(mode)) {
102
+ if (current->directory) {
103
int retval;
107
- int pathlen = strlen(path);
104
+ int pathlen = strlen(current->name);
105
char *newbase;
109
- void *eltbuf;
110
- char elttype[20];
111
- unsigned long eltsize;
106
113
- eltbuf = read_sha1_file(sha1, elttype, &eltsize);
114
- if (!eltbuf || strcmp(elttype, "tree")) {
115
- if (eltbuf) free(eltbuf);
116
- return -1;
117
- }
107
newbase = xmalloc(baselen + 1 + pathlen);
108
memcpy(newbase, base, baselen);
120
- memcpy(newbase + baselen, path, pathlen);
109
+ memcpy(newbase + baselen, current->name, pathlen);
110
newbase[baselen + pathlen] = '/';
122
- retval = read_tree_recursive(eltbuf, eltsize,
111
+ retval = read_tree_recursive(current->item.tree,
112
newbase,
113
baselen + pathlen + 1,
114
stage, match, fn);
126
- free(eltbuf);
115
free(newbase);
116
if (retval)
117
return -1;
@@ -133,9 +121,9 @@ int read_tree_recursive(void *buffer, unsigned long size,
121
return 0;
122
}
123
136
-int read_tree(void *buffer, unsigned long size, int stage, const char **match)
124
+int read_tree(struct tree *tree, int stage, const char **match)
125
{
138
- return read_tree_recursive(buffer, size, "", 0, stage, match, read_one_entry);
126
+ return read_tree_recursive(tree, "", 0, stage, match, read_one_entry);
127
}
128
129
struct tree *lookup_tree(const unsigned char *sha1)
tree.h
+5
-4
@@ -37,10 +37,11 @@ struct tree *parse_tree_indirect(const unsigned char *sha1);
37
#define READ_TREE_RECURSIVE 1
38
typedef int (*read_tree_fn_t)(unsigned char *, const char *, int, const char *, unsigned int, int);
39
40
-extern int read_tree_recursive(void *buffer, unsigned long size,
41
- const char *base, int baselen,
42
- int stage, const char **match,
43
- read_tree_fn_t fn);
40
+extern int read_tree_recursive(struct tree *tree,
41
+ const char *base, int baselen,
42
+ int stage, const char **match,
43
+ read_tree_fn_t fn);
44
45
+extern int read_tree(struct tree *tree, int stage, const char **paths);
46
47
#endif /* TREE_H */