2 #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 int read_tree_at(struct repository
*r
,
16 struct tree
*tree
, struct strbuf
*base
,
17 const struct pathspec
*pathspec
,
18 read_tree_fn_t fn
, void *context
)
20 struct tree_desc desc
;
21 struct name_entry entry
;
23 int len
, oldlen
= base
->len
;
24 enum interesting retval
= entry_not_interesting
;
29 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
31 while (tree_entry(&desc
, &entry
)) {
32 if (retval
!= all_entries_interesting
) {
33 retval
= tree_entry_interesting(r
->index
, &entry
,
35 if (retval
== all_entries_not_interesting
)
37 if (retval
== entry_not_interesting
)
41 switch (fn(&entry
.oid
, base
,
42 entry
.path
, entry
.mode
, context
)) {
45 case READ_TREE_RECURSIVE
:
51 if (S_ISDIR(entry
.mode
))
52 oidcpy(&oid
, &entry
.oid
);
53 else if (S_ISGITLINK(entry
.mode
)) {
54 struct commit
*commit
;
56 commit
= lookup_commit(r
, &entry
.oid
);
58 die("Commit %s in submodule path %s%s not found",
59 oid_to_hex(&entry
.oid
),
60 base
->buf
, entry
.path
);
62 if (parse_commit(commit
))
63 die("Invalid commit %s in submodule path %s%s",
64 oid_to_hex(&entry
.oid
),
65 base
->buf
, entry
.path
);
67 oidcpy(&oid
, get_commit_tree_oid(commit
));
72 len
= tree_entry_len(&entry
);
73 strbuf_add(base
, entry
.path
, len
);
74 strbuf_addch(base
, '/');
75 retval
= read_tree_at(r
, lookup_tree(r
, &oid
),
78 strbuf_setlen(base
, oldlen
);
85 int read_tree(struct repository
*r
,
87 const struct pathspec
*pathspec
,
88 read_tree_fn_t fn
, void *context
)
90 struct strbuf sb
= STRBUF_INIT
;
91 int ret
= read_tree_at(r
, tree
, &sb
, pathspec
, fn
, context
);
96 int cmp_cache_name_compare(const void *a_
, const void *b_
)
98 const struct cache_entry
*ce1
, *ce2
;
100 ce1
= *((const struct cache_entry
**)a_
);
101 ce2
= *((const struct cache_entry
**)b_
);
102 return cache_name_stage_compare(ce1
->name
, ce1
->ce_namelen
, ce_stage(ce1
),
103 ce2
->name
, ce2
->ce_namelen
, ce_stage(ce2
));
106 struct tree
*lookup_tree(struct repository
*r
, const struct object_id
*oid
)
108 struct object
*obj
= lookup_object(r
, oid
);
110 return create_object(r
, oid
, alloc_tree_node(r
));
111 return object_as_type(obj
, OBJ_TREE
, 0);
114 int parse_tree_buffer(struct tree
*item
, void *buffer
, unsigned long size
)
116 if (item
->object
.parsed
)
118 item
->object
.parsed
= 1;
119 item
->buffer
= buffer
;
125 int parse_tree_gently(struct tree
*item
, int quiet_on_missing
)
127 enum object_type type
;
131 if (item
->object
.parsed
)
133 buffer
= read_object_file(&item
->object
.oid
, &type
, &size
);
135 return quiet_on_missing
? -1 :
136 error("Could not read %s",
137 oid_to_hex(&item
->object
.oid
));
138 if (type
!= OBJ_TREE
) {
140 return error("Object %s not a tree",
141 oid_to_hex(&item
->object
.oid
));
143 return parse_tree_buffer(item
, buffer
, size
);
146 void free_tree_buffer(struct tree
*tree
)
148 FREE_AND_NULL(tree
->buffer
);
150 tree
->object
.parsed
= 0;
153 struct tree
*parse_tree_indirect(const struct object_id
*oid
)
155 struct repository
*r
= the_repository
;
156 struct object
*obj
= parse_object(r
, oid
);
157 return (struct tree
*)repo_peel_to_type(r
, NULL
, 0, obj
, OBJ_TREE
);