blob: ea386e506659c65224cfe55bdc4f8d086171637a [file] [log] [blame]
Junio C Hamano8f1d2e62006-01-07 01:33:54 -08001#include "cache.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -07002#include "tree.h"
3#include "blob.h"
Daniel Barkalow77675e22005-09-05 02:03:51 -04004#include "commit.h"
5#include "tag.h"
Linus Torvalds136f2e52006-05-29 12:16:12 -07006#include "tree-walk.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -07007#include <stdlib.h>
8
9const char *tree_type = "tree";
10
Linus Torvalds3a7c3522006-05-29 12:16:46 -070011static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
Linus Torvalds94537c72005-04-22 16:42:37 -070012{
Linus Torvalds3c5e8462005-11-26 09:38:20 -080013 int len;
14 unsigned int size;
15 struct cache_entry *ce;
16
17 if (S_ISDIR(mode))
18 return READ_TREE_RECURSIVE;
19
20 len = strlen(pathname);
21 size = cache_entry_size(baselen + len);
Peter Eriksen90321c12006-04-03 19:30:46 +010022 ce = xcalloc(1, size);
Linus Torvalds94537c72005-04-22 16:42:37 -070023
24 ce->ce_mode = create_ce_mode(mode);
25 ce->ce_flags = create_ce_flags(baselen + len, stage);
26 memcpy(ce->name, base, baselen);
27 memcpy(ce->name + baselen, pathname, len+1);
Shawn Pearcee7024962006-08-23 02:49:00 -040028 hashcpy(ce->sha1, sha1);
Junio C Hamanob1557252005-06-25 02:25:29 -070029 return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
Linus Torvalds94537c72005-04-22 16:42:37 -070030}
31
Linus Torvalds3e587632005-07-14 11:39:27 -070032static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths)
Linus Torvalds0ca14a52005-07-14 11:26:31 -070033{
Linus Torvalds3e587632005-07-14 11:39:27 -070034 const char *match;
Linus Torvalds0ca14a52005-07-14 11:26:31 -070035 int pathlen;
36
37 if (!paths)
38 return 1;
39 pathlen = strlen(path);
40 while ((match = *paths++) != NULL) {
41 int matchlen = strlen(match);
42
43 if (baselen >= matchlen) {
44 /* If it doesn't match, move along... */
45 if (strncmp(base, match, matchlen))
46 continue;
47 /* The base is a subdirectory of a path which was specified. */
48 return 1;
49 }
50
51 /* Does the base match? */
52 if (strncmp(base, match, baselen))
53 continue;
54
55 match += baselen;
56 matchlen -= baselen;
57
58 if (pathlen > matchlen)
59 continue;
60
61 if (matchlen > pathlen) {
62 if (match[pathlen] != '/')
63 continue;
64 if (!S_ISDIR(mode))
65 continue;
66 }
67
68 if (strncmp(path, match, pathlen))
69 continue;
Linus Torvalds3e587632005-07-14 11:39:27 -070070
71 return 1;
Linus Torvalds0ca14a52005-07-14 11:26:31 -070072 }
73 return 0;
74}
75
Daniel Barkalow521698b2006-01-26 01:13:36 -050076int read_tree_recursive(struct tree *tree,
Linus Torvalds3c5e8462005-11-26 09:38:20 -080077 const char *base, int baselen,
78 int stage, const char **match,
79 read_tree_fn_t fn)
Linus Torvalds94537c72005-04-22 16:42:37 -070080{
Linus Torvalds0790a422006-05-29 12:17:28 -070081 struct tree_desc desc;
Linus Torvalds4c068a92006-05-30 09:45:45 -070082 struct name_entry entry;
Linus Torvalds0790a422006-05-29 12:17:28 -070083
Daniel Barkalow521698b2006-01-26 01:13:36 -050084 if (parse_tree(tree))
85 return -1;
Linus Torvalds0790a422006-05-29 12:17:28 -070086
87 desc.buf = tree->buffer;
88 desc.size = tree->size;
89
Linus Torvalds4c068a92006-05-30 09:45:45 -070090 while (tree_entry(&desc, &entry)) {
91 if (!match_tree_entry(base, baselen, entry.path, entry.mode, match))
Linus Torvalds0ca14a52005-07-14 11:26:31 -070092 continue;
93
Linus Torvalds4c068a92006-05-30 09:45:45 -070094 switch (fn(entry.sha1, base, baselen, entry.path, entry.mode, stage)) {
Linus Torvalds3c5e8462005-11-26 09:38:20 -080095 case 0:
96 continue;
97 case READ_TREE_RECURSIVE:
98 break;;
99 default:
100 return -1;
101 }
Linus Torvalds4c068a92006-05-30 09:45:45 -0700102 if (S_ISDIR(entry.mode)) {
Linus Torvalds94537c72005-04-22 16:42:37 -0700103 int retval;
Jonas Fonseca1c9da462005-04-27 00:00:01 +0200104 char *newbase;
Linus Torvalds94537c72005-04-22 16:42:37 -0700105
Linus Torvalds4c068a92006-05-30 09:45:45 -0700106 newbase = xmalloc(baselen + 1 + entry.pathlen);
Linus Torvalds94537c72005-04-22 16:42:37 -0700107 memcpy(newbase, base, baselen);
Linus Torvalds4c068a92006-05-30 09:45:45 -0700108 memcpy(newbase + baselen, entry.path, entry.pathlen);
109 newbase[baselen + entry.pathlen] = '/';
110 retval = read_tree_recursive(lookup_tree(entry.sha1),
Linus Torvalds94537c72005-04-22 16:42:37 -0700111 newbase,
Linus Torvalds4c068a92006-05-30 09:45:45 -0700112 baselen + entry.pathlen + 1,
Linus Torvalds3c5e8462005-11-26 09:38:20 -0800113 stage, match, fn);
Linus Torvalds94537c72005-04-22 16:42:37 -0700114 free(newbase);
115 if (retval)
116 return -1;
117 continue;
118 }
Linus Torvalds94537c72005-04-22 16:42:37 -0700119 }
120 return 0;
121}
122
Daniel Barkalow521698b2006-01-26 01:13:36 -0500123int read_tree(struct tree *tree, int stage, const char **match)
Linus Torvalds94537c72005-04-22 16:42:37 -0700124{
Daniel Barkalow521698b2006-01-26 01:13:36 -0500125 return read_tree_recursive(tree, "", 0, stage, match, read_one_entry);
Linus Torvalds94537c72005-04-22 16:42:37 -0700126}
127
Jason McMullan5d6ccf52005-06-03 11:05:39 -0400128struct tree *lookup_tree(const unsigned char *sha1)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700129{
130 struct object *obj = lookup_object(sha1);
131 if (!obj) {
Linus Torvalds855419f2006-06-19 10:44:15 -0700132 struct tree *ret = alloc_tree_node();
Daniel Barkalow175785e2005-04-18 11:39:48 -0700133 created_object(sha1, &ret->object);
Linus Torvalds19746322006-07-11 20:45:31 -0700134 ret->object.type = OBJ_TREE;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700135 return ret;
136 }
Nicolas Pitred1af0022005-05-20 16:59:17 -0400137 if (!obj->type)
Linus Torvalds19746322006-07-11 20:45:31 -0700138 obj->type = OBJ_TREE;
139 if (obj->type != OBJ_TREE) {
Linus Torvalds885a86a2006-06-14 16:45:13 -0700140 error("Object %s is a %s, not a tree",
141 sha1_to_hex(sha1), typename(obj->type));
Daniel Barkalow175785e2005-04-18 11:39:48 -0700142 return NULL;
143 }
144 return (struct tree *) obj;
145}
146
David Rientjes74b504f2006-08-14 13:40:06 -0700147static void track_tree_refs(struct tree *item)
Linus Torvalds2d9c58c2006-05-29 12:18:33 -0700148{
149 int n_refs = 0, i;
150 struct object_refs *refs;
151 struct tree_desc desc;
Linus Torvalds4c068a92006-05-30 09:45:45 -0700152 struct name_entry entry;
Linus Torvalds2d9c58c2006-05-29 12:18:33 -0700153
154 /* Count how many entries there are.. */
155 desc.buf = item->buffer;
156 desc.size = item->size;
157 while (desc.size) {
158 n_refs++;
159 update_tree_entry(&desc);
160 }
161
162 /* Allocate object refs and walk it again.. */
163 i = 0;
164 refs = alloc_object_refs(n_refs);
165 desc.buf = item->buffer;
166 desc.size = item->size;
Linus Torvalds4c068a92006-05-30 09:45:45 -0700167 while (tree_entry(&desc, &entry)) {
Linus Torvalds2d9c58c2006-05-29 12:18:33 -0700168 struct object *obj;
169
Linus Torvalds4c068a92006-05-30 09:45:45 -0700170 if (S_ISDIR(entry.mode))
171 obj = &lookup_tree(entry.sha1)->object;
Linus Torvalds2d9c58c2006-05-29 12:18:33 -0700172 else
Linus Torvalds4c068a92006-05-30 09:45:45 -0700173 obj = &lookup_blob(entry.sha1)->object;
Linus Torvalds2d9c58c2006-05-29 12:18:33 -0700174 refs->ref[i++] = obj;
175 }
176 set_object_refs(&item->object, refs);
Linus Torvalds2d9c58c2006-05-29 12:18:33 -0700177}
178
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400179int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700180{
Daniel Barkalow175785e2005-04-18 11:39:48 -0700181 if (item->object.parsed)
182 return 0;
183 item->object.parsed = 1;
Linus Torvalds136f2e52006-05-29 12:16:12 -0700184 item->buffer = buffer;
185 item->size = size;
186
Linus Torvalds2d9c58c2006-05-29 12:18:33 -0700187 if (track_object_refs)
188 track_tree_refs(item);
189 return 0;
190}
Linus Torvalds136f2e52006-05-29 12:16:12 -0700191
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400192int parse_tree(struct tree *item)
193{
194 char type[20];
195 void *buffer;
196 unsigned long size;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400197
198 if (item->object.parsed)
199 return 0;
200 buffer = read_sha1_file(item->object.sha1, type, &size);
201 if (!buffer)
202 return error("Could not read %s",
203 sha1_to_hex(item->object.sha1));
204 if (strcmp(type, tree_type)) {
205 free(buffer);
206 return error("Object %s not a tree",
207 sha1_to_hex(item->object.sha1));
208 }
Linus Torvalds136f2e52006-05-29 12:16:12 -0700209 return parse_tree_buffer(item, buffer, size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400210}
Daniel Barkalow77675e22005-09-05 02:03:51 -0400211
212struct tree *parse_tree_indirect(const unsigned char *sha1)
213{
214 struct object *obj = parse_object(sha1);
215 do {
216 if (!obj)
217 return NULL;
Linus Torvalds19746322006-07-11 20:45:31 -0700218 if (obj->type == OBJ_TREE)
Daniel Barkalow77675e22005-09-05 02:03:51 -0400219 return (struct tree *) obj;
Linus Torvalds19746322006-07-11 20:45:31 -0700220 else if (obj->type == OBJ_COMMIT)
Daniel Barkalow77675e22005-09-05 02:03:51 -0400221 obj = &(((struct commit *) obj)->tree->object);
Linus Torvalds19746322006-07-11 20:45:31 -0700222 else if (obj->type == OBJ_TAG)
Daniel Barkalow77675e22005-09-05 02:03:51 -0400223 obj = ((struct tag *) obj)->tagged;
224 else
225 return NULL;
226 if (!obj->parsed)
227 parse_object(obj->sha1);
228 } while (1);
229}