1 #define NO_THE_INDEX_COMPATIBILITY_MACROS
3 #include "cache-tree.h"
5 #include "object-store.h"
10 #include "tree-walk.h"
11 #include "repository.h"
13 const char *tree_type
= "tree";
15 static int read_one_entry_opt(struct index_state
*istate
,
16 const struct object_id
*oid
,
17 const char *base
, int baselen
,
19 unsigned mode
, int stage
, int opt
)
23 struct cache_entry
*ce
;
26 return READ_TREE_RECURSIVE
;
28 len
= strlen(pathname
);
29 size
= cache_entry_size(baselen
+ len
);
30 ce
= xcalloc(1, size
);
32 ce
->ce_mode
= create_ce_mode(mode
);
33 ce
->ce_flags
= create_ce_flags(stage
);
34 ce
->ce_namelen
= baselen
+ len
;
35 memcpy(ce
->name
, base
, baselen
);
36 memcpy(ce
->name
+ baselen
, pathname
, len
+1);
37 oidcpy(&ce
->oid
, oid
);
38 return add_index_entry(istate
, ce
, opt
);
41 static int read_one_entry(const struct object_id
*oid
, struct strbuf
*base
,
42 const char *pathname
, unsigned mode
, int stage
,
45 struct index_state
*istate
= context
;
46 return read_one_entry_opt(istate
, oid
, base
->buf
, base
->len
, pathname
,
48 ADD_CACHE_OK_TO_ADD
|ADD_CACHE_SKIP_DFCHECK
);
52 * This is used when the caller knows there is no existing entries at
53 * the stage that will conflict with the entry being added.
55 static int read_one_entry_quick(const struct object_id
*oid
, struct strbuf
*base
,
56 const char *pathname
, unsigned mode
, int stage
,
59 struct index_state
*istate
= context
;
60 return read_one_entry_opt(istate
, oid
, base
->buf
, base
->len
, pathname
,
62 ADD_CACHE_JUST_APPEND
);
65 static int read_tree_1(struct tree
*tree
, struct strbuf
*base
,
66 int stage
, const struct pathspec
*pathspec
,
67 read_tree_fn_t fn
, void *context
)
69 struct tree_desc desc
;
70 struct name_entry entry
;
72 int len
, oldlen
= base
->len
;
73 enum interesting retval
= entry_not_interesting
;
78 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
80 while (tree_entry(&desc
, &entry
)) {
81 if (retval
!= all_entries_interesting
) {
82 retval
= tree_entry_interesting(&entry
, base
, 0, pathspec
);
83 if (retval
== all_entries_not_interesting
)
85 if (retval
== entry_not_interesting
)
89 switch (fn(entry
.oid
, base
,
90 entry
.path
, entry
.mode
, stage
, context
)) {
93 case READ_TREE_RECURSIVE
:
99 if (S_ISDIR(entry
.mode
))
100 oidcpy(&oid
, entry
.oid
);
101 else if (S_ISGITLINK(entry
.mode
)) {
102 struct commit
*commit
;
104 commit
= lookup_commit(the_repository
, entry
.oid
);
106 die("Commit %s in submodule path %s%s not found",
107 oid_to_hex(entry
.oid
),
108 base
->buf
, entry
.path
);
110 if (parse_commit(commit
))
111 die("Invalid commit %s in submodule path %s%s",
112 oid_to_hex(entry
.oid
),
113 base
->buf
, entry
.path
);
115 oidcpy(&oid
, get_commit_tree_oid(commit
));
120 len
= tree_entry_len(&entry
);
121 strbuf_add(base
, entry
.path
, len
);
122 strbuf_addch(base
, '/');
123 retval
= read_tree_1(lookup_tree(the_repository
, &oid
),
124 base
, stage
, pathspec
,
126 strbuf_setlen(base
, oldlen
);
133 int read_tree_recursive(struct tree
*tree
,
134 const char *base
, int baselen
,
135 int stage
, const struct pathspec
*pathspec
,
136 read_tree_fn_t fn
, void *context
)
138 struct strbuf sb
= STRBUF_INIT
;
141 strbuf_add(&sb
, base
, baselen
);
142 ret
= read_tree_1(tree
, &sb
, stage
, pathspec
, fn
, context
);
147 static int cmp_cache_name_compare(const void *a_
, const void *b_
)
149 const struct cache_entry
*ce1
, *ce2
;
151 ce1
= *((const struct cache_entry
**)a_
);
152 ce2
= *((const struct cache_entry
**)b_
);
153 return cache_name_stage_compare(ce1
->name
, ce1
->ce_namelen
, ce_stage(ce1
),
154 ce2
->name
, ce2
->ce_namelen
, ce_stage(ce2
));
157 int read_tree(struct tree
*tree
, int stage
, struct pathspec
*match
,
158 struct index_state
*istate
)
160 read_tree_fn_t fn
= NULL
;
164 * Currently the only existing callers of this function all
165 * call it with stage=1 and after making sure there is nothing
166 * at that stage; we could always use read_one_entry_quick().
168 * But when we decide to straighten out git-read-tree not to
169 * use unpack_trees() in some cases, this will probably start
174 * See if we have cache entry at the stage. If so,
175 * do it the original slow way, otherwise, append and then
178 for (i
= 0; !fn
&& i
< istate
->cache_nr
; i
++) {
179 const struct cache_entry
*ce
= istate
->cache
[i
];
180 if (ce_stage(ce
) == stage
)
185 fn
= read_one_entry_quick
;
186 err
= read_tree_recursive(tree
, "", 0, stage
, match
, fn
, istate
);
187 if (fn
== read_one_entry
|| err
)
191 * Sort the cache entry -- we need to nuke the cache tree, though.
193 cache_tree_free(&istate
->cache_tree
);
194 QSORT(istate
->cache
, istate
->cache_nr
, cmp_cache_name_compare
);
198 struct tree
*lookup_tree(struct repository
*r
, const struct object_id
*oid
)
200 struct object
*obj
= lookup_object(r
, oid
->hash
);
202 return create_object(r
, oid
->hash
,
204 return object_as_type(r
, obj
, OBJ_TREE
, 0);
207 int parse_tree_buffer(struct tree
*item
, void *buffer
, unsigned long size
)
209 if (item
->object
.parsed
)
211 item
->object
.parsed
= 1;
212 item
->buffer
= buffer
;
218 int parse_tree_gently(struct tree
*item
, int quiet_on_missing
)
220 enum object_type type
;
224 if (item
->object
.parsed
)
226 buffer
= read_object_file(&item
->object
.oid
, &type
, &size
);
228 return quiet_on_missing
? -1 :
229 error("Could not read %s",
230 oid_to_hex(&item
->object
.oid
));
231 if (type
!= OBJ_TREE
) {
233 return error("Object %s not a tree",
234 oid_to_hex(&item
->object
.oid
));
236 return parse_tree_buffer(item
, buffer
, size
);
239 void free_tree_buffer(struct tree
*tree
)
241 FREE_AND_NULL(tree
->buffer
);
243 tree
->object
.parsed
= 0;
246 struct tree
*parse_tree_indirect(const struct object_id
*oid
)
248 struct object
*obj
= parse_object(the_repository
, oid
);
252 if (obj
->type
== OBJ_TREE
)
253 return (struct tree
*) obj
;
254 else if (obj
->type
== OBJ_COMMIT
)
255 obj
= &(get_commit_tree(((struct commit
*)obj
))->object
);
256 else if (obj
->type
== OBJ_TAG
)
257 obj
= ((struct tag
*) obj
)->tagged
;
261 parse_object(the_repository
, &obj
->oid
);