Skip to content

Commit 16a4c6e

Browse files
author
Junio C Hamano
committed
Merge branch 'lt/tree-2'
* lt/tree-2: fetch.c: do not call process_tree() from process_tree(). tree_entry(): new tree-walking helper function adjust to the rebased series by Linus. Remove "tree->entries" tree-entry list from tree parser Switch "read_tree_recursive()" over to tree-walk functionality Make "tree_entry" have a SHA1 instead of a union of object pointers Add raw tree buffer info to "struct tree" Remove last vestiges of generic tree_entry_list Convert fetch.c: process_tree() to raw tree walker Convert "mark_tree_uninteresting()" to raw tree walker Remove unused "zeropad" entry from tree_list_entry fsck-objects: avoid unnecessary tree_entry_list usage Remove "tree->entries" tree-entry list from tree parser builtin-read-tree.c: avoid tree_entry_list in prime_cache_tree_rec() Switch "read_tree_recursive()" over to tree-walk functionality Make "tree_entry" have a SHA1 instead of a union of object pointers Make "struct tree" contain the pointer to the tree buffer
2 parents f0679f4 + 6f9012b commit 16a4c6e

16 files changed

+293
-216
lines changed

blame.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ static void free_patch(struct patch *p)
149149
free(p);
150150
}
151151

152-
static int get_blob_sha1_internal(unsigned char *sha1, const char *base,
152+
static int get_blob_sha1_internal(const unsigned char *sha1, const char *base,
153153
int baselen, const char *pathname,
154154
unsigned mode, int stage);
155155

@@ -178,7 +178,7 @@ static int get_blob_sha1(struct tree *t, const char *pathname,
178178
return 0;
179179
}
180180

181-
static int get_blob_sha1_internal(unsigned char *sha1, const char *base,
181+
static int get_blob_sha1_internal(const unsigned char *sha1, const char *base,
182182
int baselen, const char *pathname,
183183
unsigned mode, int stage)
184184
{

builtin-grep.c

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -578,11 +578,9 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
578578
struct tree_desc *tree,
579579
const char *tree_name, const char *base)
580580
{
581-
unsigned mode;
582581
int len;
583582
int hit = 0;
584-
const char *path;
585-
const unsigned char *sha1;
583+
struct name_entry entry;
586584
char *down;
587585
char *path_buf = xmalloc(PATH_MAX + strlen(tree_name) + 100);
588586

@@ -597,36 +595,32 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
597595
}
598596
len = strlen(path_buf);
599597

600-
while (tree->size) {
601-
int pathlen;
602-
sha1 = tree_entry_extract(tree, &path, &mode);
603-
pathlen = strlen(path);
604-
strcpy(path_buf + len, path);
598+
while (tree_entry(tree, &entry)) {
599+
strcpy(path_buf + len, entry.path);
605600

606-
if (S_ISDIR(mode))
601+
if (S_ISDIR(entry.mode))
607602
/* Match "abc/" against pathspec to
608603
* decide if we want to descend into "abc"
609604
* directory.
610605
*/
611-
strcpy(path_buf + len + pathlen, "/");
606+
strcpy(path_buf + len + entry.pathlen, "/");
612607

613608
if (!pathspec_matches(paths, down))
614609
;
615-
else if (S_ISREG(mode))
616-
hit |= grep_sha1(opt, sha1, path_buf);
617-
else if (S_ISDIR(mode)) {
610+
else if (S_ISREG(entry.mode))
611+
hit |= grep_sha1(opt, entry.sha1, path_buf);
612+
else if (S_ISDIR(entry.mode)) {
618613
char type[20];
619614
struct tree_desc sub;
620615
void *data;
621-
data = read_sha1_file(sha1, type, &sub.size);
616+
data = read_sha1_file(entry.sha1, type, &sub.size);
622617
if (!data)
623618
die("unable to read tree (%s)",
624-
sha1_to_hex(sha1));
619+
sha1_to_hex(entry.sha1));
625620
sub.buf = data;
626621
hit |= grep_tree(opt, paths, &sub, tree_name, down);
627622
free(data);
628623
}
629-
update_tree_entry(tree);
630624
}
631625
return hit;
632626
}

builtin-ls-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static int show_recursive(const char *base, int baselen, const char *pathname)
5353
}
5454
}
5555

