1 #include "git-compat-util.h"
2 #include "cache-tree.h"
5 #include "object-name.h"
6 #include "object-store-ll.h"
11 #include "tree-walk.h"
12 #include "repository.h"
13 #include "environment.h"
15 const char *tree_type
= "tree";
17 int read_tree_at(struct repository
*r
,
18 struct tree
*tree
, struct strbuf
*base
,
20 const struct pathspec
*pathspec
,
21 read_tree_fn_t fn
, void *context
)
23 struct tree_desc desc
;
24 struct name_entry entry
;
26 int len
, oldlen
= base
->len
;
27 enum interesting retval
= entry_not_interesting
;
29 if (depth
> max_allowed_tree_depth
)
30 return error("exceeded maximum allowed tree depth");
35 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
37 while (tree_entry(&desc
, &entry
)) {
38 if (retval
!= all_entries_interesting
) {
39 retval
= tree_entry_interesting(r
->index
, &entry
,
41 if (retval
== all_entries_not_interesting
)
43 if (retval
== entry_not_interesting
)
47 switch (fn(&entry
.oid
, base
,
48 entry
.path
, entry
.mode
, context
)) {
51 case READ_TREE_RECURSIVE
:
57 if (S_ISDIR(entry
.mode
))
58 oidcpy(&oid
, &entry
.oid
);
59 else if (S_ISGITLINK(entry
.mode
)) {
60 struct commit
*commit
;
62 commit
= lookup_commit(r
, &entry
.oid
);
64 die("Commit %s in submodule path %s%s not found",
65 oid_to_hex(&entry
.oid
),
66 base
->buf
, entry
.path
);
68 if (repo_parse_commit(r
, commit
))
69 die("Invalid commit %s in submodule path %s%s",
70 oid_to_hex(&entry
.oid
),
71 base
->buf
, entry
.path
);
73 oidcpy(&oid
, get_commit_tree_oid(commit
));
78 len
= tree_entry_len(&entry
);
79 strbuf_add(base
, entry
.path
, len
);
80 strbuf_addch(base
, '/');
81 retval
= read_tree_at(r
, lookup_tree(r
, &oid
),
82 base
, depth
+ 1, pathspec
,
84 strbuf_setlen(base
, oldlen
);
91 int read_tree(struct repository
*r
,
93 const struct pathspec
*pathspec
,
94 read_tree_fn_t fn
, void *context
)
96 struct strbuf sb
= STRBUF_INIT
;
97 int ret
= read_tree_at(r
, tree
, &sb
, 0, pathspec
, fn
, context
);
102 int base_name_compare(const char *name1
, size_t len1
, int mode1
,
103 const char *name2
, size_t len2
, int mode2
)
105 unsigned char c1
, c2
;
106 size_t len
= len1
< len2
? len1
: len2
;
109 cmp
= memcmp(name1
, name2
, len
);
114 if (!c1
&& S_ISDIR(mode1
))
116 if (!c2
&& S_ISDIR(mode2
))
118 return (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
122 * df_name_compare() is identical to base_name_compare(), except it
123 * compares conflicting directory/file entries as equal. Note that
124 * while a directory name compares as equal to a regular file, they
125 * then individually compare _differently_ to a filename that has
126 * a dot after the basename (because '\0' < '.' < '/').
128 * This is used by routines that want to traverse the git namespace
129 * but then handle conflicting entries together when possible.
131 int df_name_compare(const char *name1
, size_t len1
, int mode1
,
132 const char *name2
, size_t len2
, int mode2
)
134 unsigned char c1
, c2
;
135 size_t len
= len1
< len2
? len1
: len2
;
138 cmp
= memcmp(name1
, name2
, len
);
141 /* Directories and files compare equal (same length, same name) */
145 if (!c1
&& S_ISDIR(mode1
))
148 if (!c2
&& S_ISDIR(mode2
))
150 if (c1
== '/' && !c2
)
152 if (c2
== '/' && !c1
)
157 int name_compare(const char *name1
, size_t len1
, const char *name2
, size_t len2
)
159 size_t min_len
= (len1
< len2
) ? len1
: len2
;
160 int cmp
= memcmp(name1
, name2
, min_len
);
170 struct tree
*lookup_tree(struct repository
*r
, const struct object_id
*oid
)
172 struct object
*obj
= lookup_object(r
, oid
);
174 return create_object(r
, oid
, alloc_tree_node(r
));
175 return object_as_type(obj
, OBJ_TREE
, 0);
178 int parse_tree_buffer(struct tree
*item
, void *buffer
, unsigned long size
)
180 if (item
->object
.parsed
)
182 item
->object
.parsed
= 1;
183 item
->buffer
= buffer
;
189 int parse_tree_gently(struct tree
*item
, int quiet_on_missing
)
191 enum object_type type
;
195 if (item
->object
.parsed
)
197 buffer
= repo_read_object_file(the_repository
, &item
->object
.oid
,
200 return quiet_on_missing
? -1 :
201 error("Could not read %s",
202 oid_to_hex(&item
->object
.oid
));
203 if (type
!= OBJ_TREE
) {
205 return error("Object %s not a tree",
206 oid_to_hex(&item
->object
.oid
));
208 return parse_tree_buffer(item
, buffer
, size
);
211 void free_tree_buffer(struct tree
*tree
)
213 FREE_AND_NULL(tree
->buffer
);
215 tree
->object
.parsed
= 0;
218 struct tree
*parse_tree_indirect(const struct object_id
*oid
)
220 struct repository
*r
= the_repository
;
221 struct object
*obj
= parse_object(r
, oid
);
222 return (struct tree
*)repo_peel_to_type(r
, NULL
, 0, obj
, OBJ_TREE
);