]> git.ipfire.org Git - thirdparty/git.git/blame - tree.c
Several trivial documentation touch ups.
[thirdparty/git.git] / tree.c
CommitLineData
8f1d2e6f 1#include "cache.h"
175785e5
DB
2#include "tree.h"
3#include "blob.h"
77675e2a
DB
4#include "commit.h"
5#include "tag.h"
175785e5
DB
6#include <stdlib.h>
7
8const char *tree_type = "tree";
9
94537c78
LT
10static int read_one_entry(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
11{
3c5e8468
LT
12 int len;
13 unsigned int size;
14 struct cache_entry *ce;
15
16 if (S_ISDIR(mode))
17 return READ_TREE_RECURSIVE;
18
19 len = strlen(pathname);
20 size = cache_entry_size(baselen + len);
90321c10 21 ce = xcalloc(1, size);
94537c78
LT
22
23 ce->ce_mode = create_ce_mode(mode);
24 ce->ce_flags = create_ce_flags(baselen + len, stage);
25 memcpy(ce->name, base, baselen);
26 memcpy(ce->name + baselen, pathname, len+1);
27 memcpy(ce->sha1, sha1, 20);
b155725d 28 return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
94537c78
LT
29}
30
3e587635 31static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths)
0ca14a57 32{
3e587635 33 const char *match;
0ca14a57
LT
34 int pathlen;
35
36 if (!paths)
37 return 1;
38 pathlen = strlen(path);
39 while ((match = *paths++) != NULL) {
40 int matchlen = strlen(match);
41
42 if (baselen >= matchlen) {
43 /* If it doesn't match, move along... */
44 if (strncmp(base, match, matchlen))
45 continue;
46 /* The base is a subdirectory of a path which was specified. */
47 return 1;
48 }
49
50 /* Does the base match? */
51 if (strncmp(base, match, baselen))
52 continue;
53
54 match += baselen;
55 matchlen -= baselen;
56
57 if (pathlen > matchlen)
58 continue;
59
60 if (matchlen > pathlen) {
61 if (match[pathlen] != '/')
62 continue;
63 if (!S_ISDIR(mode))
64 continue;
65 }
66
67 if (strncmp(path, match, pathlen))
68 continue;
3e587635
LT
69
70 return 1;
0ca14a57
LT
71 }
72 return 0;
73}
74
521698b1 75int read_tree_recursive(struct tree *tree,
3c5e8468
LT
76 const char *base, int baselen,
77 int stage, const char **match,
78 read_tree_fn_t fn)
94537c78 79{
521698b1
DB
80 struct tree_entry_list *list;
81 if (parse_tree(tree))
82 return -1;
83 list = tree->entries;
84 while (list) {
85 struct tree_entry_list *current = list;
86 list = list->next;
87 if (!match_tree_entry(base, baselen, current->name,
88 current->mode, match))
0ca14a57
LT
89 continue;
90
521698b1
DB
91 switch (fn(current->item.any->sha1, base, baselen,
92 current->name, current->mode, stage)) {
3c5e8468
LT
93 case 0:
94 continue;
95 case READ_TREE_RECURSIVE:
96 break;;
97 default:
98 return -1;
99 }
521698b1 100 if (current->directory) {
94537c78 101 int retval;
521698b1 102 int pathlen = strlen(current->name);
1c9da46d 103 char *newbase;
94537c78 104
1c9da46d 105 newbase = xmalloc(baselen + 1 + pathlen);
94537c78 106 memcpy(newbase, base, baselen);
521698b1 107 memcpy(newbase + baselen, current->name, pathlen);
94537c78 108 newbase[baselen + pathlen] = '/';
521698b1 109 retval = read_tree_recursive(current->item.tree,
94537c78 110 newbase,
0ca14a57 111 baselen + pathlen + 1,
3c5e8468 112 stage, match, fn);
94537c78
LT
113 free(newbase);
114 if (retval)
115 return -1;
116 continue;
117 }
94537c78
LT
118 }
119 return 0;
120}
121
521698b1 122int read_tree(struct tree *tree, int stage, const char **match)
94537c78 123{
521698b1 124 return read_tree_recursive(tree, "", 0, stage, match, read_one_entry);
94537c78
LT
125}
126
5d6ccf5c 127struct tree *lookup_tree(const unsigned char *sha1)
175785e5
DB
128{
129 struct object *obj = lookup_object(sha1);
130 if (!obj) {
90321c10 131 struct tree *ret = xcalloc(1, sizeof(struct tree));
175785e5 132 created_object(sha1, &ret->object);
d32987be 133 ret->object.type = tree_type;
175785e5
DB
134 return ret;
135 }
d1af002d
NP
136 if (!obj->type)
137 obj->type = tree_type;
c35dfe85 138 if (obj->type != tree_type) {
175785e5
DB
139 error("Object %s is a %s, not a tree",
140 sha1_to_hex(sha1), obj->type);
141 return NULL;
142 }
143 return (struct tree *) obj;
144}
145
bd2c39f5 146int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
175785e5 147{
bd2c39f5 148 void *bufptr = buffer;
08692164 149 struct tree_entry_list **list_p;
4a4e6fd7 150 int n_refs = 0;
bd2c39f5 151
175785e5
DB
152 if (item->object.parsed)
153 return 0;
154 item->object.parsed = 1;
08692164 155 list_p = &item->entries;
175785e5
DB
156 while (size) {
157 struct object *obj;
08692164 158 struct tree_entry_list *entry;
175785e5
DB
159 int len = 1+strlen(bufptr);
160 unsigned char *file_sha1 = bufptr + len;
161 char *path = strchr(bufptr, ' ');
162 unsigned int mode;
163 if (size < len + 20 || !path ||
bd2c39f5 164 sscanf(bufptr, "%o", &mode) != 1)
175785e5
DB
165 return -1;
166
812666c8 167 entry = xmalloc(sizeof(struct tree_entry_list));
08692164 168 entry->name = strdup(path + 1);
42ea9cb2
LT
169 entry->directory = S_ISDIR(mode) != 0;
170 entry->executable = (mode & S_IXUSR) != 0;
171 entry->symlink = S_ISLNK(mode) != 0;
64071805 172 entry->zeropad = *(char *)bufptr == '0';
42ea9cb2 173 entry->mode = mode;
08692164
DB
174 entry->next = NULL;
175
175785e5
DB
176 bufptr += len + 20;
177 size -= len + 20;
178
08692164
DB
179 if (entry->directory) {
180 entry->item.tree = lookup_tree(file_sha1);
181 obj = &entry->item.tree->object;
175785e5 182 } else {
08692164
DB
183 entry->item.blob = lookup_blob(file_sha1);
184 obj = &entry->item.blob->object;
175785e5 185 }
235ac407 186 if (obj)
4a4e6fd7 187 n_refs++;
08692164
DB
188 *list_p = entry;
189 list_p = &entry->next;
175785e5 190 }
4a4e6fd7
SV
191
192 if (track_object_refs) {
193 struct tree_entry_list *entry;
194 unsigned i = 0;
195 struct object_refs *refs = alloc_object_refs(n_refs);
196 for (entry = item->entries; entry; entry = entry->next)
197 refs->ref[i++] = entry->item.any;
198 set_object_refs(&item->object, refs);
199 }
200
175785e5
DB
201 return 0;
202}
bd2c39f5
NP
203
204int parse_tree(struct tree *item)
205{
206 char type[20];
207 void *buffer;
208 unsigned long size;
209 int ret;
210
211 if (item->object.parsed)
212 return 0;
213 buffer = read_sha1_file(item->object.sha1, type, &size);
214 if (!buffer)
215 return error("Could not read %s",
216 sha1_to_hex(item->object.sha1));
217 if (strcmp(type, tree_type)) {
218 free(buffer);
219 return error("Object %s not a tree",
220 sha1_to_hex(item->object.sha1));
221 }
222 ret = parse_tree_buffer(item, buffer, size);
223 free(buffer);
224 return ret;
225}
77675e2a
DB
226
227struct tree *parse_tree_indirect(const unsigned char *sha1)
228{
229 struct object *obj = parse_object(sha1);
230 do {
231 if (!obj)
232 return NULL;
233 if (obj->type == tree_type)
234 return (struct tree *) obj;
235 else if (obj->type == commit_type)
236 obj = &(((struct commit *) obj)->tree->object);
237 else if (obj->type == tag_type)
238 obj = ((struct tag *) obj)->tagged;
239 else
240 return NULL;
241 if (!obj->parsed)
242 parse_object(obj->sha1);
243 } while (1);
244}