1
#include "cache.h"
2
+#include "cache-tree.h"
3
#include "tree.h"
4
#include "blob.h"
5
#include "commit.h"
8
9
const char *tree_type = "tree";
10
10
-static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
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)
12
{
13
int len;
14
unsigned int size;
26
memcpy(ce->name, base, baselen);
27
memcpy(ce->name + baselen, pathname, len+1);
28
hashcpy(ce->sha1, sha1);
28
- return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
29
+ return add_cache_entry(ce, opt);
30
+}
31
+
32
+static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
33
+{
34
+ return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage,
35
+ ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
36
+}
37
+
38
+/*
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.
41
+ */
42
+static int read_one_entry_quick(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
43
+{
44
+ return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage,
45
+ ADD_CACHE_JUST_APPEND);
46
}
47
48
static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths)
136
return 0;
137
}
138
139
+static int cmp_cache_name_compare(const void *a_, const void *b_)
140
+{
141
+ const struct cache_entry *ce1, *ce2;
142
+
143
+ ce1 = *((const struct cache_entry **)a_);
144
+ ce2 = *((const struct cache_entry **)b_);
145
+ return cache_name_compare(ce1->name, ntohs(ce1->ce_flags),
146
+ ce2->name, ntohs(ce2->ce_flags));
147
+}
148
+
149
int read_tree(struct tree *tree, int stage, const char **match)
150
{
124
- return read_tree_recursive(tree, "", 0, stage, match, read_one_entry);
151
+ read_tree_fn_t fn = NULL;
152
+ int i, err;
153
+
154
+ /*
155
+ * Currently the only existing callers of this function all
156
+ * call it with stage=1 and after making sure there is nothing
157
+ * at that stage; we could always use read_one_entry_quick().
158
+ *
159
+ * But when we decide to straighten out git-read-tree not to
160
+ * use unpack_trees() in some cases, this will probably start
161
+ * to matter.
162
+ */
163
+
164
+ /*
165
+ * See if we have cache entry at the stage. If so,
166
+ * do it the original slow way, otherwise, append and then
167
+ * sort at the end.
168
+ */
169
+ for (i = 0; !fn && i < active_nr; i++) {
170
+ struct cache_entry *ce = active_cache[i];
171
+ if (ce_stage(ce) == stage)
172
+ fn = read_one_entry;
173
+ }
174
+
175
+ if (!fn)
176
+ fn = read_one_entry_quick;
177
+ err = read_tree_recursive(tree, "", 0, stage, match, fn);
178
+ if (fn == read_one_entry || err)
179
+ return err;
180
+
181
+ /*
182
+ * Sort the cache entry -- we need to nuke the cache tree, though.
183
+ */
184
+ cache_tree_free(&active_cache_tree);
185
+ qsort(active_cache, active_nr, sizeof(active_cache[0]),
186
+ cmp_cache_name_compare);
187
+ return 0;
188
}
189
190
struct tree *lookup_tree(const unsigned char *sha1)