🌐 AI搜索 & 代理 主页
blob: b6f02fecc46ec4633dc1ee75f38bc90761a4fbe3 [file] [log] [blame]
Junio C Hamano8f1d2e62006-01-07 09:33:541#include "cache.h"
Daniel Barkalow175785e2005-04-18 18:39:482#include "tree.h"
3#include "blob.h"
Daniel Barkalow77675e22005-09-05 06:03:514#include "commit.h"
5#include "tag.h"
Linus Torvalds136f2e52006-05-29 19:16:126#include "tree-walk.h"
Daniel Barkalow175785e2005-04-18 18:39:487
8const char *tree_type = "tree";
9
Linus Torvalds3a7c3522006-05-29 19:16:4610static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
Linus Torvalds94537c72005-04-22 23:42:3711{
Linus Torvalds3c5e8462005-11-26 17:38:2012 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);
Peter Eriksen90321c12006-04-03 18:30:4621 ce = xcalloc(1, size);
Linus Torvalds94537c72005-04-22 23:42:3722
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);
Shawn Pearcee7024962006-08-23 06:49:0027 hashcpy(ce->sha1, sha1);
Junio C Hamanob1557252005-06-25 09:25:2928 return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
Linus Torvalds94537c72005-04-22 23:42:3729}
30
Linus Torvalds3e587632005-07-14 18:39:2731static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths)
Linus Torvalds0ca14a52005-07-14 18:26:3132{
Linus Torvalds3e587632005-07-14 18:39:2733 const char *match;
Linus Torvalds0ca14a52005-07-14 18:26:3134 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;
Linus Torvalds3e587632005-07-14 18:39:2769
70 return 1;
Linus Torvalds0ca14a52005-07-14 18:26:3171 }
72 return 0;
73}
74
Daniel Barkalow521698b2006-01-26 06:13:3675int read_tree_recursive(struct tree *tree,
Linus Torvalds3c5e8462005-11-26 17:38:2076 const char *base, int baselen,
77 int stage, const char **match,
78 read_tree_fn_t fn)
Linus Torvalds94537c72005-04-22 23:42:3779{
Linus Torvalds0790a422006-05-29 19:17:2880 struct tree_desc desc;
Linus Torvalds4c068a92006-05-30 16:45:4581 struct name_entry entry;
Linus Torvalds0790a422006-05-29 19:17:2882
Daniel Barkalow521698b2006-01-26 06:13:3683 if (parse_tree(tree))
84 return -1;
Linus Torvalds0790a422006-05-29 19:17:2885
86 desc.buf = tree->buffer;
87 desc.size = tree->size;
88
Linus Torvalds4c068a92006-05-30 16:45:4589 while (tree_entry(&desc, &entry)) {
90 if (!match_tree_entry(base, baselen, entry.path, entry.mode, match))
Linus Torvalds0ca14a52005-07-14 18:26:3191 continue;
92
Linus Torvalds4c068a92006-05-30 16:45:4593 switch (fn(entry.sha1, base, baselen, entry.path, entry.mode, stage)) {
Linus Torvalds3c5e8462005-11-26 17:38:2094 case 0:
95 continue;
96 case READ_TREE_RECURSIVE:
97 break;;
98 default:
99 return -1;
100 }
Linus Torvalds4c068a92006-05-30 16:45:45101 if (S_ISDIR(entry.mode)) {
Linus Torvalds94537c72005-04-22 23:42:37102 int retval;
Jonas Fonseca1c9da462005-04-26 22:00:01103 char *newbase;
Linus Torvalds94537c72005-04-22 23:42:37104
Linus Torvalds4c068a92006-05-30 16:45:45105 newbase = xmalloc(baselen + 1 + entry.pathlen);
Linus Torvalds94537c72005-04-22 23:42:37106 memcpy(newbase, base, baselen);
Linus Torvalds4c068a92006-05-30 16:45:45107 memcpy(newbase + baselen, entry.path, entry.pathlen);
108 newbase[baselen + entry.pathlen] = '/';
109 retval = read_tree_recursive(lookup_tree(entry.sha1),
Linus Torvalds94537c72005-04-22 23:42:37110 newbase,
Linus Torvalds4c068a92006-05-30 16:45:45111 baselen + entry.pathlen + 1,
Linus Torvalds3c5e8462005-11-26 17:38:20112 stage, match, fn);
Linus Torvalds94537c72005-04-22 23:42:37113 free(newbase);
114 if (retval)
115 return -1;
116 continue;
117 }
Linus Torvalds94537c72005-04-22 23:42:37118 }
119 return 0;
120}
121
Daniel Barkalow521698b2006-01-26 06:13:36122int read_tree(struct tree *tree, int stage, const char **match)
Linus Torvalds94537c72005-04-22 23:42:37123{
Daniel Barkalow521698b2006-01-26 06:13:36124 return read_tree_recursive(tree, "", 0, stage, match, read_one_entry);
Linus Torvalds94537c72005-04-22 23:42:37125}
126
Jason McMullan5d6ccf52005-06-03 15:05:39127struct tree *lookup_tree(const unsigned char *sha1)
Daniel Barkalow175785e2005-04-18 18:39:48128{
129 struct object *obj = lookup_object(sha1);
130 if (!obj) {
Linus Torvalds855419f2006-06-19 17:44:15131 struct tree *ret = alloc_tree_node();
Daniel Barkalow175785e2005-04-18 18:39:48132 created_object(sha1, &ret->object);
Linus Torvalds19746322006-07-12 03:45:31133 ret->object.type = OBJ_TREE;
Daniel Barkalow175785e2005-04-18 18:39:48134 return ret;
135 }
Nicolas Pitred1af0022005-05-20 20:59:17136 if (!obj->type)
Linus Torvalds19746322006-07-12 03:45:31137 obj->type = OBJ_TREE;
138 if (obj->type != OBJ_TREE) {
Linus Torvalds885a86a2006-06-14 23:45:13139 error("Object %s is a %s, not a tree",
140 sha1_to_hex(sha1), typename(obj->type));
Daniel Barkalow175785e2005-04-18 18:39:48141 return NULL;
142 }
143 return (struct tree *) obj;
144}
145
David Rientjes74b504f2006-08-14 20:40:06146static void track_tree_refs(struct tree *item)
Linus Torvalds2d9c58c2006-05-29 19:18:33147{
148 int n_refs = 0, i;
149 struct object_refs *refs;
150 struct tree_desc desc;
Linus Torvalds4c068a92006-05-30 16:45:45151 struct name_entry entry;
Linus Torvalds2d9c58c2006-05-29 19:18:33152
153 /* Count how many entries there are.. */
154 desc.buf = item->buffer;
155 desc.size = item->size;
156 while (desc.size) {
157 n_refs++;
158 update_tree_entry(&desc);
159 }
160
161 /* Allocate object refs and walk it again.. */
162 i = 0;
163 refs = alloc_object_refs(n_refs);
164 desc.buf = item->buffer;
165 desc.size = item->size;
Linus Torvalds4c068a92006-05-30 16:45:45166 while (tree_entry(&desc, &entry)) {
Linus Torvalds2d9c58c2006-05-29 19:18:33167 struct object *obj;
168
Linus Torvalds4c068a92006-05-30 16:45:45169 if (S_ISDIR(entry.mode))
170 obj = &lookup_tree(entry.sha1)->object;
Linus Torvalds2d9c58c2006-05-29 19:18:33171 else
Linus Torvalds4c068a92006-05-30 16:45:45172 obj = &lookup_blob(entry.sha1)->object;
Linus Torvalds2d9c58c2006-05-29 19:18:33173 refs->ref[i++] = obj;
174 }
175 set_object_refs(&item->object, refs);
Linus Torvalds2d9c58c2006-05-29 19:18:33176}
177
Nicolas Pitrebd2c39f2005-05-06 17:48:34178int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
Daniel Barkalow175785e2005-04-18 18:39:48179{
Daniel Barkalow175785e2005-04-18 18:39:48180 if (item->object.parsed)
181 return 0;
182 item->object.parsed = 1;
Linus Torvalds136f2e52006-05-29 19:16:12183 item->buffer = buffer;
184 item->size = size;
185
Linus Torvalds2d9c58c2006-05-29 19:18:33186 if (track_object_refs)
187 track_tree_refs(item);
188 return 0;
189}
Linus Torvalds136f2e52006-05-29 19:16:12190
Nicolas Pitrebd2c39f2005-05-06 17:48:34191int parse_tree(struct tree *item)
192{
193 char type[20];
194 void *buffer;
195 unsigned long size;
Nicolas Pitrebd2c39f2005-05-06 17:48:34196
197 if (item->object.parsed)
198 return 0;
199 buffer = read_sha1_file(item->object.sha1, type, &size);
200 if (!buffer)
201 return error("Could not read %s",
202 sha1_to_hex(item->object.sha1));
203 if (strcmp(type, tree_type)) {
204 free(buffer);
205 return error("Object %s not a tree",
206 sha1_to_hex(item->object.sha1));
207 }
Linus Torvalds136f2e52006-05-29 19:16:12208 return parse_tree_buffer(item, buffer, size);
Nicolas Pitrebd2c39f2005-05-06 17:48:34209}
Daniel Barkalow77675e22005-09-05 06:03:51210
211struct tree *parse_tree_indirect(const unsigned char *sha1)
212{
213 struct object *obj = parse_object(sha1);
214 do {
215 if (!obj)
216 return NULL;
Linus Torvalds19746322006-07-12 03:45:31217 if (obj->type == OBJ_TREE)
Daniel Barkalow77675e22005-09-05 06:03:51218 return (struct tree *) obj;
Linus Torvalds19746322006-07-12 03:45:31219 else if (obj->type == OBJ_COMMIT)
Daniel Barkalow77675e22005-09-05 06:03:51220 obj = &(((struct commit *) obj)->tree->object);
Linus Torvalds19746322006-07-12 03:45:31221 else if (obj->type == OBJ_TAG)
Daniel Barkalow77675e22005-09-05 06:03:51222 obj = ((struct tag *) obj)->tagged;
223 else
224 return NULL;
225 if (!obj->parsed)
226 parse_object(obj->sha1);
227 } while (1);
228}