t-ctype: avoid duplicating class names
[git.git] / tree.c
blob508e5fd76fd5bb588796bb9c51a78557880ba074
1 #include "git-compat-util.h"
2 #include "hex.h"
3 #include "tree.h"
4 #include "object-name.h"
5 #include "object-store-ll.h"
6 #include "commit.h"
7 #include "alloc.h"
8 #include "tree-walk.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,
16 int depth,
17 const struct pathspec *pathspec,
18 read_tree_fn_t fn, void *context)
20 struct tree_desc desc;
21 struct name_entry entry;
22 struct object_id oid;
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");
29 if (parse_tree(tree))
30 return -1;
32 init_tree_desc(&desc, tree->buffer, tree->size);
34 while (tree_entry(&desc, &entry)) {
35 if (retval != all_entries_interesting) {
36 retval = tree_entry_interesting(r->index, &entry,
37 base, pathspec);
38 if (retval == all_entries_not_interesting)
39 break;
40 if (retval == entry_not_interesting)
41 continue;
44 switch (fn(&entry.oid, base,
45 entry.path, entry.mode, context)) {
46 case 0:
47 continue;
48 case READ_TREE_RECURSIVE:
49 break;
50 default:
51 return -1;
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);
60 if (!commit)
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));
72 else
73 continue;
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,
80 fn, context);
81 strbuf_setlen(base, oldlen);
82 if (retval)
83 return -1;
85 return 0;
88 int read_tree(struct repository *r,
89 struct tree *tree,
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);
95 strbuf_release(&sb);
96 return ret;
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;
104 int cmp;
106 cmp = memcmp(name1, name2, len);
107 if (cmp)
108 return cmp;
109 c1 = name1[len];
110 c2 = name2[len];
111 if (!c1 && S_ISDIR(mode1))
112 c1 = '/';
113 if (!c2 && S_ISDIR(mode2))
114 c2 = '/';
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;
133 int cmp;
135 cmp = memcmp(name1, name2, len);
136 if (cmp)
137 return cmp;
138 /* Directories and files compare equal (same length, same name) */
139 if (len1 == len2)
140 return 0;
141 c1 = name1[len];
142 if (!c1 && S_ISDIR(mode1))
143 c1 = '/';
144 c2 = name2[len];
145 if (!c2 && S_ISDIR(mode2))
146 c2 = '/';
147 if (c1 == '/' && !c2)
148 return 0;
149 if (c2 == '/' && !c1)
150 return 0;
151 return c1 - c2;
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);
158 if (cmp)
159 return cmp;
160 if (len1 < len2)
161 return -1;
162 if (len1 > len2)
163 return 1;
164 return 0;
167 struct tree *lookup_tree(struct repository *r, const struct object_id *oid)
169 struct object *obj = lookup_object(r, oid);
170 if (!obj)
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)
178 return 0;
179 item->object.parsed = 1;
180 item->buffer = buffer;
181 item->size = size;
183 return 0;
186 int parse_tree_gently(struct tree *item, int quiet_on_missing)
188 enum object_type type;
189 void *buffer;
190 unsigned long size;
192 if (item->object.parsed)
193 return 0;
194 buffer = repo_read_object_file(the_repository, &item->object.oid,
195 &type, &size);
196 if (!buffer)
197 return quiet_on_missing ? -1 :
198 error("Could not read %s",
199 oid_to_hex(&item->object.oid));
200 if (type != OBJ_TREE) {
201 free(buffer);
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);
211 tree->size = 0;
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);