git-add: introduce --edit (to edit the diff vs. the index)
[git/dscho.git] / tree.c
blob0d703a0c473e65c60d9e130b4e1fd50b79e9462b
1 #include "cache.h"
2 #include "cache-tree.h"
3 #include "tree.h"
4 #include "blob.h"
5 #include "commit.h"
6 #include "tag.h"
7 #include "tree-walk.h"
9 const char *tree_type = "tree";
11 static int read_one_entry_opt(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, int opt)
13 int len;
14 unsigned int size;
15 struct cache_entry *ce;
17 if (S_ISDIR(mode))
18 return READ_TREE_RECURSIVE;
20 len = strlen(pathname);
21 size = cache_entry_size(baselen + len);
22 ce = xcalloc(1, size);
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);
28 hashcpy(ce->sha1, sha1);
29 return add_cache_entry(ce, opt);
32 static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, void *context)
34 return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage,
35 ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
39 * This is used when the caller knows there is no existing entries at
40 * the stage that will conflict with the entry being added.
42 static int read_one_entry_quick(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, void *context)
44 return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage,
45 ADD_CACHE_JUST_APPEND);
48 static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths)
50 const char *match;
51 int pathlen;
53 if (!paths)
54 return 1;
55 pathlen = strlen(path);
56 while ((match = *paths++) != NULL) {
57 int matchlen = strlen(match);
59 if (baselen >= matchlen) {
60 /* If it doesn't match, move along... */
61 if (strncmp(base, match, matchlen))
62 continue;
63 /* pathspecs match only at the directory boundaries */
64 if (!matchlen ||
65 base[matchlen] == '/' ||
66 match[matchlen - 1] == '/')
67 return 1;
68 continue;
71 /* Does the base match? */
72 if (strncmp(base, match, baselen))
73 continue;
75 match += baselen;
76 matchlen -= baselen;
78 if (pathlen > matchlen)
79 continue;
81 if (matchlen > pathlen) {
82 if (match[pathlen] != '/')
83 continue;
84 if (!S_ISDIR(mode))
85 continue;
88 if (strncmp(path, match, pathlen))
89 continue;
91 return 1;
93 return 0;
96 int read_tree_recursive(struct tree *tree,
97 const char *base, int baselen,
98 int stage, const char **match,
99 read_tree_fn_t fn, void *context)
101 struct tree_desc desc;
102 struct name_entry entry;
104 if (parse_tree(tree))
105 return -1;
107 init_tree_desc(&desc, tree->buffer, tree->size);
109 while (tree_entry(&desc, &entry)) {
110 if (!match_tree_entry(base, baselen, entry.path, entry.mode, match))
111 continue;
113 switch (fn(entry.sha1, base, baselen, entry.path, entry.mode, stage, context)) {
114 case 0:
115 continue;
116 case READ_TREE_RECURSIVE:
117 break;
118 default:
119 return -1;
121 if (S_ISDIR(entry.mode)) {
122 int retval;
123 char *newbase;
124 unsigned int pathlen = tree_entry_len(entry.path, entry.sha1);
126 newbase = xmalloc(baselen + 1 + pathlen);
127 memcpy(newbase, base, baselen);
128 memcpy(newbase + baselen, entry.path, pathlen);
129 newbase[baselen + pathlen] = '/';
130 retval = read_tree_recursive(lookup_tree(entry.sha1),
131 newbase,
132 baselen + pathlen + 1,
133 stage, match, fn, context);
134 free(newbase);
135 if (retval)
136 return -1;
137 continue;
138 } else if (S_ISGITLINK(entry.mode)) {
139 int retval;
140 struct strbuf path;
141 unsigned int entrylen;
142 struct commit *commit;
144 entrylen = tree_entry_len(entry.path, entry.sha1);
145 strbuf_init(&path, baselen + entrylen + 1);
146 strbuf_add(&path, base, baselen);
147 strbuf_add(&path, entry.path, entrylen);
148 strbuf_addch(&path, '/');
150 commit = lookup_commit(entry.sha1);
151 if (!commit)
152 die("Commit %s in submodule path %s not found",
153 sha1_to_hex(entry.sha1), path.buf);
155 if (parse_commit(commit))
156 die("Invalid commit %s in submodule path %s",
157 sha1_to_hex(entry.sha1), path.buf);
159 retval = read_tree_recursive(commit->tree,
160 path.buf, path.len,
161 stage, match, fn, context);
162 strbuf_release(&path);
163 if (retval)
164 return -1;
165 continue;
168 return 0;
171 static int cmp_cache_name_compare(const void *a_, const void *b_)
173 const struct cache_entry *ce1, *ce2;
175 ce1 = *((const struct cache_entry **)a_);
176 ce2 = *((const struct cache_entry **)b_);
177 return cache_name_compare(ce1->name, ce1->ce_flags,
178 ce2->name, ce2->ce_flags);
181 int read_tree(struct tree *tree, int stage, const char **match)
183 read_tree_fn_t fn = NULL;
184 int i, err;
187 * Currently the only existing callers of this function all
188 * call it with stage=1 and after making sure there is nothing
189 * at that stage; we could always use read_one_entry_quick().
191 * But when we decide to straighten out git-read-tree not to
192 * use unpack_trees() in some cases, this will probably start
193 * to matter.
197 * See if we have cache entry at the stage. If so,
198 * do it the original slow way, otherwise, append and then
199 * sort at the end.
201 for (i = 0; !fn && i < active_nr; i++) {
202 struct cache_entry *ce = active_cache[i];
203 if (ce_stage(ce) == stage)
204 fn = read_one_entry;
207 if (!fn)
208 fn = read_one_entry_quick;
209 err = read_tree_recursive(tree, "", 0, stage, match, fn, NULL);
210 if (fn == read_one_entry || err)
211 return err;
214 * Sort the cache entry -- we need to nuke the cache tree, though.
216 cache_tree_free(&active_cache_tree);
217 qsort(active_cache, active_nr, sizeof(active_cache[0]),
218 cmp_cache_name_compare);
219 return 0;
222 struct tree *lookup_tree(const unsigned char *sha1)
224 struct object *obj = lookup_object(sha1);
225 if (!obj)
226 return create_object(sha1, OBJ_TREE, alloc_tree_node());
227 if (!obj->type)
228 obj->type = OBJ_TREE;
229 if (obj->type != OBJ_TREE) {
230 error("Object %s is a %s, not a tree",
231 sha1_to_hex(sha1), typename(obj->type));
232 return NULL;
234 return (struct tree *) obj;
237 int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
239 if (item->object.parsed)
240 return 0;
241 item->object.parsed = 1;
242 item->buffer = buffer;
243 item->size = size;
245 return 0;
248 int parse_tree(struct tree *item)
250 enum object_type type;
251 void *buffer;
252 unsigned long size;
254 if (item->object.parsed)
255 return 0;
256 buffer = read_sha1_file(item->object.sha1, &type, &size);
257 if (!buffer)
258 return error("Could not read %s",
259 sha1_to_hex(item->object.sha1));
260 if (type != OBJ_TREE) {
261 free(buffer);
262 return error("Object %s not a tree",
263 sha1_to_hex(item->object.sha1));
265 return parse_tree_buffer(item, buffer, size);
268 struct tree *parse_tree_indirect(const unsigned char *sha1)
270 struct object *obj = parse_object(sha1);
271 do {
272 if (!obj)
273 return NULL;
274 if (obj->type == OBJ_TREE)
275 return (struct tree *) obj;
276 else if (obj->type == OBJ_COMMIT)
277 obj = &(((struct commit *) obj)->tree->object);
278 else if (obj->type == OBJ_TAG)
279 obj = ((struct tag *) obj)->tagged;
280 else
281 return NULL;
282 if (!obj->parsed)
283 parse_object(obj->sha1);
284 } while (1);