9 const char *tree_type
= "tree";
11 static int read_one_entry(const unsigned char *sha1
, const char *base
, int baselen
, const char *pathname
, unsigned mode
, int stage
)
15 struct cache_entry
*ce
;
18 return READ_TREE_RECURSIVE
;
20 len
= strlen(pathname
);
21 size
= cache_entry_size(baselen
+ len
);
22 ce
= xcalloc(1, size
);
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);
29 return add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_SKIP_DFCHECK
);
32 static int match_tree_entry(const char *base
, int baselen
, const char *path
, unsigned int mode
, const char **paths
)
39 pathlen
= strlen(path
);
40 while ((match
= *paths
++) != NULL
) {
41 int matchlen
= strlen(match
);
43 if (baselen
>= matchlen
) {
44 /* If it doesn't match, move along... */
45 if (strncmp(base
, match
, matchlen
))
47 /* The base is a subdirectory of a path which was specified. */
51 /* Does the base match? */
52 if (strncmp(base
, match
, baselen
))
58 if (pathlen
> matchlen
)
61 if (matchlen
> pathlen
) {
62 if (match
[pathlen
] != '/')
68 if (strncmp(path
, match
, pathlen
))
76 int read_tree_recursive(struct tree
*tree
,
77 const char *base
, int baselen
,
78 int stage
, const char **match
,
81 struct tree_desc desc
;
82 struct name_entry entry
;
87 desc
.buf
= tree
->buffer
;
88 desc
.size
= tree
->size
;
90 while (tree_entry(&desc
, &entry
)) {
91 if (!match_tree_entry(base
, baselen
, entry
.path
, entry
.mode
, match
))
94 switch (fn(entry
.sha1
, base
, baselen
, entry
.path
, entry
.mode
, stage
)) {
97 case READ_TREE_RECURSIVE
:
102 if (S_ISDIR(entry
.mode
)) {
106 newbase
= xmalloc(baselen
+ 1 + entry
.pathlen
);
107 memcpy(newbase
, base
, baselen
);
108 memcpy(newbase
+ baselen
, entry
.path
, entry
.pathlen
);
109 newbase
[baselen
+ entry
.pathlen
] = '/';
110 retval
= read_tree_recursive(lookup_tree(entry
.sha1
),
112 baselen
+ entry
.pathlen
+ 1,
123 int read_tree(struct tree
*tree
, int stage
, const char **match
)
125 return read_tree_recursive(tree
, "", 0, stage
, match
, read_one_entry
);
128 struct tree
*lookup_tree(const unsigned char *sha1
)
130 struct object
*obj
= lookup_object(sha1
);
132 struct tree
*ret
= alloc_tree_node();
133 created_object(sha1
, &ret
->object
);
134 ret
->object
.type
= TYPE_TREE
;
138 obj
->type
= TYPE_TREE
;
139 if (obj
->type
!= TYPE_TREE
) {
140 error("Object %s is a %s, not a tree",
141 sha1_to_hex(sha1
), typename(obj
->type
));
144 return (struct tree
*) obj
;
147 static int track_tree_refs(struct tree
*item
)
150 struct object_refs
*refs
;
151 struct tree_desc desc
;
152 struct name_entry entry
;
154 /* Count how many entries there are.. */
155 desc
.buf
= item
->buffer
;
156 desc
.size
= item
->size
;
159 update_tree_entry(&desc
);
162 /* Allocate object refs and walk it again.. */
164 refs
= alloc_object_refs(n_refs
);
165 desc
.buf
= item
->buffer
;
166 desc
.size
= item
->size
;
167 while (tree_entry(&desc
, &entry
)) {
170 if (S_ISDIR(entry
.mode
))
171 obj
= &lookup_tree(entry
.sha1
)->object
;
173 obj
= &lookup_blob(entry
.sha1
)->object
;
174 refs
->ref
[i
++] = obj
;
176 set_object_refs(&item
->object
, refs
);
180 int parse_tree_buffer(struct tree
*item
, void *buffer
, unsigned long size
)
182 if (item
->object
.parsed
)
184 item
->object
.parsed
= 1;
185 item
->buffer
= buffer
;
188 if (track_object_refs
)
189 track_tree_refs(item
);
193 int parse_tree(struct tree
*item
)
199 if (item
->object
.parsed
)
201 buffer
= read_sha1_file(item
->object
.sha1
, type
, &size
);
203 return error("Could not read %s",
204 sha1_to_hex(item
->object
.sha1
));
205 if (strcmp(type
, tree_type
)) {
207 return error("Object %s not a tree",
208 sha1_to_hex(item
->object
.sha1
));
210 return parse_tree_buffer(item
, buffer
, size
);
213 struct tree
*parse_tree_indirect(const unsigned char *sha1
)
215 struct object
*obj
= parse_object(sha1
);
219 if (obj
->type
== TYPE_TREE
)
220 return (struct tree
*) obj
;
221 else if (obj
->type
== TYPE_COMMIT
)
222 obj
= &(((struct commit
*) obj
)->tree
->object
);
223 else if (obj
->type
== TYPE_TAG
)
224 obj
= ((struct tag
*) obj
)->tagged
;
228 parse_object(obj
->sha1
);