Merge branch 'ab/test-must-be-empty'
[git] / tree.c
1 #define NO_THE_INDEX_COMPATIBILITY_MACROS
2 #include "cache.h"
3 #include "cache-tree.h"
4 #include "tree.h"
5 #include "object-store.h"
6 #include "blob.h"
7 #include "commit.h"
8 #include "tag.h"
9 #include "alloc.h"
10 #include "tree-walk.h"
11 #include "repository.h"
12
13 const char *tree_type = "tree";
14
15 static int read_one_entry_opt(struct index_state *istate,
16                               const struct object_id *oid,
17                               const char *base, int baselen,
18                               const char *pathname,
19                               unsigned mode, int stage, int opt)
20 {
21         int len;
22         struct cache_entry *ce;
23
24         if (S_ISDIR(mode))
25                 return READ_TREE_RECURSIVE;
26
27         len = strlen(pathname);
28         ce = make_empty_cache_entry(istate, baselen + len);
29
30         ce->ce_mode = create_ce_mode(mode);
31         ce->ce_flags = create_ce_flags(stage);
32         ce->ce_namelen = baselen + len;
33         memcpy(ce->name, base, baselen);
34         memcpy(ce->name + baselen, pathname, len+1);
35         oidcpy(&ce->oid, oid);
36         return add_index_entry(istate, ce, opt);
37 }
38
39 static int read_one_entry(const struct object_id *oid, struct strbuf *base,
40                           const char *pathname, unsigned mode, int stage,
41                           void *context)
42 {
43         struct index_state *istate = context;
44         return read_one_entry_opt(istate, oid, base->buf, base->len, pathname,
45                                   mode, stage,
46                                   ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
47 }
48
49 /*
50  * This is used when the caller knows there is no existing entries at
51  * the stage that will conflict with the entry being added.
52  */
53 static int read_one_entry_quick(const struct object_id *oid, struct strbuf *base,
54                                 const char *pathname, unsigned mode, int stage,
55                                 void *context)
56 {
57         struct index_state *istate = context;
58         return read_one_entry_opt(istate, oid, base->buf, base->len, pathname,
59                                   mode, stage,
60                                   ADD_CACHE_JUST_APPEND);
61 }
62
63 static int read_tree_1(struct tree *tree, struct strbuf *base,
64                        int stage, const struct pathspec *pathspec,
65                        read_tree_fn_t fn, void *context)
66 {
67         struct tree_desc desc;
68         struct name_entry entry;
69         struct object_id oid;
70         int len, oldlen = base->len;
71         enum interesting retval = entry_not_interesting;
72
73         if (parse_tree(tree))
74                 return -1;
75
76         init_tree_desc(&desc, tree->buffer, tree->size);
77
78         while (tree_entry(&desc, &entry)) {
79                 if (retval != all_entries_interesting) {
80                         retval = tree_entry_interesting(&entry, base, 0, pathspec);
81                         if (retval == all_entries_not_interesting)
82                                 break;
83                         if (retval == entry_not_interesting)
84                                 continue;
85                 }
86
87                 switch (fn(entry.oid, base,
88                            entry.path, entry.mode, stage, context)) {
89                 case 0:
90                         continue;
91                 case READ_TREE_RECURSIVE:
92                         break;
93                 default:
94                         return -1;
95                 }
96
97                 if (S_ISDIR(entry.mode))
98                         oidcpy(&oid, entry.oid);
99                 else if (S_ISGITLINK(entry.mode)) {
100                         struct commit *commit;
101
102                         commit = lookup_commit(the_repository, entry.oid);
103                         if (!commit)
104                                 die("Commit %s in submodule path %s%s not found",
105                                     oid_to_hex(entry.oid),
106                                     base->buf, entry.path);
107
108                         if (parse_commit(commit))
109                                 die("Invalid commit %s in submodule path %s%s",
110                                     oid_to_hex(entry.oid),
111                                     base->buf, entry.path);
112
113                         oidcpy(&oid, get_commit_tree_oid(commit));
114                 }
115                 else
116                         continue;
117
118                 len = tree_entry_len(&entry);
119                 strbuf_add(base, entry.path, len);
120                 strbuf_addch(base, '/');
121                 retval = read_tree_1(lookup_tree(the_repository, &oid),
122                                      base, stage, pathspec,
123                                      fn, context);
124                 strbuf_setlen(base, oldlen);
125                 if (retval)
126                         return -1;
127         }
128         return 0;
129 }
130
131 int read_tree_recursive(struct tree *tree,
132                         const char *base, int baselen,
133                         int stage, const struct pathspec *pathspec,
134                         read_tree_fn_t fn, void *context)
135 {
136         struct strbuf sb = STRBUF_INIT;
137         int ret;
138
139         strbuf_add(&sb, base, baselen);
140         ret = read_tree_1(tree, &sb, stage, pathspec, fn, context);
141         strbuf_release(&sb);
142         return ret;
143 }
144
145 static int cmp_cache_name_compare(const void *a_, const void *b_)
146 {
147         const struct cache_entry *ce1, *ce2;
148
149         ce1 = *((const struct cache_entry **)a_);
150         ce2 = *((const struct cache_entry **)b_);
151         return cache_name_stage_compare(ce1->name, ce1->ce_namelen, ce_stage(ce1),
152                                   ce2->name, ce2->ce_namelen, ce_stage(ce2));
153 }
154
155 int read_tree(struct tree *tree, int stage, struct pathspec *match,
156               struct index_state *istate)
157 {
158         read_tree_fn_t fn = NULL;
159         int i, err;
160
161         /*
162          * Currently the only existing callers of this function all
163          * call it with stage=1 and after making sure there is nothing
164          * at that stage; we could always use read_one_entry_quick().
165          *
166          * But when we decide to straighten out git-read-tree not to
167          * use unpack_trees() in some cases, this will probably start
168          * to matter.
169          */
170
171         /*
172          * See if we have cache entry at the stage.  If so,
173          * do it the original slow way, otherwise, append and then
174          * sort at the end.
175          */
176         for (i = 0; !fn && i < istate->cache_nr; i++) {
177                 const struct cache_entry *ce = istate->cache[i];
178                 if (ce_stage(ce) == stage)
179                         fn = read_one_entry;
180         }
181
182         if (!fn)
183                 fn = read_one_entry_quick;
184         err = read_tree_recursive(tree, "", 0, stage, match, fn, istate);
185         if (fn == read_one_entry || err)
186                 return err;
187
188         /*
189          * Sort the cache entry -- we need to nuke the cache tree, though.
190          */
191         cache_tree_free(&istate->cache_tree);
192         QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
193         return 0;
194 }
195
196 struct tree *lookup_tree(struct repository *r, const struct object_id *oid)
197 {
198         struct object *obj = lookup_object(r, oid->hash);
199         if (!obj)
200                 return create_object(r, oid->hash,
201                                      alloc_tree_node(r));
202         return object_as_type(r, obj, OBJ_TREE, 0);
203 }
204
205 int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
206 {
207         if (item->object.parsed)
208                 return 0;
209         item->object.parsed = 1;
210         item->buffer = buffer;
211         item->size = size;
212
213         return 0;
214 }
215
216 int parse_tree_gently(struct tree *item, int quiet_on_missing)
217 {
218          enum object_type type;
219          void *buffer;
220          unsigned long size;
221
222         if (item->object.parsed)
223                 return 0;
224         buffer = read_object_file(&item->object.oid, &type, &size);
225         if (!buffer)
226                 return quiet_on_missing ? -1 :
227                         error("Could not read %s",
228                              oid_to_hex(&item->object.oid));
229         if (type != OBJ_TREE) {
230                 free(buffer);
231                 return error("Object %s not a tree",
232                              oid_to_hex(&item->object.oid));
233         }
234         return parse_tree_buffer(item, buffer, size);
235 }
236
237 void free_tree_buffer(struct tree *tree)
238 {
239         FREE_AND_NULL(tree->buffer);
240         tree->size = 0;
241         tree->object.parsed = 0;
242 }
243
244 struct tree *parse_tree_indirect(const struct object_id *oid)
245 {
246         struct object *obj = parse_object(the_repository, oid);
247         do {
248                 if (!obj)
249                         return NULL;
250                 if (obj->type == OBJ_TREE)
251                         return (struct tree *) obj;
252                 else if (obj->type == OBJ_COMMIT)
253                         obj = &(get_commit_tree(((struct commit *)obj))->object);
254                 else if (obj->type == OBJ_TAG)
255                         obj = ((struct tag *) obj)->tagged;
256                 else
257                         return NULL;
258                 if (!obj->parsed)
259                         parse_object(the_repository, &obj->oid);
260         } while (1);
261 }