2 #include "cache-tree.h"
5 #include "object-name.h"
6 #include "object-store.h"
11 #include "tree-walk.h"
12 #include "repository.h"
14 const char *tree_type
= "tree";
16 int read_tree_at(struct repository
*r
,
17 struct tree
*tree
, struct strbuf
*base
,
18 const struct pathspec
*pathspec
,
19 read_tree_fn_t fn
, void *context
)
21 struct tree_desc desc
;
22 struct name_entry entry
;
24 int len
, oldlen
= base
->len
;
25 enum interesting retval
= entry_not_interesting
;
30 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
32 while (tree_entry(&desc
, &entry
)) {
33 if (retval
!= all_entries_interesting
) {
34 retval
= tree_entry_interesting(r
->index
, &entry
,
36 if (retval
== all_entries_not_interesting
)
38 if (retval
== entry_not_interesting
)
42 switch (fn(&entry
.oid
, base
,
43 entry
.path
, entry
.mode
, context
)) {
46 case READ_TREE_RECURSIVE
:
52 if (S_ISDIR(entry
.mode
))
53 oidcpy(&oid
, &entry
.oid
);
54 else if (S_ISGITLINK(entry
.mode
)) {
55 struct commit
*commit
;
57 commit
= lookup_commit(r
, &entry
.oid
);
59 die("Commit %s in submodule path %s%s not found",
60 oid_to_hex(&entry
.oid
),
61 base
->buf
, entry
.path
);
63 if (repo_parse_commit(r
, commit
))
64 die("Invalid commit %s in submodule path %s%s",
65 oid_to_hex(&entry
.oid
),
66 base
->buf
, entry
.path
);
68 oidcpy(&oid
, get_commit_tree_oid(commit
));
73 len
= tree_entry_len(&entry
);
74 strbuf_add(base
, entry
.path
, len
);
75 strbuf_addch(base
, '/');
76 retval
= read_tree_at(r
, lookup_tree(r
, &oid
),
79 strbuf_setlen(base
, oldlen
);
86 int read_tree(struct repository
*r
,
88 const struct pathspec
*pathspec
,
89 read_tree_fn_t fn
, void *context
)
91 struct strbuf sb
= STRBUF_INIT
;
92 int ret
= read_tree_at(r
, tree
, &sb
, pathspec
, fn
, context
);
97 int cmp_cache_name_compare(const void *a_
, const void *b_
)
99 const struct cache_entry
*ce1
, *ce2
;
101 ce1
= *((const struct cache_entry
**)a_
);
102 ce2
= *((const struct cache_entry
**)b_
);
103 return cache_name_stage_compare(ce1
->name
, ce1
->ce_namelen
, ce_stage(ce1
),
104 ce2
->name
, ce2
->ce_namelen
, ce_stage(ce2
));
107 struct tree
*lookup_tree(struct repository
*r
, const struct object_id
*oid
)
109 struct object
*obj
= lookup_object(r
, oid
);
111 return create_object(r
, oid
, alloc_tree_node(r
));
112 return object_as_type(obj
, OBJ_TREE
, 0);
115 int parse_tree_buffer(struct tree
*item
, void *buffer
, unsigned long size
)
117 if (item
->object
.parsed
)
119 item
->object
.parsed
= 1;
120 item
->buffer
= buffer
;
126 int parse_tree_gently(struct tree
*item
, int quiet_on_missing
)
128 enum object_type type
;
132 if (item
->object
.parsed
)
134 buffer
= repo_read_object_file(the_repository
, &item
->object
.oid
,
137 return quiet_on_missing
? -1 :
138 error("Could not read %s",
139 oid_to_hex(&item
->object
.oid
));
140 if (type
!= OBJ_TREE
) {
142 return error("Object %s not a tree",
143 oid_to_hex(&item
->object
.oid
));
145 return parse_tree_buffer(item
, buffer
, size
);
148 void free_tree_buffer(struct tree
*tree
)
150 FREE_AND_NULL(tree
->buffer
);
152 tree
->object
.parsed
= 0;
155 struct tree
*parse_tree_indirect(const struct object_id
*oid
)
157 struct repository
*r
= the_repository
;
158 struct object
*obj
= parse_object(r
, oid
);
159 return (struct tree
*)repo_peel_to_type(r
, NULL
, 0, obj
, OBJ_TREE
);