]>
git.ipfire.org Git - thirdparty/git.git/blob - tree.c
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
, void *context
)
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
, void *context
)
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 /* pathspecs match only at the directory boundaries */
65 baselen
== matchlen
||
66 base
[matchlen
] == '/' ||
67 match
[matchlen
- 1] == '/')
72 /* Does the base match? */
73 if (strncmp(base
, match
, baselen
))
79 if (pathlen
> matchlen
)
82 if (matchlen
> pathlen
) {
83 if (match
[pathlen
] != '/')
89 if (strncmp(path
, match
, pathlen
))
97 int read_tree_recursive(struct tree
*tree
,
98 const char *base
, int baselen
,
99 int stage
, const char **match
,
100 read_tree_fn_t fn
, void *context
)
102 struct tree_desc desc
;
103 struct name_entry entry
;
105 if (parse_tree(tree
))
108 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
110 while (tree_entry(&desc
, &entry
)) {
111 if (!match_tree_entry(base
, baselen
, entry
.path
, entry
.mode
, match
))
114 switch (fn(entry
.sha1
, base
, baselen
, entry
.path
, entry
.mode
, stage
, context
)) {
117 case READ_TREE_RECURSIVE
:
122 if (S_ISDIR(entry
.mode
)) {
125 unsigned int pathlen
= tree_entry_len(entry
.path
, entry
.sha1
);
127 newbase
= xmalloc(baselen
+ 1 + pathlen
);
128 memcpy(newbase
, base
, baselen
);
129 memcpy(newbase
+ baselen
, entry
.path
, pathlen
);
130 newbase
[baselen
+ pathlen
] = '/';
131 retval
= read_tree_recursive(lookup_tree(entry
.sha1
),
133 baselen
+ pathlen
+ 1,
134 stage
, match
, fn
, context
);
139 } else if (S_ISGITLINK(entry
.mode
)) {
142 unsigned int entrylen
;
143 struct commit
*commit
;
145 entrylen
= tree_entry_len(entry
.path
, entry
.sha1
);
146 strbuf_init(&path
, baselen
+ entrylen
+ 1);
147 strbuf_add(&path
, base
, baselen
);
148 strbuf_add(&path
, entry
.path
, entrylen
);
149 strbuf_addch(&path
, '/');
151 commit
= lookup_commit(entry
.sha1
);
153 die("Commit %s in submodule path %s not found",
154 sha1_to_hex(entry
.sha1
), path
.buf
);
156 if (parse_commit(commit
))
157 die("Invalid commit %s in submodule path %s",
158 sha1_to_hex(entry
.sha1
), path
.buf
);
160 retval
= read_tree_recursive(commit
->tree
,
162 stage
, match
, fn
, context
);
163 strbuf_release(&path
);
172 static int cmp_cache_name_compare(const void *a_
, const void *b_
)
174 const struct cache_entry
*ce1
, *ce2
;
176 ce1
= *((const struct cache_entry
**)a_
);
177 ce2
= *((const struct cache_entry
**)b_
);
178 return cache_name_compare(ce1
->name
, ce1
->ce_flags
,
179 ce2
->name
, ce2
->ce_flags
);
182 int read_tree(struct tree
*tree
, int stage
, const char **match
)
184 read_tree_fn_t fn
= NULL
;
188 * Currently the only existing callers of this function all
189 * call it with stage=1 and after making sure there is nothing
190 * at that stage; we could always use read_one_entry_quick().
192 * But when we decide to straighten out git-read-tree not to
193 * use unpack_trees() in some cases, this will probably start
198 * See if we have cache entry at the stage. If so,
199 * do it the original slow way, otherwise, append and then
202 for (i
= 0; !fn
&& i
< active_nr
; i
++) {
203 struct cache_entry
*ce
= active_cache
[i
];
204 if (ce_stage(ce
) == stage
)
209 fn
= read_one_entry_quick
;
210 err
= read_tree_recursive(tree
, "", 0, stage
, match
, fn
, NULL
);
211 if (fn
== read_one_entry
|| err
)
215 * Sort the cache entry -- we need to nuke the cache tree, though.
217 cache_tree_free(&active_cache_tree
);
218 qsort(active_cache
, active_nr
, sizeof(active_cache
[0]),
219 cmp_cache_name_compare
);
223 struct tree
*lookup_tree(const unsigned char *sha1
)
225 struct object
*obj
= lookup_object(sha1
);
227 return create_object(sha1
, OBJ_TREE
, alloc_tree_node());
229 obj
->type
= OBJ_TREE
;
230 if (obj
->type
!= OBJ_TREE
) {
231 error("Object %s is a %s, not a tree",
232 sha1_to_hex(sha1
), typename(obj
->type
));
235 return (struct tree
*) obj
;
238 int parse_tree_buffer(struct tree
*item
, void *buffer
, unsigned long size
)
240 if (item
->object
.parsed
)
242 item
->object
.parsed
= 1;
243 item
->buffer
= buffer
;
249 int parse_tree(struct tree
*item
)
251 enum object_type type
;
255 if (item
->object
.parsed
)
257 buffer
= read_sha1_file(item
->object
.sha1
, &type
, &size
);
259 return error("Could not read %s",
260 sha1_to_hex(item
->object
.sha1
));
261 if (type
!= OBJ_TREE
) {
263 return error("Object %s not a tree",
264 sha1_to_hex(item
->object
.sha1
));
266 return parse_tree_buffer(item
, buffer
, size
);
269 struct tree
*parse_tree_indirect(const unsigned char *sha1
)
271 struct object
*obj
= parse_object(sha1
);
275 if (obj
->type
== OBJ_TREE
)
276 return (struct tree
*) obj
;
277 else if (obj
->type
== OBJ_COMMIT
)
278 obj
= &(((struct commit
*) obj
)->tree
->object
);
279 else if (obj
->type
== OBJ_TAG
)
280 obj
= ((struct tag
*) obj
)->tagged
;
284 parse_object(obj
->sha1
);