1 #include "git-compat-util.h"
4 #include "object-name.h"
5 #include "object-store-ll.h"
9 #include "repository.h"
10 #include "environment.h"
12 const char *tree_type
= "tree";
14 int read_tree_at(struct repository
*r
,
15 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
;
26 if (depth
> max_allowed_tree_depth
)
27 return error("exceeded maximum allowed tree depth");
32 init_tree_desc(&desc
, &tree
->object
.oid
, tree
->buffer
, tree
->size
);
34 while (tree_entry(&desc
, &entry
)) {
35 if (retval
!= all_entries_interesting
) {
36 retval
= tree_entry_interesting(r
->index
, &entry
,
38 if (retval
== all_entries_not_interesting
)
40 if (retval
== entry_not_interesting
)
44 switch (fn(&entry
.oid
, base
,
45 entry
.path
, entry
.mode
, context
)) {
48 case READ_TREE_RECURSIVE
:
54 if (S_ISDIR(entry
.mode
))
55 oidcpy(&oid
, &entry
.oid
);
56 else if (S_ISGITLINK(entry
.mode
)) {
57 struct commit
*commit
;
59 commit
= lookup_commit(r
, &entry
.oid
);
61 die("Commit %s in submodule path %s%s not found",
62 oid_to_hex(&entry
.oid
),
63 base
->buf
, entry
.path
);
65 if (repo_parse_commit(r
, commit
))
66 die("Invalid commit %s in submodule path %s%s",
67 oid_to_hex(&entry
.oid
),
68 base
->buf
, entry
.path
);
70 oidcpy(&oid
, get_commit_tree_oid(commit
));
75 len
= tree_entry_len(&entry
);
76 strbuf_add(base
, entry
.path
, len
);
77 strbuf_addch(base
, '/');
78 retval
= read_tree_at(r
, lookup_tree(r
, &oid
),
79 base
, depth
+ 1, pathspec
,
81 strbuf_setlen(base
, oldlen
);
88 int read_tree(struct repository
*r
,
90 const struct pathspec
*pathspec
,
91 read_tree_fn_t fn
, void *context
)
93 struct strbuf sb
= STRBUF_INIT
;
94 int ret
= read_tree_at(r
, tree
, &sb
, 0, pathspec
, fn
, context
);
99 int base_name_compare(const char *name1
, size_t len1
, int mode1
,
100 const char *name2
, size_t len2
, int mode2
)
102 unsigned char c1
, c2
;
103 size_t len
= len1
< len2
? len1
: len2
;
106 cmp
= memcmp(name1
, name2
, len
);
111 if (!c1
&& S_ISDIR(mode1
))
113 if (!c2
&& S_ISDIR(mode2
))
115 return (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
119 * df_name_compare() is identical to base_name_compare(), except it
120 * compares conflicting directory/file entries as equal. Note that
121 * while a directory name compares as equal to a regular file, they
122 * then individually compare _differently_ to a filename that has
123 * a dot after the basename (because '\0' < '.' < '/').
125 * This is used by routines that want to traverse the git namespace
126 * but then handle conflicting entries together when possible.
128 int df_name_compare(const char *name1
, size_t len1
, int mode1
,
129 const char *name2
, size_t len2
, int mode2
)
131 unsigned char c1
, c2
;
132 size_t len
= len1
< len2
? len1
: len2
;
135 cmp
= memcmp(name1
, name2
, len
);
138 /* Directories and files compare equal (same length, same name) */
142 if (!c1
&& S_ISDIR(mode1
))
145 if (!c2
&& S_ISDIR(mode2
))
147 if (c1
== '/' && !c2
)
149 if (c2
== '/' && !c1
)
154 int name_compare(const char *name1
, size_t len1
, const char *name2
, size_t len2
)
156 size_t min_len
= (len1
< len2
) ? len1
: len2
;
157 int cmp
= memcmp(name1
, name2
, min_len
);
167 struct tree
*lookup_tree(struct repository
*r
, const struct object_id
*oid
)
169 struct object
*obj
= lookup_object(r
, oid
);
171 return create_object(r
, oid
, alloc_tree_node(r
));
172 return object_as_type(obj
, OBJ_TREE
, 0);
175 int parse_tree_buffer(struct tree
*item
, void *buffer
, unsigned long size
)
177 if (item
->object
.parsed
)
179 item
->object
.parsed
= 1;
180 item
->buffer
= buffer
;
186 int parse_tree_gently(struct tree
*item
, int quiet_on_missing
)
188 enum object_type type
;
192 if (item
->object
.parsed
)
194 buffer
= repo_read_object_file(the_repository
, &item
->object
.oid
,
197 return quiet_on_missing
? -1 :
198 error("Could not read %s",
199 oid_to_hex(&item
->object
.oid
));
200 if (type
!= OBJ_TREE
) {
202 return error("Object %s not a tree",
203 oid_to_hex(&item
->object
.oid
));
205 return parse_tree_buffer(item
, buffer
, size
);
208 void free_tree_buffer(struct tree
*tree
)
210 FREE_AND_NULL(tree
->buffer
);
212 tree
->object
.parsed
= 0;
215 struct tree
*parse_tree_indirect(const struct object_id
*oid
)
217 struct repository
*r
= the_repository
;
218 struct object
*obj
= parse_object(r
, oid
);
219 return (struct tree
*)repo_peel_to_type(r
, NULL
, 0, obj
, OBJ_TREE
);