56-
static int show_tree(unsigned char *sha1, const char *base, int baselen,
56+
static int show_tree(const unsigned char *sha1, const char *base, int baselen,
5757
const char *pathname, unsigned mode, int stage)
5858
{
5959
int retval = 0;

builtin-read-tree.c

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "object.h"
1111
#include "tree.h"
12+
#include "tree-walk.h"
1213
#include "cache-tree.h"
1314
#include <sys/time.h>
1415
#include <signal.h>
@@ -29,7 +30,17 @@ static int merge_size = 0;
2930

3031
static struct object_list *trees = NULL;
3132

32-
static struct cache_entry df_conflict_entry = {
33+
static struct cache_entry df_conflict_entry = {
34+
};
35+
36+
struct tree_entry_list {
37+
struct tree_entry_list *next;
38+
unsigned directory : 1;
39+
unsigned executable : 1;
40+
unsigned symlink : 1;
41+
unsigned int mode;
42+
const char *name;
43+
const unsigned char *sha1;
3344
};
3445

3546
static struct tree_entry_list df_conflict_list = {
@@ -39,7 +50,35 @@ static struct tree_entry_list df_conflict_list = {
3950

4051
typedef int (*merge_fn_t)(struct cache_entry **src);
4152

42-
static int entcmp(char *name1, int dir1, char *name2, int dir2)
53+
static struct tree_entry_list *create_tree_entry_list(struct tree *tree)
54+
{
55+
struct tree_desc desc;
56+
struct name_entry one;
57+
struct tree_entry_list *ret = NULL;
58+
struct tree_entry_list **list_p = &ret;
59+
60+
desc.buf = tree->buffer;
61+
desc.size = tree->size;
62+
63+
while (tree_entry(&desc, &one)) {
64+
struct tree_entry_list *entry;
65+
66+
entry = xmalloc(sizeof(struct tree_entry_list));
67+
entry->name = one.path;
68+
entry->sha1 = one.sha1;
69+
entry->mode = one.mode;
70+
entry->directory = S_ISDIR(one.mode) != 0;
71+
entry->executable = (one.mode & S_IXUSR) != 0;
72+
entry->symlink = S_ISLNK(one.mode) != 0;
73+
entry->next = NULL;
74+
75+
*list_p = entry;
76+
list_p = &entry->next;
77+
}
78+
return ret;
79+
}
80+
81+
static int entcmp(const char *name1, int dir1, const char *name2, int dir2)
4382
{
4483
int len1 = strlen(name1);
4584
int len2 = strlen(name2);
@@ -67,7 +106,7 @@ static int unpack_trees_rec(struct tree_entry_list **posns, int len,
67106
int src_size = len + 1;
68107
do {
69108
int i;
70-
char *first;
109+
const char *first;
71110
int firstdir = 0;
72111
int pathlen;
73112
unsigned ce_size;
@@ -161,9 +200,10 @@ static int unpack_trees_rec(struct tree_entry_list **posns, int len,
161200
}
162201

163202
if (posns[i]->directory) {
203+
struct tree *tree = lookup_tree(posns[i]->sha1);
164204
any_dirs = 1;
165-
parse_tree(posns[i]->item.tree);
166-
subposns[i] = posns[i]->item.tree->entries;
205+
parse_tree(tree);
206+
subposns[i] = create_tree_entry_list(tree);
167207
posns[i] = posns[i]->next;
168208
src[i + merge] = &df_conflict_entry;
169209
continue;
@@ -187,7 +227,7 @@ static int unpack_trees_rec(struct tree_entry_list **posns, int len,
187227

188228
any_files = 1;
189229

190-
memcpy(ce->sha1, posns[i]->item.any->sha1, 20);
230+
memcpy(ce->sha1, posns[i]->sha1, 20);
191231
src[i + merge] = ce;
192232
subposns[i] = &df_conflict_list;
193233
posns[i] = posns[i]->next;
@@ -368,7 +408,7 @@ static int unpack_trees(merge_fn_t fn)
368408
if (len) {
369409
posns = xmalloc(len * sizeof(struct tree_entry_list *));
370410
for (i = 0; i < len; i++) {
371-
posns[i] = ((struct tree *) posn->item)->entries;
411+
posns[i] = create_tree_entry_list((struct tree *) posn->item);
372412
posn = posn->next;
373413
}
374414
if (unpack_trees_rec(posns, len, "", fn, &indpos))
@@ -775,19 +815,23 @@ static int read_cache_unmerged(void)
775815

776816
static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
777817
{
778-
struct tree_entry_list *ent;
818+
struct tree_desc desc;
819+
struct name_entry entry;
779820
int cnt;
780821

781822
memcpy(it->sha1, tree->object.sha1, 20);
782-
for (cnt = 0, ent = tree->entries; ent; ent = ent->next) {
783-
if (!ent->directory)
823+
desc.buf = tree->buffer;
824+
desc.size = tree->size;
825+
cnt = 0;
826+
while (tree_entry(&desc, &entry)) {
827+
if (!S_ISDIR(entry.mode))
784828
cnt++;
785829
else {
786830
struct cache_tree_sub *sub;
787-
struct tree *subtree = (struct tree *)ent->item.tree;
831+
struct tree *subtree = lookup_tree(entry.sha1);
788832
if (!subtree->object.parsed)
789833
parse_tree(subtree);
790-
sub = cache_tree_sub(it, ent->name);
834+
sub = cache_tree_sub(it, entry.path);
791835
sub->cache_tree = cache_tree();
792836
prime_cache_tree_rec(sub->cache_tree, subtree);
793837
cnt += sub->cache_tree->entry_count;

builtin-rev-list.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ static struct object_list **process_tree(struct tree *tree,
113113
const char *name)
114114
{
115115
struct object *obj = &tree->object;
116-
struct tree_entry_list *entry;
116+
struct tree_desc desc;
117+
struct name_entry entry;
117118
struct name_path me;
118119

119120
if (!revs.tree_objects)
@@ -128,18 +129,18 @@ static struct object_list **process_tree(struct tree *tree,
128129
me.up = path;
129130
me.elem = name;
130131
me.elem_len = strlen(name);
131-
entry = tree->entries;
132-
tree->entries = NULL;
133-
while (entry) {
134-
struct tree_entry_list *next = entry->next;
135-
if (entry->directory)
136-
p = process_tree(entry->item.tree, p, &me, entry->name);
132+
133+
desc.buf = tree->buffer;
134+
desc.size = tree->size;
135+
136+
while (tree_entry(&desc, &entry)) {
137+
if (S_ISDIR(entry.mode))
138+
p = process_tree(lookup_tree(entry.sha1), p, &me, name);
137139
else
138-
p = process_blob(entry->item.blob, p, &me, entry->name);
139-
free(entry->name);
140-
free(entry);
141-
entry = next;
140+
p = process_blob(lookup_blob(entry.sha1), p, &me, name);
142141
}
142+
free(tree->buffer);
143+
tree->buffer = NULL;
143144
return p;
144145
}
145146

builtin-tar-tree.c

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -271,30 +271,25 @@ static void write_global_extended_header(const unsigned char *sha1)
271271
static void traverse_tree(struct tree_desc *tree, struct strbuf *path)
272272
{
273273
int pathlen = path->len;
274+
struct name_entry entry;
274275

275-
while (tree->size) {
276-
const char *name;
277-
const unsigned char *sha1;
278-
unsigned mode;
276+
while (tree_entry(tree, &entry)) {
279277
void *eltbuf;
280278
char elttype[20];
281279
unsigned long eltsize;
282280

283-
sha1 = tree_entry_extract(tree, &name, &mode);
284-
update_tree_entry(tree);
285-
286-
eltbuf = read_sha1_file(sha1, elttype, &eltsize);
281+
eltbuf = read_sha1_file(entry.sha1, elttype, &eltsize);
287282
if (!eltbuf)
288-
die("cannot read %s", sha1_to_hex(sha1));
283+
die("cannot read %s", sha1_to_hex(entry.sha1));
289284

290285
path->len = pathlen;
291-
strbuf_append_string(path, name);
292-
if (S_ISDIR(mode))
286+
strbuf_append_string(path, entry.path);
287+
if (S_ISDIR(entry.mode))
293288
strbuf_append_string(path, "/");
294289

295-
write_entry(sha1, path, mode, eltbuf, eltsize);
290+
write_entry(entry.sha1, path, entry.mode, eltbuf, eltsize);
296291

297-
if (S_ISDIR(mode)) {
292+
if (S_ISDIR(entry.mode)) {
298293
struct tree_desc subtree;
299294
subtree.buf = eltbuf;
300295
subtree.size = eltsize;

fetch.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "cache.h"
44
#include "commit.h"
55
#include "tree.h"
6+
#include "tree-walk.h"
67
#include "tag.h"
78
#include "blob.h"
89
#include "refs.h"
@@ -37,21 +38,33 @@ static int process(struct object *obj);
3738

3839
static int process_tree(struct tree *tree)
3940
{
40-
struct tree_entry_list *entry;
41+
struct tree_desc desc;
42+
struct name_entry entry;
4143

4244
if (parse_tree(tree))
4345
return -1;
4446

45-
entry = tree->entries;
46-
tree->entries = NULL;
47-
while (entry) {
48-
struct tree_entry_list *next = entry->next;
49-
if (process(entry->item.any))
47+
desc.buf = tree->buffer;
48+
desc.size = tree->size;
49+
while (tree_entry(&desc, &entry)) {
50+
struct object *obj = NULL;
51+
52+
if (S_ISDIR(entry.mode)) {
53+
struct tree *tree = lookup_tree(entry.sha1);
54+
if (tree)
55+
obj = &tree->object;
56+
}
57+
else {
58+
struct blob *blob = lookup_blob(entry.sha1);
59+
if (blob)
60+
obj = &blob->object;
61+
}
62+
if (!obj || process(obj))
5063
return -1;
51-
free(entry->name);
52-
free(entry);
53-
entry = next;
5464
}
65+
free(tree->buffer);
66+
tree->buffer = NULL;
67+
tree->size = 0;
5568
return 0;
5669
}
5770

0 commit comments

Comments
 (0)