1 #include "git-compat-util.h"
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 base_name_compare(const char *name1
, size_t len1
, int mode1
,
98 const char *name2
, size_t len2
, int mode2
)
100 unsigned char c1
, c2
;
101 size_t len
= len1
< len2
? len1
: len2
;
104 cmp
= memcmp(name1
, name2
, len
);
109 if (!c1
&& S_ISDIR(mode1
))
111 if (!c2
&& S_ISDIR(mode2
))
113 return (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
117 * df_name_compare() is identical to base_name_compare(), except it
118 * compares conflicting directory/file entries as equal. Note that
119 * while a directory name compares as equal to a regular file, they
120 * then individually compare _differently_ to a filename that has
121 * a dot after the basename (because '\0' < '.' < '/').
123 * This is used by routines that want to traverse the git namespace
124 * but then handle conflicting entries together when possible.
126 int df_name_compare(const char *name1
, size_t len1
, int mode1
,
127 const char *name2
, size_t len2
, int mode2
)
129 unsigned char c1
, c2
;
130 size_t len
= len1
< len2
? len1
: len2
;
133 cmp
= memcmp(name1
, name2
, len
);
136 /* Directories and files compare equal (same length, same name) */
140 if (!c1
&& S_ISDIR(mode1
))
143 if (!c2
&& S_ISDIR(mode2
))
145 if (c1
== '/' && !c2
)
147 if (c2
== '/' && !c1
)
152 int name_compare(const char *name1
, size_t len1
, const char *name2
, size_t len2
)
154 size_t min_len
= (len1
< len2
) ? len1
: len2
;
155 int cmp
= memcmp(name1
, name2
, min_len
);
165 struct tree
*lookup_tree(struct repository
*r
, const struct object_id
*oid
)
167 struct object
*obj
= lookup_object(r
, oid
);
169 return create_object(r
, oid
, alloc_tree_node(r
));
170 return object_as_type(obj
, OBJ_TREE
, 0);
173 int parse_tree_buffer(struct tree
*item
, void *buffer
, unsigned long size
)
175 if (item
->object
.parsed
)
177 item
->object
.parsed
= 1;
178 item
->buffer
= buffer
;
184 int parse_tree_gently(struct tree
*item
, int quiet_on_missing
)
186 enum object_type type
;
190 if (item
->object
.parsed
)
192 buffer
= repo_read_object_file(the_repository
, &item
->object
.oid
,
195 return quiet_on_missing
? -1 :
196 error("Could not read %s",
197 oid_to_hex(&item
->object
.oid
));
198 if (type
!= OBJ_TREE
) {
200 return error("Object %s not a tree",
201 oid_to_hex(&item
->object
.oid
));
203 return parse_tree_buffer(item
, buffer
, size
);
206 void free_tree_buffer(struct tree
*tree
)
208 FREE_AND_NULL(tree
->buffer
);
210 tree
->object
.parsed
= 0;
213 struct tree
*parse_tree_indirect(const struct object_id
*oid
)
215 struct repository
*r
= the_repository
;
216 struct object
*obj
= parse_object(r
, oid
);
217 return (struct tree
*)repo_peel_to_type(r
, NULL
, 0, obj
, OBJ_TREE
);