Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 285 lines (244 sloc) 6.224 kb
8f1d2e6f »
2006-01-07 [PATCH] Compilation: zero-length array declaration.
1 #include "cache.h"
175785e5 »
2005-04-18 [PATCH] Implementations of parsing functions
2 #include "tree.h"
3 #include "blob.h"
77675e2a »
2005-09-05 [PATCH] Add a function for getting a struct tree for an ent.
4 #include "commit.h"
5 #include "tag.h"
136f2e54 »
2006-05-29 Make "struct tree" contain the pointer to the tree buffer
6 #include "tree-walk.h"
175785e5 »
2005-04-18 [PATCH] Implementations of parsing functions
7 #include <stdlib.h>
8
9 const char *tree_type = "tree";
10
3a7c352b »
2006-05-29 Make "tree_entry" have a SHA1 instead of a union of object pointers
11 static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
94537c78 »
2005-04-22 Move "read_tree()" to "tree.c" to be used as a generic helper function.
12 {
3c5e8468 »
2005-11-26 ls-tree: major rewrite to do pathspec
13 int len;
14 unsigned int size;
15 struct cache_entry *ce;
16
17 if (S_ISDIR(mode))
18 return READ_TREE_RECURSIVE;
19
20 len = strlen(pathname);
21 size = cache_entry_size(baselen + len);
90321c10 »
2006-04-03 Replace xmalloc+memset(0) with xcalloc.
22 ce = xcalloc(1, size);
94537c78 »
2005-04-22 Move "read_tree()" to "tree.c" to be used as a generic helper function.
23
24 ce->ce_mode = create_ce_mode(mode);
25 ce->ce_flags = create_ce_flags(baselen + len, stage);
26 memcpy(ce->name, base, baselen);
27 memcpy(ce->name + baselen, pathname, len+1);
28 memcpy(ce->sha1, sha1, 20);
b155725d »
2005-06-25 [PATCH] Fix oversimplified optimization for add_cache_entry().
29 return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
94537c78 »
2005-04-22 Move "read_tree()" to "tree.c" to be used as a generic helper function.
30 }
31
3e587635 »
2005-07-14 Fix up read_tree() pathspec matching to use "const char **"
32 static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths)
0ca14a57 »
2005-07-14 Start adding interfaces to read in partial trees
33 {
3e587635 »
2005-07-14 Fix up read_tree() pathspec matching to use "const char **"
34 const char *match;
0ca14a57 »
2005-07-14 Start adding interfaces to read in partial trees
35 int pathlen;
36
37 if (!paths)
38 return 1;
39 pathlen = strlen(path);
40 while ((match = *paths++) != NULL) {
41 int matchlen = strlen(match);
42
43 if (baselen >= matchlen) {
44 /* If it doesn't match, move along... */
45 if (strncmp(base, match, matchlen))
46 continue;
47 /* The base is a subdirectory of a path which was specified. */
48 return 1;
49 }
50
51 /* Does the base match? */
52 if (strncmp(base, match, baselen))
53 continue;
54
55 match += baselen;
56 matchlen -= baselen;
57
58 if (pathlen > matchlen)
59 continue;
60
61 if (matchlen > pathlen) {
62 if (match[pathlen] != '/')
63 continue;
64 if (!S_ISDIR(mode))
65 continue;
66 }
67
68 if (strncmp(path, match, pathlen))
69 continue;
3e587635 »
2005-07-14 Fix up read_tree() pathspec matching to use "const char **"
70
71 return 1;
0ca14a57 »
2005-07-14 Start adding interfaces to read in partial trees
72 }
73 return 0;
74 }
75
521698b1 »
2006-01-26 Only use a single parser for tree objects
76 int read_tree_recursive(struct tree *tree,
3c5e8468 »
2005-11-26 ls-tree: major rewrite to do pathspec
77 const char *base, int baselen,
78 int stage, const char **match,
79 read_tree_fn_t fn)
94537c78 »
2005-04-22 Move "read_tree()" to "tree.c" to be used as a generic helper function.
80 {
0790a42a »
2006-05-29 Switch "read_tree_recursive()" over to tree-walk functionality
81 struct tree_desc desc;
82
521698b1 »
2006-01-26 Only use a single parser for tree objects
83 if (parse_tree(tree))
84 return -1;
0790a42a »
2006-05-29 Switch "read_tree_recursive()" over to tree-walk functionality
85
86 desc.buf = tree->buffer;
87 desc.size = tree->size;
88
89 while (desc.size) {
90 unsigned mode;
91 const char *name;
92 const unsigned char *sha1;
93
94 sha1 = tree_entry_extract(&desc, &name, &mode);
95 update_tree_entry(&desc);
96
97 if (!match_tree_entry(base, baselen, name, mode, match))
0ca14a57 »
2005-07-14 Start adding interfaces to read in partial trees
98 continue;
99
0790a42a »
2006-05-29 Switch "read_tree_recursive()" over to tree-walk functionality
100 switch (fn(sha1, base, baselen, name, mode, stage)) {
3c5e8468 »
2005-11-26 ls-tree: major rewrite to do pathspec
101 case 0:
102 continue;
103 case READ_TREE_RECURSIVE:
104 break;;
105 default:
106 return -1;
107 }
0790a42a »
2006-05-29 Switch "read_tree_recursive()" over to tree-walk functionality
108 if (S_ISDIR(mode)) {
94537c78 »
2005-04-22 Move "read_tree()" to "tree.c" to be used as a generic helper function.
109 int retval;
0790a42a »
2006-05-29 Switch "read_tree_recursive()" over to tree-walk functionality
110 int pathlen = strlen(name);
1c9da46d »
2005-04-27 [PATCH] read_tree_recursive(): Fix leaks
111 char *newbase;
94537c78 »
2005-04-22 Move "read_tree()" to "tree.c" to be used as a generic helper function.
112
1c9da46d »
2005-04-27 [PATCH] read_tree_recursive(): Fix leaks
113 newbase = xmalloc(baselen + 1 + pathlen);
94537c78 »
2005-04-22 Move "read_tree()" to "tree.c" to be used as a generic helper function.
114 memcpy(newbase, base, baselen);
0790a42a »
2006-05-29 Switch "read_tree_recursive()" over to tree-walk functionality
115 memcpy(newbase + baselen, name, pathlen);
94537c78 »
2005-04-22 Move "read_tree()" to "tree.c" to be used as a generic helper function.
116 newbase[baselen + pathlen] = '/';
0790a42a »
2006-05-29 Switch "read_tree_recursive()" over to tree-walk functionality
117 retval = read_tree_recursive(lookup_tree(sha1),
94537c78 »
2005-04-22 Move "read_tree()" to "tree.c" to be used as a generic helper function.
118 newbase,
0ca14a57 »
2005-07-14 Start adding interfaces to read in partial trees
119 baselen + pathlen + 1,
3c5e8468 »
2005-11-26 ls-tree: major rewrite to do pathspec
120 stage, match, fn);
94537c78 »
2005-04-22 Move "read_tree()" to "tree.c" to be used as a generic helper function.
121 free(newbase);
122 if (retval)
123 return -1;
124 continue;
125 }
126 }
127 return 0;
128 }
129
521698b1 »
2006-01-26 Only use a single parser for tree objects
130 int read_tree(struct tree *tree, int stage, const char **match)
94537c78 »
2005-04-22 Move "read_tree()" to "tree.c" to be used as a generic helper function.
131 {
521698b1 »
2006-01-26 Only use a single parser for tree objects
132 return read_tree_recursive(tree, "", 0, stage, match, read_one_entry);
94537c78 »
2005-04-22 Move "read_tree()" to "tree.c" to be used as a generic helper function.
133 }
134
5d6ccf5c »
2005-06-03 [PATCH] Anal retentive 'const unsigned char *sha1'
135 struct tree *lookup_tree(const unsigned char *sha1)
175785e5 »
2005-04-18 [PATCH] Implementations of parsing functions
136 {
137 struct object *obj = lookup_object(sha1);
138 if (!obj) {
90321c10 »
2006-04-03 Replace xmalloc+memset(0) with xcalloc.
139 struct tree *ret = xcalloc(1, sizeof(struct tree));
175785e5 »
2005-04-18 [PATCH] Implementations of parsing functions
140 created_object(sha1, &ret->object);
d32987be »
2005-04-24 Set object type at object creation time, not object parse time.
141 ret->object.type = tree_type;
175785e5 »
2005-04-18 [PATCH] Implementations of parsing functions
142 return ret;
143 }
d1af002d »
2005-05-20 [PATCH] delta check
144 if (!obj->type)
145 obj->type = tree_type;
c35dfe85 »
2005-04-24 Verify that the object type matches for tree/commit objects even befo…
146 if (obj->type != tree_type) {
175785e5 »
2005-04-18 [PATCH] Implementations of parsing functions
147 error("Object %s is a %s, not a tree",
148 sha1_to_hex(sha1), obj->type);
149 return NULL;
150 }
151 return (struct tree *) obj;
152 }
153
2d9c58c6 »
2006-05-29 Remove "tree->entries" tree-entry list from tree parser
154 static int track_tree_refs(struct tree *item)
175785e5 »
2005-04-18 [PATCH] Implementations of parsing functions
155 {
2d9c58c6 »
2006-05-29 Remove "tree->entries" tree-entry list from tree parser
156 int n_refs = 0, i;
157 struct object_refs *refs;
136f2e54 »
2006-05-29 Make "struct tree" contain the pointer to the tree buffer
158 struct tree_desc desc;
bd2c39f5 »
2005-05-06 [PATCH] don't load and decompress objects twice with parse_object()
159
2d9c58c6 »
2006-05-29 Remove "tree->entries" tree-entry list from tree parser
160 /* Count how many entries there are.. */
161 desc.buf = item->buffer;
162 desc.size = item->size;
163 while (desc.size) {
164 n_refs++;
165 update_tree_entry(&desc);
166 }
167
168 /* Allocate object refs and walk it again.. */
169 i = 0;
170 refs = alloc_object_refs(n_refs);
171 desc.buf = item->buffer;
172 desc.size = item->size;
173 while (desc.size) {
174 unsigned mode;
175 const char *name;
176 const unsigned char *sha1;
177 struct object *obj;
178
179 sha1 = tree_entry_extract(&desc, &name, &mode);
180 update_tree_entry(&desc);
181 if (S_ISDIR(mode))
182 obj = &lookup_tree(sha1)->object;
183 else
184 obj = &lookup_blob(sha1)->object;
185 refs->ref[i++] = obj;
186 }
187 set_object_refs(&item->object, refs);
188 return 0;
189 }
190
191 int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
192 {
175785e5 »
2005-04-18 [PATCH] Implementations of parsing functions
193 if (item->object.parsed)
194 return 0;
195 item->object.parsed = 1;
136f2e54 »
2006-05-29 Make "struct tree" contain the pointer to the tree buffer
196 item->buffer = buffer;
197 item->size = size;
198
2d9c58c6 »
2006-05-29 Remove "tree->entries" tree-entry list from tree parser
199 if (track_object_refs)
200 track_tree_refs(item);
201 return 0;
202 }
203
204 struct tree_entry_list *create_tree_entry_list(struct tree *tree)
205 {
206 struct tree_desc desc;
207 struct tree_entry_list *ret = NULL;
208 struct tree_entry_list **list_p = &ret;
209
210 desc.buf = tree->buffer;
211 desc.size = tree->size;
136f2e54 »
2006-05-29 Make "struct tree" contain the pointer to the tree buffer
212
213 while (desc.size) {
214 unsigned mode;
215 const char *path;
216 const unsigned char *sha1;
08692164 »
2005-04-23 [PATCH] Parse tree objects completely
217 struct tree_entry_list *entry;
136f2e54 »
2006-05-29 Make "struct tree" contain the pointer to the tree buffer
218
219 sha1 = tree_entry_extract(&desc, &path, &mode);
175785e5 »
2005-04-18 [PATCH] Implementations of parsing functions
220
812666c8 »
2005-04-26 [PATCH] introduce xmalloc and xrealloc
221 entry = xmalloc(sizeof(struct tree_entry_list));
136f2e54 »
2006-05-29 Make "struct tree" contain the pointer to the tree buffer
222 entry->name = path;
3a7c352b »
2006-05-29 Make "tree_entry" have a SHA1 instead of a union of object pointers
223 entry->sha1 = sha1;
136f2e54 »
2006-05-29 Make "struct tree" contain the pointer to the tree buffer
224 entry->mode = mode;
42ea9cb2 »
2005-05-05 Be more careful about tree entry modes.
225 entry->directory = S_ISDIR(mode) != 0;
226 entry->executable = (mode & S_IXUSR) != 0;
227 entry->symlink = S_ISLNK(mode) != 0;
136f2e54 »
2006-05-29 Make "struct tree" contain the pointer to the tree buffer
228 entry->zeropad = *(const char *)(desc.buf) == '0';
08692164 »
2005-04-23 [PATCH] Parse tree objects completely
229 entry->next = NULL;
230
136f2e54 »
2006-05-29 Make "struct tree" contain the pointer to the tree buffer
231 update_tree_entry(&desc);
08692164 »
2005-04-23 [PATCH] Parse tree objects completely
232 *list_p = entry;
233 list_p = &entry->next;
175785e5 »
2005-04-18 [PATCH] Implementations of parsing functions
234 }
2d9c58c6 »
2006-05-29 Remove "tree->entries" tree-entry list from tree parser
235 return ret;
236 }
4a4e6fd7 »
2005-11-15 Rework object refs tracking to reduce memory usage
237
2d9c58c6 »
2006-05-29 Remove "tree->entries" tree-entry list from tree parser
238 void free_tree_entry_list(struct tree_entry_list *list)
239 {
240 while (list) {
241 struct tree_entry_list *next = list->next;
242 free(list);
243 list = next;
4a4e6fd7 »
2005-11-15 Rework object refs tracking to reduce memory usage
244 }
175785e5 »
2005-04-18 [PATCH] Implementations of parsing functions
245 }
bd2c39f5 »
2005-05-06 [PATCH] don't load and decompress objects twice with parse_object()
246
247 int parse_tree(struct tree *item)
248 {
249 char type[20];
250 void *buffer;
251 unsigned long size;
252
253 if (item->object.parsed)
254 return 0;
255 buffer = read_sha1_file(item->object.sha1, type, &size);
256 if (!buffer)
257 return error("Could not read %s",
258 sha1_to_hex(item->object.sha1));
259 if (strcmp(type, tree_type)) {
260 free(buffer);
261 return error("Object %s not a tree",
262 sha1_to_hex(item->object.sha1));
263 }
136f2e54 »
2006-05-29 Make "struct tree" contain the pointer to the tree buffer
264 return parse_tree_buffer(item, buffer, size);
bd2c39f5 »
2005-05-06 [PATCH] don't load and decompress objects twice with parse_object()
265 }
77675e2a »
2005-09-05 [PATCH] Add a function for getting a struct tree for an ent.
266
267 struct tree *parse_tree_indirect(const unsigned char *sha1)
268 {
269 struct object *obj = parse_object(sha1);
270 do {
271 if (!obj)
272 return NULL;
273 if (obj->type == tree_type)
274 return (struct tree *) obj;
275 else if (obj->type == commit_type)
276 obj = &(((struct commit *) obj)->tree->object);
277 else if (obj->type == tag_type)
278 obj = ((struct tag *) obj)->tagged;
279 else
280 return NULL;
281 if (!obj->parsed)
282 parse_object(obj->sha1);
283 } while (1);
284 }
Something went wrong with that request. Please try again.