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 read_tree_1(struct tree
*tree
, struct strbuf
*base
,
49 int stage
, struct pathspec
*pathspec
,
50 read_tree_fn_t fn
, void *context
)
52 struct tree_desc desc
;
53 struct name_entry entry
;
54 unsigned char sha1
[20];
55 int len
, oldlen
= base
->len
;
56 enum interesting retval
= entry_not_interesting
;
61 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
63 while (tree_entry(&desc
, &entry
)) {
64 if (retval
!= all_entries_interesting
) {
65 retval
= tree_entry_interesting(&entry
, base
, 0, pathspec
);
66 if (retval
== all_entries_not_interesting
)
68 if (retval
== entry_not_interesting
)
72 switch (fn(entry
.sha1
, base
->buf
, base
->len
,
73 entry
.path
, entry
.mode
, stage
, context
)) {
76 case READ_TREE_RECURSIVE
:
82 if (S_ISDIR(entry
.mode
))
83 hashcpy(sha1
, entry
.sha1
);
84 else if (S_ISGITLINK(entry
.mode
)) {
85 struct commit
*commit
;
87 commit
= lookup_commit(entry
.sha1
);
89 die("Commit %s in submodule path %s%s not found",
90 sha1_to_hex(entry
.sha1
),
91 base
->buf
, entry
.path
);
93 if (parse_commit(commit
))
94 die("Invalid commit %s in submodule path %s%s",
95 sha1_to_hex(entry
.sha1
),
96 base
->buf
, entry
.path
);
98 hashcpy(sha1
, commit
->tree
->object
.sha1
);
103 len
= tree_entry_len(&entry
);
104 strbuf_add(base
, entry
.path
, len
);
105 strbuf_addch(base
, '/');
106 retval
= read_tree_1(lookup_tree(sha1
),
107 base
, stage
, pathspec
,
109 strbuf_setlen(base
, oldlen
);
116 int read_tree_recursive(struct tree
*tree
,
117 const char *base
, int baselen
,
118 int stage
, struct pathspec
*pathspec
,
119 read_tree_fn_t fn
, void *context
)
121 struct strbuf sb
= STRBUF_INIT
;
124 strbuf_add(&sb
, base
, baselen
);
125 ret
= read_tree_1(tree
, &sb
, stage
, pathspec
, fn
, context
);
130 static int cmp_cache_name_compare(const void *a_
, const void *b_
)
132 const struct cache_entry
*ce1
, *ce2
;
134 ce1
= *((const struct cache_entry
**)a_
);
135 ce2
= *((const struct cache_entry
**)b_
);
136 return cache_name_compare(ce1
->name
, ce1
->ce_flags
,
137 ce2
->name
, ce2
->ce_flags
);
140 int read_tree(struct tree
*tree
, int stage
, struct pathspec
*match
)
142 read_tree_fn_t fn
= NULL
;
146 * Currently the only existing callers of this function all
147 * call it with stage=1 and after making sure there is nothing
148 * at that stage; we could always use read_one_entry_quick().
150 * But when we decide to straighten out git-read-tree not to
151 * use unpack_trees() in some cases, this will probably start
156 * See if we have cache entry at the stage. If so,
157 * do it the original slow way, otherwise, append and then
160 for (i
= 0; !fn
&& i
< active_nr
; i
++) {
161 struct cache_entry
*ce
= active_cache
[i
];
162 if (ce_stage(ce
) == stage
)
167 fn
= read_one_entry_quick
;
168 err
= read_tree_recursive(tree
, "", 0, stage
, match
, fn
, NULL
);
169 if (fn
== read_one_entry
|| err
)
173 * Sort the cache entry -- we need to nuke the cache tree, though.
175 cache_tree_free(&active_cache_tree
);
176 qsort(active_cache
, active_nr
, sizeof(active_cache
[0]),
177 cmp_cache_name_compare
);
181 struct tree
*lookup_tree(const unsigned char *sha1
)
183 struct object
*obj
= lookup_object(sha1
);
185 return create_object(sha1
, OBJ_TREE
, alloc_tree_node());
187 obj
->type
= OBJ_TREE
;
188 if (obj
->type
!= OBJ_TREE
) {
189 error("Object %s is a %s, not a tree",
190 sha1_to_hex(sha1
), typename(obj
->type
));
193 return (struct tree
*) obj
;
196 int parse_tree_buffer(struct tree
*item
, void *buffer
, unsigned long size
)
198 if (item
->object
.parsed
)
200 item
->object
.parsed
= 1;
201 item
->buffer
= buffer
;
207 int parse_tree(struct tree
*item
)
209 enum object_type type
;
213 if (item
->object
.parsed
)
215 buffer
= read_sha1_file(item
->object
.sha1
, &type
, &size
);
217 return error("Could not read %s",
218 sha1_to_hex(item
->object
.sha1
));
219 if (type
!= OBJ_TREE
) {
221 return error("Object %s not a tree",
222 sha1_to_hex(item
->object
.sha1
));
224 return parse_tree_buffer(item
, buffer
, size
);
227 struct tree
*parse_tree_indirect(const unsigned char *sha1
)
229 struct object
*obj
= parse_object(sha1
);
233 if (obj
->type
== OBJ_TREE
)
234 return (struct tree
*) obj
;
235 else if (obj
->type
== OBJ_COMMIT
)
236 obj
= &(((struct commit
*) obj
)->tree
->object
);
237 else if (obj
->type
== OBJ_TAG
)
238 obj
= ((struct tag
*) obj
)->tagged
;
242 parse_object(obj
->sha1
);