decorate: add clear_decoration() function
[git.git] / tree.c
blobc745462f968ed11db2cca234c304dedb5b5c23ae
1 #include "git-compat-util.h"
2 #include "cache-tree.h"
3 #include "hex.h"
4 #include "tree.h"
5 #include "object-name.h"
6 #include "object-store-ll.h"
7 #include "blob.h"
8 #include "commit.h"
9 #include "tag.h"
10 #include "alloc.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;
23 struct object_id oid;
24 int len, oldlen = base->len;
25 enum interesting retval = entry_not_interesting;
27 if (parse_tree(tree))
28 return -1;
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,
35 base, pathspec);
36 if (retval == all_entries_not_interesting)
37 break;
38 if (retval == entry_not_interesting)
39 continue;
42 switch (fn(&entry.oid, base,
43 entry.path, entry.mode, context)) {
44 case 0:
45 continue;
46 case READ_TREE_RECURSIVE:
47 break;
48 default:
49 return -1;
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);
58 if (!commit)
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));
70 else
71 continue;
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),
77 base, pathspec,
78 fn, context);
79 strbuf_setlen(base, oldlen);
80 if (retval)
81 return -1;
83 return 0;
86 int read_tree(struct repository *r,
87 struct tree *tree,
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);
93 strbuf_release(&sb);
94 return ret;
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;
102 int cmp;
104 cmp = memcmp(name1, name2, len);
105 if (cmp)
106 return cmp;
107 c1 = name1[len];
108 c2 = name2[len];
109 if (!c1 && S_ISDIR(mode1))
110 c1 = '/';
111 if (!c2 && S_ISDIR(mode2))
112 c2 = '/';
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;
131 int cmp;
133 cmp = memcmp(name1, name2, len);
134 if (cmp)
135 return cmp;
136 /* Directories and files compare equal (same length, same name) */
137 if (len1 == len2)
138 return 0;
139 c1 = name1[len];
140 if (!c1 && S_ISDIR(mode1))
141 c1 = '/';
142 c2 = name2[len];
143 if (!c2 && S_ISDIR(mode2))
144 c2 = '/';
145 if (c1 == '/' && !c2)
146 return 0;
147 if (c2 == '/' && !c1)
148 return 0;
149 return c1 - c2;
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);
156 if (cmp)
157 return cmp;
158 if (len1 < len2)
159 return -1;
160 if (len1 > len2)
161 return 1;
162 return 0;
165 struct tree *lookup_tree(struct repository *r, const struct object_id *oid)
167 struct object *obj = lookup_object(r, oid);
168 if (!obj)
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)
176 return 0;
177 item->object.parsed = 1;
178 item->buffer = buffer;
179 item->size = size;
181 return 0;
184 int parse_tree_gently(struct tree *item, int quiet_on_missing)
186 enum object_type type;
187 void *buffer;
188 unsigned long size;
190 if (item->object.parsed)
191 return 0;
192 buffer = repo_read_object_file(the_repository, &item->object.oid,
193 &type, &size);
194 if (!buffer)
195 return quiet_on_missing ? -1 :
196 error("Could not read %s",
197 oid_to_hex(&item->object.oid));
198 if (type != OBJ_TREE) {
199 free(buffer);
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);
209 tree->size = 0;
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);