Skip to content

Commit

Permalink
Initialize tree descriptors with a helper function rather than by hand.
Browse files Browse the repository at this point in the history
This removes slightly more lines than it adds, but the real reason for
doing this is that future optimizations will require more setup of the
tree descriptor, and so we want to do it in one place.

Also renamed the "desc.buf" field to "desc.buffer" just to trigger
compiler errors for old-style manual initializations, making sure I
didn't miss anything.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
  • Loading branch information
Linus Torvalds authored and Junio C Hamano committed Mar 21, 2007
1 parent a8c4047 commit 6fda5e5
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 62 deletions.
5 changes: 2 additions & 3 deletions builtin-fsck.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,7 @@ static int fsck_tree(struct tree *item)
const char *o_name;
const unsigned char *o_sha1;

desc.buf = item->buffer;
desc.size = item->size;
init_tree_desc(&desc, item->buffer, item->size);

o_mode = 0;
o_name = NULL;
Expand All @@ -242,7 +241,7 @@ static int fsck_tree(struct tree *item)

if (strchr(name, '/'))
has_full_path = 1;
has_zero_pad |= *(char *)desc.buf == '0';
has_zero_pad |= *(char *)desc.buffer == '0';
update_tree_entry(&desc);

switch (mode) {
Expand Down
11 changes: 7 additions & 4 deletions builtin-grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,11 +388,13 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
enum object_type type;
struct tree_desc sub;
void *data;
data = read_sha1_file(entry.sha1, &type, &sub.size);
unsigned long size;

data = read_sha1_file(entry.sha1, &type, &size);
if (!data)
die("unable to read tree (%s)",
sha1_to_hex(entry.sha1));
sub.buf = data;
init_tree_desc(&sub, data, size);
hit |= grep_tree(opt, paths, &sub, tree_name, down);
free(data);
}
Expand All @@ -408,12 +410,13 @@ static int grep_object(struct grep_opt *opt, const char **paths,
if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
struct tree_desc tree;
void *data;
unsigned long size;
int hit;
data = read_object_with_reference(obj->sha1, tree_type,
&tree.size, NULL);
&size, NULL);
if (!data)
die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
tree.buf = data;
init_tree_desc(&tree, data, size);
hit = grep_tree(opt, paths, &tree, name, "");
free(data);
return hit;
Expand Down
6 changes: 2 additions & 4 deletions builtin-pack-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -873,8 +873,7 @@ static void add_pbase_object(struct tree_desc *tree,
tree = pbase_tree_get(entry.sha1);
if (!tree)
return;
sub.buf = tree->tree_data;
sub.size = tree->tree_size;
init_tree_desc(&sub, tree->tree_data, tree->tree_size);

add_pbase_object(&sub, down, downlen, fullname);
pbase_tree_put(tree);
Expand Down Expand Up @@ -937,8 +936,7 @@ static void add_preferred_base_object(const char *name, unsigned hash)
}
else {
struct tree_desc tree;
tree.buf = it->pcache.tree_data;
tree.size = it->pcache.tree_size;
init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);
add_pbase_object(&tree, name, cmplen, name);
}
}
Expand Down
3 changes: 1 addition & 2 deletions builtin-read-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
int cnt;

