]> git.ipfire.org Git - thirdparty/git.git/blob - tree.c
object-name.h: move declarations for object-name.c functions from cache.h
[thirdparty/git.git] / tree.c
1 #include "cache.h"
2 #include "cache-tree.h"
3 #include "hex.h"
4 #include "tree.h"
5 #include "object-name.h"
6 #include "object-store.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"
13
14 const char *tree_type = "tree";
15
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)
20 {
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;
26
27 if (parse_tree(tree))
28 return -1;
29
30 init_tree_desc(&desc, tree->buffer, tree->size);
31
32 while (tree_entry(&desc, &entry)) {
33 if (retval != all_entries_interesting) {
34 retval = tree_entry_interesting(r->index, &entry,
35 base, 0, pathspec);
36 if (retval == all_entries_not_interesting)
37 break;
38 if (retval == entry_not_interesting)
39 continue;
40 }
41
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;
50 }
51
52 if (S_ISDIR(entry.mode))
53 oidcpy(&oid, &entry.oid);
54 else if (S_ISGITLINK(entry.mode)) {
55 struct commit *commit;
56
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);
62
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);
67
68 oidcpy(&oid, get_commit_tree_oid(commit));
69 }
70 else
71 continue;
72
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;
82 }
83 return 0;
84 }
85
86 int read_tree(struct repository *r,
87 struct tree *tree,
88 const struct pathspec *pathspec,
89 read_tree_fn_t fn, void *context)
90 {
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;
95 }
96
97 int cmp_cache_name_compare(const void *a_, const void *b_)
98 {
99 const struct cache_entry *ce1, *ce2;
100
101 ce1 = *((const struct cache_entry **)a_);
102 ce2 = *((const struct cache_entry **)b_);
103 return cache_name_stage_compare(ce1->name, ce1->ce_namelen, ce_stage(ce1),
104 ce2->name, ce2->ce_namelen, ce_stage(ce2));
105 }
106
107 struct tree *lookup_tree(struct repository *r, const struct object_id *oid)
108 {
109 struct object *obj = lookup_object(r, oid);
110 if (!obj)
111 return create_object(r, oid, alloc_tree_node(r));
112 return object_as_type(obj, OBJ_TREE, 0);
113 }
114
115 int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
116 {
117 if (item->object.parsed)
118 return 0;
119 item->object.parsed = 1;
120 item->buffer = buffer;
121 item->size = size;
122
123 return 0;
124 }
125
126 int parse_tree_gently(struct tree *item, int quiet_on_missing)
127 {
128 enum object_type type;
129 void *buffer;
130 unsigned long size;
131
132 if (item->object.parsed)
133 return 0;
134 buffer = repo_read_object_file(the_repository, &item->object.oid,
135 &type, &size);
136 if (!buffer)
137 return quiet_on_missing ? -1 :
138 error("Could not read %s",
139 oid_to_hex(&item->object.oid));
140 if (type != OBJ_TREE) {
141 free(buffer);
142 return error("Object %s not a tree",
143 oid_to_hex(&item->object.oid));
144 }
145 return parse_tree_buffer(item, buffer, size);
146 }
147
148 void free_tree_buffer(struct tree *tree)
149 {
150 FREE_AND_NULL(tree->buffer);
151 tree->size = 0;
152 tree->object.parsed = 0;
153 }
154
155 struct tree *parse_tree_indirect(const struct object_id *oid)
156 {
157 struct repository *r = the_repository;
158 struct object *obj = parse_object(r, oid);
159 return (struct tree *)repo_peel_to_type(r, NULL, 0, obj, OBJ_TREE);
160 }