2 #include "cache-tree.h"
9 const char *tree_type
= "tree";
11 static int read_one_entry_opt(const unsigned char *sha1
, const char *base
, int baselen
, const char *pathname
, unsigned mode
, int stage
, int opt
)
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 hashcpy(ce
->sha1
, sha1
);
29 return add_cache_entry(ce
, opt
);
32 static int read_one_entry(const unsigned char *sha1
, const char *base
, int baselen
, const char *pathname
, unsigned mode
, int stage
)
34 return read_one_entry_opt(sha1
, base
, baselen
, pathname
, mode
, stage
,
35 ADD_CACHE_OK_TO_ADD
|ADD_CACHE_SKIP_DFCHECK
);
39 * This is used when the caller knows there is no existing entries at
40 * the stage that will conflict with the entry being added.
42 static int read_one_entry_quick(const unsigned char *sha1
, const char *base
, int baselen
, const char *pathname
, unsigned mode
, int stage
)
44 return read_one_entry_opt(sha1
, base
, baselen
, pathname
, mode
, stage
,
45 ADD_CACHE_JUST_APPEND
);
48 static int match_tree_entry(const char *base
, int baselen
, const char *path
, unsigned int mode
, const char **paths
)
55 pathlen
= strlen(path
);
56 while ((match
= *paths
++) != NULL
) {
57 int matchlen
= strlen(match
);
59 if (baselen
>= matchlen
) {
60 /* If it doesn't match, move along... */
61 if (strncmp(base
, match
, matchlen
))
63 /* The base is a subdirectory of a path which was specified. */
67 /* Does the base match? */
68 if (strncmp(base
, match
, baselen
))
74 if (pathlen
> matchlen
)
77 if (matchlen
> pathlen
) {
78 if (match
[pathlen
] != '/')
84 if (strncmp(path
, match
, pathlen
))
92 int read_tree_recursive(struct tree
*tree
,
93 const char *base
, int baselen
,
94 int stage
, const char **match
,
97 struct tree_desc desc
;
98 struct name_entry entry
;
100 if (parse_tree(tree
))
103 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
105 while (tree_entry(&desc
, &entry
)) {
106 if (!match_tree_entry(base
, baselen
, entry
.path
, entry
.mode
, match
))
109 switch (fn(entry
.sha1
, base
, baselen
, entry
.path
, entry
.mode
, stage
)) {
112 case READ_TREE_RECURSIVE
:
117 if (S_ISDIR(entry
.mode
)) {
120 unsigned int pathlen
= tree_entry_len(entry
.path
, entry
.sha1
);
122 newbase
= xmalloc(baselen
+ 1 + pathlen
);
123 memcpy(newbase
, base
, baselen
);
124 memcpy(newbase
+ baselen
, entry
.path
, pathlen
);
125 newbase
[baselen
+ pathlen
] = '/';
126 retval
= read_tree_recursive(lookup_tree(entry
.sha1
),
128 baselen
+ pathlen
+ 1,
139 static int cmp_cache_name_compare(const void *a_
, const void *b_
)
141 const struct cache_entry
*ce1
, *ce2
;
143 ce1
= *((const struct cache_entry
**)a_
);
144 ce2
= *((const struct cache_entry
**)b_
);
145 return cache_name_compare(ce1
->name
, ntohs(ce1
->ce_flags
),
146 ce2
->name
, ntohs(ce2
->ce_flags
));
149 int read_tree(struct tree
*tree
, int stage
, const char **match
)
151 read_tree_fn_t fn
= NULL
;
155 * Currently the only existing callers of this function all
156 * call it with stage=1 and after making sure there is nothing
157 * at that stage; we could always use read_one_entry_quick().
159 * But when we decide to straighten out git-read-tree not to
160 * use unpack_trees() in some cases, this will probably start
165 * See if we have cache entry at the stage. If so,
166 * do it the original slow way, otherwise, append and then
169 for (i
= 0; !fn
&& i
< active_nr
; i
++) {
170 struct cache_entry
*ce
= active_cache
[i
];
171 if (ce_stage(ce
) == stage
)
176 fn
= read_one_entry_quick
;
177 err
= read_tree_recursive(tree
, "", 0, stage
, match
, fn
);
178 if (fn
== read_one_entry
|| err
)
182 * Sort the cache entry -- we need to nuke the cache tree, though.
184 cache_tree_free(&active_cache_tree
);
185 qsort(active_cache
, active_nr
, sizeof(active_cache
[0]),
186 cmp_cache_name_compare
);
190 struct tree
*lookup_tree(const unsigned char *sha1
)
192 struct object
*obj
= lookup_object(sha1
);
194 return create_object(sha1
, OBJ_TREE
, alloc_tree_node());
196 obj
->type
= OBJ_TREE
;
197 if (obj
->type
!= OBJ_TREE
) {
198 error("Object %s is a %s, not a tree",
199 sha1_to_hex(sha1
), typename(obj
->type
));
202 return (struct tree
*) obj
;
206 * NOTE! Tree refs to external git repositories
207 * (ie gitlinks) do not count as real references.
209 * You don't have to have those repositories
210 * available at all, much less have the objects
211 * accessible from the current repository.
213 static void track_tree_refs(struct tree
*item
)
216 struct object_refs
*refs
;
217 struct tree_desc desc
;
218 struct name_entry entry
;
220 /* Count how many entries there are.. */
221 init_tree_desc(&desc
, item
->buffer
, item
->size
);
222 while (tree_entry(&desc
, &entry
)) {
223 if (S_ISGITLINK(entry
.mode
))
228 /* Allocate object refs and walk it again.. */
230 refs
= alloc_object_refs(n_refs
);
231 init_tree_desc(&desc
, item
->buffer
, item
->size
);
232 while (tree_entry(&desc
, &entry
)) {
235 if (S_ISGITLINK(entry
.mode
))
237 if (S_ISDIR(entry
.mode
))
238 obj
= &lookup_tree(entry
.sha1
)->object
;
239 else if (S_ISREG(entry
.mode
) || S_ISLNK(entry
.mode
))
240 obj
= &lookup_blob(entry
.sha1
)->object
;
242 warning("in tree %s: entry %s has bad mode %.6o\n",
243 sha1_to_hex(item
->object
.sha1
), entry
.path
, entry
.mode
);
244 obj
= lookup_unknown_object(entry
.sha1
);
246 refs
->ref
[i
++] = obj
;
248 set_object_refs(&item
->object
, refs
);
251 int parse_tree_buffer(struct tree
*item
, void *buffer
, unsigned long size
)
253 if (item
->object
.parsed
)
255 item
->object
.parsed
= 1;
256 item
->buffer
= buffer
;
259 if (track_object_refs
)
260 track_tree_refs(item
);
264 int parse_tree(struct tree
*item
)
266 enum object_type type
;
270 if (item
->object
.parsed
)
272 buffer
= read_sha1_file(item
->object
.sha1
, &type
, &size
);
274 return error("Could not read %s",
275 sha1_to_hex(item
->object
.sha1
));
276 if (type
!= OBJ_TREE
) {
278 return error("Object %s not a tree",
279 sha1_to_hex(item
->object
.sha1
));
281 return parse_tree_buffer(item
, buffer
, size
);
284 struct tree
*parse_tree_indirect(const unsigned char *sha1
)
286 struct object
*obj
= parse_object(sha1
);
290 if (obj
->type
== OBJ_TREE
)
291 return (struct tree
*) obj
;
292 else if (obj
->type
== OBJ_COMMIT
)
293 obj
= &(((struct commit
*) obj
)->tree
->object
);
294 else if (obj
->type
== OBJ_TAG
)
295 obj
= ((struct tag
*) obj
)->tagged
;
299 parse_object(obj
->sha1
);