2 #include "cache-tree.h"
4 #include "object-store.h"
10 #include "repository.h"
12 const char *tree_type
= "tree";
14 int read_tree_at(struct repository
*r
,
15 struct tree
*tree
, struct strbuf
*base
,
16 const struct pathspec
*pathspec
,
17 read_tree_fn_t fn
, void *context
)
19 struct tree_desc desc
;
20 struct name_entry entry
;
22 int len
, oldlen
= base
->len
;
23 enum interesting retval
= entry_not_interesting
;
28 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
30 while (tree_entry(&desc
, &entry
)) {
31 if (retval
!= all_entries_interesting
) {
32 retval
= tree_entry_interesting(r
->index
, &entry
,
34 if (retval
== all_entries_not_interesting
)
36 if (retval
== entry_not_interesting
)
40 switch (fn(&entry
.oid
, base
,
41 entry
.path
, entry
.mode
, context
)) {
44 case READ_TREE_RECURSIVE
:
50 if (S_ISDIR(entry
.mode
))
51 oidcpy(&oid
, &entry
.oid
);
52 else if (S_ISGITLINK(entry
.mode
)) {
53 struct commit
*commit
;
55 commit
= lookup_commit(r
, &entry
.oid
);
57 die("Commit %s in submodule path %s%s not found",
58 oid_to_hex(&entry
.oid
),
59 base
->buf
, entry
.path
);
61 if (parse_commit(commit
))
62 die("Invalid commit %s in submodule path %s%s",
63 oid_to_hex(&entry
.oid
),
64 base
->buf
, entry
.path
);
66 oidcpy(&oid
, get_commit_tree_oid(commit
));
71 len
= tree_entry_len(&entry
);
72 strbuf_add(base
, entry
.path
, len
);
73 strbuf_addch(base
, '/');
74 retval
= read_tree_at(r
, lookup_tree(r
, &oid
),
77 strbuf_setlen(base
, oldlen
);
84 int read_tree(struct repository
*r
,
86 const struct pathspec
*pathspec
,
87 read_tree_fn_t fn
, void *context
)
89 struct strbuf sb
= STRBUF_INIT
;
90 int ret
= read_tree_at(r
, tree
, &sb
, pathspec
, fn
, context
);
95 int cmp_cache_name_compare(const void *a_
, const void *b_
)
97 const struct cache_entry
*ce1
, *ce2
;
99 ce1
= *((const struct cache_entry
**)a_
);
100 ce2
= *((const struct cache_entry
**)b_
);
101 return cache_name_stage_compare(ce1
->name
, ce1
->ce_namelen
, ce_stage(ce1
),
102 ce2
->name
, ce2
->ce_namelen
, ce_stage(ce2
));
105 struct tree
*lookup_tree(struct repository
*r
, const struct object_id
*oid
)
107 struct object
*obj
= lookup_object(r
, oid
);
109 return create_object(r
, oid
, alloc_tree_node(r
));
110 return object_as_type(obj
, OBJ_TREE
, 0);
113 int parse_tree_buffer(struct tree
*item
, void *buffer
, unsigned long size
)
115 if (item
->object
.parsed
)
117 item
->object
.parsed
= 1;
118 item
->buffer
= buffer
;
124 int parse_tree_gently(struct tree
*item
, int quiet_on_missing
)
126 enum object_type type
;
130 if (item
->object
.parsed
)
132 buffer
= read_object_file(&item
->object
.oid
, &type
, &size
);
134 return quiet_on_missing
? -1 :
135 error("Could not read %s",
136 oid_to_hex(&item
->object
.oid
));
137 if (type
!= OBJ_TREE
) {
139 return error("Object %s not a tree",
140 oid_to_hex(&item
->object
.oid
));
142 return parse_tree_buffer(item
, buffer
, size
);
145 void free_tree_buffer(struct tree
*tree
)
147 FREE_AND_NULL(tree
->buffer
);
149 tree
->object
.parsed
= 0;
152 struct tree
*parse_tree_indirect(const struct object_id
*oid
)
154 struct repository
*r
= the_repository
;
155 struct object
*obj
= parse_object(r
, oid
);
156 return (struct tree
*)repo_peel_to_type(r
, NULL
, 0, obj
, OBJ_TREE
);