Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 246 lines (213 sloc) 6.318 kB
8f1d2e6 [PATCH] Compilation: zero-length array declaration.
Junio C Hamano authored
1 #include "cache.h"
af3785d @gitster Optimize "diff --cached" performance.
gitster authored
2 #include "cache-tree.h"
175785e [PATCH] Implementations of parsing functions
Daniel Barkalow authored
3 #include "tree.h"
4 #include "blob.h"
77675e2 [PATCH] Add a function for getting a struct tree for an ent.
Daniel Barkalow authored
5 #include "commit.h"
6 #include "tag.h"
136f2e5 Make "struct tree" contain the pointer to the tree buffer
Linus Torvalds authored
7 #include "tree-walk.h"
175785e [PATCH] Implementations of parsing functions
Daniel Barkalow authored
8
9 const char *tree_type = "tree";
10
af3785d @gitster Optimize "diff --cached" performance.
gitster authored
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)
94537c7 Move "read_tree()" to "tree.c" to be used as a generic helper function.
Linus Torvalds authored
12 {
3c5e846 ls-tree: major rewrite to do pathspec
Linus Torvalds authored
13 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);
90321c1 Replace xmalloc+memset(0) with xcalloc.
Peter Eriksen authored
22 ce = xcalloc(1, size);
94537c7 Move "read_tree()" to "tree.c" to be used as a generic helper function.
Linus Torvalds authored
23
24 ce->ce_mode = create_ce_mode(mode);
b60e188 @tgummerer Strip namelen out of ce_flags into a ce_namelen field
tgummerer authored
25 ce->ce_flags = create_ce_flags(stage);
26 ce->ce_namelen = baselen + len;
94537c7 Move "read_tree()" to "tree.c" to be used as a generic helper function.
Linus Torvalds authored
27 memcpy(ce->name, base, baselen);
28 memcpy(ce->name + baselen, pathname, len+1);
e702496 @spearce Convert memcpy(a,b,20) to hashcpy(a,b).
spearce authored
29 hashcpy(ce->sha1, sha1);
af3785d @gitster Optimize "diff --cached" performance.
gitster authored
30 return add_cache_entry(ce, opt);
31 }
32
671f070 add context pointer to read_tree_recursive()
René Scharfe authored
33 static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, void *context)
af3785d @gitster Optimize "diff --cached" performance.
gitster authored
34 {
35 return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage,
36 ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
37 }
38
39 /*
40 * This is used when the caller knows there is no existing entries at
41 * the stage that will conflict with the entry being added.
42 */
671f070 add context pointer to read_tree_recursive()
René Scharfe authored
43 static int read_one_entry_quick(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, void *context)
af3785d @gitster Optimize "diff --cached" performance.
gitster authored
44 {
45 return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage,
46 ADD_CACHE_JUST_APPEND);
94537c7 Move "read_tree()" to "tree.c" to be used as a generic helper function.
Linus Torvalds authored
47 }
48
ffd31f6 @pclouds Reimplement read_tree_recursive() using tree_entry_interesting()
pclouds authored
49 static int read_tree_1(struct tree *tree, struct strbuf *base,
50 int stage, struct pathspec *pathspec,
51 read_tree_fn_t fn, void *context)
94537c7 Move "read_tree()" to "tree.c" to be used as a generic helper function.
Linus Torvalds authored
52 {
0790a42 Switch "read_tree_recursive()" over to tree-walk functionality
Linus Torvalds authored
53 struct tree_desc desc;
4c068a9 tree_entry(): new tree-walking helper function
Linus Torvalds authored
54 struct name_entry entry;
ffd31f6 @pclouds Reimplement read_tree_recursive() using tree_entry_interesting()
pclouds authored
55 unsigned char sha1[20];
d688cf0 @pclouds tree_entry_interesting(): give meaningful names to return values
pclouds authored
56 int len, oldlen = base->len;
57 enum interesting retval = entry_not_interesting;
0790a42 Switch "read_tree_recursive()" over to tree-walk functionality
Linus Torvalds authored
58
521698b Only use a single parser for tree objects
Daniel Barkalow authored
59 if (parse_tree(tree))
60 return -1;
0790a42 Switch "read_tree_recursive()" over to tree-walk functionality
Linus Torvalds authored
61
6fda5e5 @torvalds Initialize tree descriptors with a helper function rather than by hand.
torvalds authored
62 init_tree_desc(&desc, tree->buffer, tree->size);
0790a42 Switch "read_tree_recursive()" over to tree-walk functionality
Linus Torvalds authored
63
4c068a9 tree_entry(): new tree-walking helper function
Linus Torvalds authored
64 while (tree_entry(&desc, &entry)) {
d688cf0 @pclouds tree_entry_interesting(): give meaningful names to return values
pclouds authored
65 if (retval != all_entries_interesting) {
ffd31f6 @pclouds Reimplement read_tree_recursive() using tree_entry_interesting()
pclouds authored
66 retval = tree_entry_interesting(&entry, base, 0, pathspec);
d688cf0 @pclouds tree_entry_interesting(): give meaningful names to return values
pclouds authored
67 if (retval == all_entries_not_interesting)
ffd31f6 @pclouds Reimplement read_tree_recursive() using tree_entry_interesting()
pclouds authored
68 break;
d688cf0 @pclouds tree_entry_interesting(): give meaningful names to return values
pclouds authored
69 if (retval == entry_not_interesting)
ffd31f6 @pclouds Reimplement read_tree_recursive() using tree_entry_interesting()
pclouds authored
70 continue;
71 }
0ca14a5 Start adding interfaces to read in partial trees
Linus Torvalds authored
72
ffd31f6 @pclouds Reimplement read_tree_recursive() using tree_entry_interesting()
pclouds authored
73 switch (fn(entry.sha1, base->buf, base->len,
74 entry.path, entry.mode, stage, context)) {
3c5e846 ls-tree: major rewrite to do pathspec
Linus Torvalds authored
75 case 0:
76 continue;
77 case READ_TREE_RECURSIVE:
ba19a80 @gitster Drop double-semicolon in C
gitster authored
78 break;
3c5e846 ls-tree: major rewrite to do pathspec
Linus Torvalds authored
79 default:
80 return -1;
81 }
d3bee16 @hjemli tree.c: allow read_tree_recursive() to traverse gitlink entries
hjemli authored
82
ffd31f6 @pclouds Reimplement read_tree_recursive() using tree_entry_interesting()
pclouds authored
83 if (S_ISDIR(entry.mode))
84 hashcpy(sha1, entry.sha1);
85 else if (S_ISGITLINK(entry.mode)) {
86 struct commit *commit;
d3bee16 @hjemli tree.c: allow read_tree_recursive() to traverse gitlink entries
hjemli authored
87
88 commit = lookup_commit(entry.sha1);
89 if (!commit)
ffd31f6 @pclouds Reimplement read_tree_recursive() using tree_entry_interesting()
pclouds authored
90 die("Commit %s in submodule path %s%s not found",
91 sha1_to_hex(entry.sha1),
92 base->buf, entry.path);
d3bee16 @hjemli tree.c: allow read_tree_recursive() to traverse gitlink entries
hjemli authored
93
94 if (parse_commit(commit))
ffd31f6 @pclouds Reimplement read_tree_recursive() using tree_entry_interesting()
pclouds authored
95 die("Invalid commit %s in submodule path %s%s",
96 sha1_to_hex(entry.sha1),
97 base->buf, entry.path);
98
99 hashcpy(sha1, commit->tree->object.sha1);
94537c7 Move "read_tree()" to "tree.c" to be used as a generic helper function.
Linus Torvalds authored
100 }
ffd31f6 @pclouds Reimplement read_tree_recursive() using tree_entry_interesting()
pclouds authored
101 else
102 continue;
103
0de1633 @pclouds tree-walk.c: do not leak internal structure in tree_entry_len()
pclouds authored
104 len = tree_entry_len(&entry);
ffd31f6 @pclouds Reimplement read_tree_recursive() using tree_entry_interesting()
pclouds authored
105 strbuf_add(base, entry.path, len);
106 strbuf_addch(base, '/');
107 retval = read_tree_1(lookup_tree(sha1),
108 base, stage, pathspec,
109 fn, context);
110 strbuf_setlen(base, oldlen);
111 if (retval)
112 return -1;
94537c7 Move "read_tree()" to "tree.c" to be used as a generic helper function.
Linus Torvalds authored
113 }
114 return 0;
115 }
116
ffd31f6 @pclouds Reimplement read_tree_recursive() using tree_entry_interesting()
pclouds authored
117 int read_tree_recursive(struct tree *tree,
118 const char *base, int baselen,
f0096c0 @pclouds Convert read_tree{,_recursive} to support struct pathspec
pclouds authored
119 int stage, struct pathspec *pathspec,
ffd31f6 @pclouds Reimplement read_tree_recursive() using tree_entry_interesting()
pclouds authored
120 read_tree_fn_t fn, void *context)
121 {
122 struct strbuf sb = STRBUF_INIT;
f0096c0 @pclouds Convert read_tree{,_recursive} to support struct pathspec
pclouds authored
123 int ret;
ffd31f6 @pclouds Reimplement read_tree_recursive() using tree_entry_interesting()
pclouds authored
124
125 strbuf_add(&sb, base, baselen);
f0096c0 @pclouds Convert read_tree{,_recursive} to support struct pathspec
pclouds authored
126 ret = read_tree_1(tree, &sb, stage, pathspec, fn, context);
ffd31f6 @pclouds Reimplement read_tree_recursive() using tree_entry_interesting()
pclouds authored
127 strbuf_release(&sb);
128 return ret;
129 }
130
af3785d @gitster Optimize "diff --cached" performance.
gitster authored
131 static int cmp_cache_name_compare(const void *a_, const void *b_)
132 {
133 const struct cache_entry *ce1, *ce2;
134
135 ce1 = *((const struct cache_entry **)a_);
136 ce2 = *((const struct cache_entry **)b_);
b60e188 @tgummerer Strip namelen out of ce_flags into a ce_namelen field
tgummerer authored
137 return cache_name_stage_compare(ce1->name, ce1->ce_namelen, ce_stage(ce1),
138 ce2->name, ce2->ce_namelen, ce_stage(ce2));
af3785d @gitster Optimize "diff --cached" performance.
gitster authored
139 }
140
f0096c0 @pclouds Convert read_tree{,_recursive} to support struct pathspec
pclouds authored
141 int read_tree(struct tree *tree, int stage, struct pathspec *match)
94537c7 Move "read_tree()" to "tree.c" to be used as a generic helper function.
Linus Torvalds authored
142 {
af3785d @gitster Optimize "diff --cached" performance.
gitster authored
143 read_tree_fn_t fn = NULL;
144 int i, err;
145
146 /*
147 * Currently the only existing callers of this function all
148 * call it with stage=1 and after making sure there is nothing
149 * at that stage; we could always use read_one_entry_quick().
150 *
151 * But when we decide to straighten out git-read-tree not to
152 * use unpack_trees() in some cases, this will probably start
153 * to matter.
154 */
155
156 /*
157 * See if we have cache entry at the stage. If so,
158 * do it the original slow way, otherwise, append and then
159 * sort at the end.
160 */
161 for (i = 0; !fn && i < active_nr; i++) {
162 struct cache_entry *ce = active_cache[i];
163 if (ce_stage(ce) == stage)
164 fn = read_one_entry;
165 }
166
167 if (!fn)
168 fn = read_one_entry_quick;
671f070 add context pointer to read_tree_recursive()
René Scharfe authored
169 err = read_tree_recursive(tree, "", 0, stage, match, fn, NULL);
af3785d @gitster Optimize "diff --cached" performance.
gitster authored
170 if (fn == read_one_entry || err)
171 return err;
172
173 /*
174 * Sort the cache entry -- we need to nuke the cache tree, though.
175 */
176 cache_tree_free(&active_cache_tree);
177 qsort(active_cache, active_nr, sizeof(active_cache[0]),
178 cmp_cache_name_compare);
179 return 0;
94537c7 Move "read_tree()" to "tree.c" to be used as a generic helper function.
Linus Torvalds authored
180 }
181
5d6ccf5 [PATCH] Anal retentive 'const unsigned char *sha1'
Jason McMullan authored
182 struct tree *lookup_tree(const unsigned char *sha1)
175785e [PATCH] Implementations of parsing functions
Daniel Barkalow authored
183 {
184 struct object *obj = lookup_object(sha1);
100c5f3 @torvalds Clean up object creation to use more common code
torvalds authored
185 if (!obj)
186 return create_object(sha1, OBJ_TREE, alloc_tree_node());
d1af002 [PATCH] delta check
Nicolas Pitre authored
187 if (!obj->type)
1974632 Remove TYPE_* constant macros and use object_type enums consistently.
Linus Torvalds authored
188 obj->type = OBJ_TREE;
189 if (obj->type != OBJ_TREE) {
885a86a Shrink "struct object" a bit
Linus Torvalds authored
190 error("Object %s is a %s, not a tree",
191 sha1_to_hex(sha1), typename(obj->type));
175785e [PATCH] Implementations of parsing functions
Daniel Barkalow authored
192 return NULL;
193 }
194 return (struct tree *) obj;
195 }
196
2d9c58c Remove "tree->entries" tree-entry list from tree parser
Linus Torvalds authored
197 int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
198 {
175785e [PATCH] Implementations of parsing functions
Daniel Barkalow authored
199 if (item->object.parsed)
200 return 0;
201 item->object.parsed = 1;
136f2e5 Make "struct tree" contain the pointer to the tree buffer
Linus Torvalds authored
202 item->buffer = buffer;
203 item->size = size;
204
2d9c58c Remove "tree->entries" tree-entry list from tree parser
Linus Torvalds authored
205 return 0;
206 }
207
bd2c39f [PATCH] don't load and decompress objects twice with parse_object()
Nicolas Pitre authored
208 int parse_tree(struct tree *item)
209 {
21666f1 convert object type handling from a string to a number
Nicolas Pitre authored
210 enum object_type type;
bd2c39f [PATCH] don't load and decompress objects twice with parse_object()
Nicolas Pitre authored
211 void *buffer;
212 unsigned long size;
213
214 if (item->object.parsed)
215 return 0;
21666f1 convert object type handling from a string to a number
Nicolas Pitre authored
216 buffer = read_sha1_file(item->object.sha1, &type, &size);
bd2c39f [PATCH] don't load and decompress objects twice with parse_object()
Nicolas Pitre authored
217 if (!buffer)
218 return error("Could not read %s",
219 sha1_to_hex(item->object.sha1));
21666f1 convert object type handling from a string to a number
Nicolas Pitre authored
220 if (type != OBJ_TREE) {
bd2c39f [PATCH] don't load and decompress objects twice with parse_object()
Nicolas Pitre authored
221 free(buffer);
222 return error("Object %s not a tree",
223 sha1_to_hex(item->object.sha1));
224 }
136f2e5 Make "struct tree" contain the pointer to the tree buffer
Linus Torvalds authored
225 return parse_tree_buffer(item, buffer, size);
bd2c39f [PATCH] don't load and decompress objects twice with parse_object()
Nicolas Pitre authored
226 }
77675e2 [PATCH] Add a function for getting a struct tree for an ent.
Daniel Barkalow authored
227
228 struct tree *parse_tree_indirect(const unsigned char *sha1)
229 {
230 struct object *obj = parse_object(sha1);
231 do {
232 if (!obj)
233 return NULL;
1974632 Remove TYPE_* constant macros and use object_type enums consistently.
Linus Torvalds authored
234 if (obj->type == OBJ_TREE)
77675e2 [PATCH] Add a function for getting a struct tree for an ent.
Daniel Barkalow authored
235 return (struct tree *) obj;
1974632 Remove TYPE_* constant macros and use object_type enums consistently.
Linus Torvalds authored
236 else if (obj->type == OBJ_COMMIT)
77675e2 [PATCH] Add a function for getting a struct tree for an ent.
Daniel Barkalow authored
237 obj = &(((struct commit *) obj)->tree->object);
1974632 Remove TYPE_* constant macros and use object_type enums consistently.
Linus Torvalds authored
238 else if (obj->type == OBJ_TAG)
77675e2 [PATCH] Add a function for getting a struct tree for an ent.
Daniel Barkalow authored
239 obj = ((struct tag *) obj)->tagged;
240 else
241 return NULL;
242 if (!obj->parsed)
243 parse_object(obj->sha1);
244 } while (1);
245 }
Something went wrong with that request. Please try again.