hashcpy(it->sha1, tree->object.sha1);
desc.buf = tree->buffer;
desc.size = tree->size;
init_tree_desc(&desc, tree->buffer, tree->size);
cnt = 0;
while (tree_entry(&desc, &entry)) {
if (!S_ISDIR(entry.mode))
Expand Down
10 changes: 5 additions & 5 deletions builtin-reflog.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,18 @@ static int tree_is_complete(const unsigned char *sha1)
if (tree->object.flags & INCOMPLETE)
return 0;

desc.buf = tree->buffer;
desc.size = tree->size;
if (!desc.buf) {
if (!tree->buffer) {
enum object_type type;
void *data = read_sha1_file(sha1, &type, &desc.size);
unsigned long size;
void *data = read_sha1_file(sha1, &type, &size);
if (!data) {
tree->object.flags |= INCOMPLETE;
return 0;
}
desc.buf = data;
tree->buffer = data;
tree->size = size;
}
init_tree_desc(&desc, tree->buffer, tree->size);
complete = 1;
while (tree_entry(&desc, &entry)) {
if (!has_sha1_file(entry.sha1) ||
Expand Down
3 changes: 1 addition & 2 deletions fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ static int process_tree(struct tree *tree)
if (parse_tree(tree))
return -1;

desc.buf = tree->buffer;
desc.size = tree->size;
init_tree_desc(&desc, tree->buffer, tree->size);
while (tree_entry(&desc, &entry)) {
struct object *obj = NULL;

Expand Down
3 changes: 1 addition & 2 deletions http-push.c
Original file line number Diff line number Diff line change
Expand Up @@ -1750,8 +1750,7 @@ static struct object_list **process_tree(struct tree *tree,
me.elem = name;
me.elem_len = strlen(name);

desc.buf = tree->buffer;
desc.size = tree->size;
init_tree_desc(&desc, tree->buffer, tree->size);

while (tree_entry(&desc, &entry)) {
if (S_ISDIR(entry.mode))
Expand Down
3 changes: 1 addition & 2 deletions list-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ static void process_tree(struct rev_info *revs,
me.elem = name;
me.elem_len = strlen(name);

desc.buf = tree->buffer;
desc.size = tree->size;
init_tree_desc(&desc, tree->buffer, tree->size);

while (tree_entry(&desc, &entry)) {
if (S_ISDIR(entry.mode))
Expand Down
3 changes: 1 addition & 2 deletions reachable.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ static void process_tree(struct tree *tree,
me.elem = name;
me.elem_len = strlen(name);

desc.buf = tree->buffer;
desc.size = tree->size;
init_tree_desc(&desc, tree->buffer, tree->size);

while (tree_entry(&desc, &entry)) {
if (S_ISDIR(entry.mode))
Expand Down
12 changes: 5 additions & 7 deletions revision.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ void mark_tree_uninteresting(struct tree *tree)
if (parse_tree(tree) < 0)
die("bad tree %s", sha1_to_hex(obj->sha1));

desc.buf = tree->buffer;
desc.size = tree->size;
init_tree_desc(&desc, tree->buffer, tree->size);
while (tree_entry(&desc, &entry)) {
if (S_ISDIR(entry.mode))
mark_tree_uninteresting(lookup_tree(entry.sha1));
Expand Down Expand Up @@ -275,18 +274,17 @@ int rev_same_tree_as_empty(struct rev_info *revs, struct tree *t1)
{
int retval;
void *tree;
unsigned long size;
struct tree_desc empty, real;

if (!t1)
return 0;

tree = read_object_with_reference(t1->object.sha1, tree_type, &real.size, NULL);
tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
if (!tree)
return 0;
real.buf = tree;

empty.buf = "";
empty.size = 0;
init_tree_desc(&real, tree, size);
init_tree_desc(&empty, "", 0);

tree_difference = REV_TREE_SAME;
revs->pruning.has_changes = 0;
Expand Down
22 changes: 12 additions & 10 deletions tree-diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,13 @@ static void show_entry(struct diff_options *opt, const char *prefix, struct tree
char *newbase = malloc_base(base, baselen, path, pathlen);
struct tree_desc inner;
void *tree;
unsigned long size;

tree = read_sha1_file(sha1, &type, &inner.size);
tree = read_sha1_file(sha1, &type, &size);
if (!tree || type != OBJ_TREE)
die("corrupt tree sha %s", sha1_to_hex(sha1));

inner.buf = tree;
init_tree_desc(&inner, tree, size);
show_tree(opt, prefix, &inner, newbase, baselen + 1 + pathlen);

free(tree);
Expand Down Expand Up @@ -227,16 +228,17 @@ int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const cha
{
void *tree1, *tree2;
struct tree_desc t1, t2;
unsigned long size1, size2;
int retval;

tree1 = read_object_with_reference(old, tree_type, &t1.size, NULL);
tree1 = read_object_with_reference(old, tree_type, &size1, NULL);
if (!tree1)
die("unable to read source tree (%s)", sha1_to_hex(old));
tree2 = read_object_with_reference(new, tree_type, &t2.size, NULL);
tree2 = read_object_with_reference(new, tree_type, &size2, NULL);
if (!tree2)
die("unable to read destination tree (%s)", sha1_to_hex(new));
t1.buf = tree1;
t2.buf = tree2;
init_tree_desc(&t1, tree1, size1);
init_tree_desc(&t2, tree2, size2);
retval = diff_tree(&t1, &t2, base, opt);
free(tree1);
free(tree2);
Expand All @@ -247,15 +249,15 @@ int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_
{
int retval;
void *tree;
unsigned long size;
struct tree_desc empty, real;

tree = read_object_with_reference(new, tree_type, &real.size, NULL);
tree = read_object_with_reference(new, tree_type, &size, NULL);
if (!tree)
die("unable to read root tree (%s)", sha1_to_hex(new));
real.buf = tree;
init_tree_desc(&real, tree, size);

empty.size = 0;
empty.buf = "";
init_tree_desc(&empty, "", 0);
retval = diff_tree(&empty, &real, base, opt);
free(tree);
return retval;
Expand Down
24 changes: 15 additions & 9 deletions tree-walk.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
#include "tree-walk.h"
#include "tree.h"

void init_tree_desc(struct tree_desc *desc, const void *buffer, unsigned long size)
{
desc->buffer = buffer;
desc->size = size;
}

void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1)
{
unsigned long size = 0;
Expand All @@ -12,8 +18,7 @@ void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1)
if (!buf)
die("unable to read tree %s", sha1_to_hex(sha1));
}
desc->size = size;
desc->buf = buf;
init_tree_desc(desc, buf, size);
return buf;
}

Expand All @@ -36,13 +41,13 @@ static void entry_extract(struct tree_desc *t, struct name_entry *a)

void update_tree_entry(struct tree_desc *desc)
{
const void *buf = desc->buf;
const void *buf = desc->buffer;
unsigned long size = desc->size;
int len = strlen(buf) + 1 + 20;

if (size < len)
die("corrupt tree file");
desc->buf = (char *) buf + len;
desc->buffer = (char *) buf + len;
desc->size = size - len;
}

Expand All @@ -62,7 +67,7 @@ static const char *get_mode(const char *str, unsigned int *modep)

const unsigned char *tree_entry_extract(struct tree_desc *desc, const char **pathp, unsigned int *modep)
{
const void *tree = desc->buf;
const void *tree = desc->buffer;
unsigned long size = desc->size;
int len = strlen(tree)+1;
const unsigned char *sha1 = (unsigned char *) tree + len;
Expand All @@ -79,7 +84,7 @@ const unsigned char *tree_entry_extract(struct tree_desc *desc, const char **pat

int tree_entry(struct tree_desc *desc, struct name_entry *entry)
{
const void *tree = desc->buf;
const void *tree = desc->buffer;
const char *path;
unsigned long len, size = desc->size;

Expand All @@ -101,7 +106,7 @@ int tree_entry(struct tree_desc *desc, struct name_entry *entry)
if (len > size)
die("corrupt tree file");

desc->buf = path;
desc->buffer = path;
desc->size = size - len;
return 1;
}
Expand Down Expand Up @@ -196,10 +201,11 @@ int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned ch
{
int retval;
void *tree;
unsigned long size;
struct tree_desc t;
unsigned char root[20];

tree = read_object_with_reference(tree_sha1, tree_type, &t.size, root);
tree = read_object_with_reference(tree_sha1, tree_type, &size, root);
if (!tree)
return -1;

Expand All @@ -208,7 +214,7 @@ int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned ch
return 0;
}

t.buf = tree;
init_tree_desc(&t, tree, size);
retval = find_tree_entry(&t, name, sha1, mode);
free(tree);
return retval;
Expand Down
5 changes: 3 additions & 2 deletions tree-walk.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#define TREE_WALK_H

struct tree_desc {
const void *buf;
unsigned long size;
const void *buffer;
unsigned int size;
};

struct name_entry {
Expand All @@ -18,6 +18,7 @@ static inline int tree_entry_len(const char *name, const unsigned char *sha1)
}

void update_tree_entry(struct tree_desc *);
void init_tree_desc(struct tree_desc *desc, const void *buf, unsigned long size);
const unsigned char *tree_entry_extract(struct tree_desc *, const char **, unsigned int *);

/* Helper function that does both of the above and returns true for success */
Expand Down
9 changes: 3 additions & 6 deletions tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ int read_tree_recursive(struct tree *tree,
if (parse_tree(tree))
return -1;

desc.buf = tree->buffer;
desc.size = tree->size;
init_tree_desc(&desc, tree->buffer, tree->size);

while (tree_entry(&desc, &entry)) {
if (!match_tree_entry(base, baselen, entry.path, entry.mode, match))
Expand Down Expand Up @@ -152,16 +151,14 @@ static void track_tree_refs(struct tree *item)
struct name_entry entry;

/* Count how many entries there are.. */
desc.buf = item->buffer;
desc.size = item->size;
init_tree_desc(&desc, item->buffer, item->size);
while (tree_entry(&desc, &entry))
n_refs++;

/* Allocate object refs and walk it again.. */
i = 0;
refs = alloc_object_refs(n_refs);
desc.buf = item->buffer;
desc.size = item->size;
init_tree_desc(&desc, item->buffer, item->size);
while (tree_entry(&desc, &entry)) {
struct object *obj;

Expand Down
3 changes: 1 addition & 2 deletions unpack-trees.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ static struct tree_entry_list *create_tree_entry_list(struct tree *tree)
if (!tree->object.parsed)
parse_tree(tree);

desc.buf = tree->buffer;
desc.size = tree->size;
init_tree_desc(&desc, tree->buffer, tree->size);

while (tree_entry(&desc, &one)) {
struct tree_entry_list *entry;
Expand Down

0 comments on commit 6fda5e5

Please sign in to comment.