Skip to content

Commit 6fda5e5

Browse files
torvaldsJunio C Hamano
authored andcommitted
Initialize tree descriptors with a helper function rather than by hand.
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>
1 parent a8c4047 commit 6fda5e5

15 files changed

+60
-62
lines changed

builtin-fsck.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,7 @@ static int fsck_tree(struct tree *item)
227227
const char *o_name;
228228
const unsigned char *o_sha1;
229229

230-
desc.buf = item->buffer;
231-
desc.size = item->size;
230+
init_tree_desc(&desc, item->buffer, item->size);
232231

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

243242
if (strchr(name, '/'))
244243
has_full_path = 1;
245-
has_zero_pad |= *(char *)desc.buf == '0';
244+
has_zero_pad |= *(char *)desc.buffer == '0';
246245
update_tree_entry(&desc);
247246

248247
switch (mode) {

builtin-grep.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,13 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
388388
enum object_type type;
389389
struct tree_desc sub;
390390
void *data;
391-
data = read_sha1_file(entry.sha1, &type, &sub.size);
391+
unsigned long size;
392+
393+
data = read_sha1_file(entry.sha1, &type, &size);
392394
if (!data)
393395
die("unable to read tree (%s)",
394396
sha1_to_hex(entry.sha1));
395-
sub.buf = data;
397+
init_tree_desc(&sub, data, size);
396398
hit |= grep_tree(opt, paths, &sub, tree_name, down);
397399
free(data);
398400
}
@@ -408,12 +410,13 @@ static int grep_object(struct grep_opt *opt, const char **paths,
408410
if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
409411
struct tree_desc tree;
410412
void *data;
413+
unsigned long size;
411414
int hit;
412415
data = read_object_with_reference(obj->sha1, tree_type,
413-
&tree.size, NULL);
416+
&size, NULL);
414417
if (!data)
415418
die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
416-
tree.buf = data;
419+
init_tree_desc(&tree, data, size);
417420
hit = grep_tree(opt, paths, &tree, name, "");
418421
free(data);
419422
return hit;

builtin-pack-objects.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -873,8 +873,7 @@ static void add_pbase_object(struct tree_desc *tree,
873873
tree = pbase_tree_get(entry.sha1);
874874
if (!tree)
875875
return;
876-
sub.buf = tree->tree_data;
877-
sub.size = tree->tree_size;
876+
init_tree_desc(&sub, tree->tree_data, tree->tree_size);
878877

879878
add_pbase_object(&sub, down, downlen, fullname);
880879
pbase_tree_put(tree);
@@ -937,8 +936,7 @@ static void add_preferred_base_object(const char *name, unsigned hash)
937936
}
938937
else {
939938
struct tree_desc tree;
940-
tree.buf = it->pcache.tree_data;
941-
tree.size = it->pcache.tree_size;
939+
init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);
942940
add_pbase_object(&tree, name, cmplen, name);
943941
}
944942
}

builtin-read-tree.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
5555
int cnt;
5656

5757
hashcpy(it->sha1, tree->object.sha1);
58-
desc.buf = tree->buffer;
59-
desc.size = tree->size;
58+
init_tree_desc(&desc, tree->buffer, tree->size);
6059
cnt = 0;
6160
while (tree_entry(&desc, &entry)) {
6261
if (!S_ISDIR(entry.mode))

builtin-reflog.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ static int tree_is_complete(const unsigned char *sha1)
5252
if (tree->object.flags & INCOMPLETE)
5353
return 0;
5454

55-
desc.buf = tree->buffer;
56-
desc.size = tree->size;
57-
if (!desc.buf) {
55+
if (!tree->buffer) {
5856
enum object_type type;
59-
void *data = read_sha1_file(sha1, &type, &desc.size);
57+
unsigned long size;
58+
void *data = read_sha1_file(sha1, &type, &size);
6059
if (!data) {
6160
tree->object.flags |= INCOMPLETE;
6261
return 0;
6362
}
64-
desc.buf = data;
6563
tree->buffer = data;
64+
tree->size = size;
6665
}
66+
init_tree_desc(&desc, tree->buffer, tree->size);
6767
complete = 1;
6868
while (tree_entry(&desc, &entry)) {
6969
if (!has_sha1_file(entry.sha1) ||

fetch.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ static int process_tree(struct tree *tree)
4242
if (parse_tree(tree))
4343
return -1;
4444

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

http-push.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,8 +1750,7 @@ static struct object_list **process_tree(struct tree *tree,
17501750
me.elem = name;
17511751
me.elem_len = strlen(name);
17521752

1753-
desc.buf = tree->buffer;
1754-
desc.size = tree->size;
1753+
init_tree_desc(&desc, tree->buffer, tree->size);
17551754

17561755
while (tree_entry(&desc, &entry)) {
17571756
if (S_ISDIR(entry.mode))

list-objects.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ static void process_tree(struct rev_info *revs,
4949
me.elem = name;
5050
me.elem_len = strlen(name);
5151

52-
desc.buf = tree->buffer;
53-
desc.size = tree->size;
52+
init_tree_desc(&desc, tree->buffer, tree->size);
5453

5554
while (tree_entry(&desc, &entry)) {
5655
if (S_ISDIR(entry.mode))

reachable.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ static void process_tree(struct tree *tree,
4242
me.elem = name;
4343
me.elem_len = strlen(name);
4444

45-
desc.buf = tree->buffer;
46-
desc.size = tree->size;
45+
init_tree_desc(&desc, tree->buffer, tree->size);
4746

4847
while (tree_entry(&desc, &entry)) {
4948
if (S_ISDIR(entry.mode))

revision.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ void mark_tree_uninteresting(struct tree *tree)
6262
if (parse_tree(tree) < 0)
6363
die("bad tree %s", sha1_to_hex(obj->sha1));
6464

65-
desc.buf = tree->buffer;
66-
desc.size = tree->size;
65+
init_tree_desc(&desc, tree->buffer, tree->size);
6766
while (tree_entry(&desc, &entry)) {
6867
if (S_ISDIR(entry.mode))
6968
mark_tree_uninteresting(lookup_tree(entry.sha1));
@@ -275,18 +274,17 @@ int rev_same_tree_as_empty(struct rev_info *revs, struct tree *t1)
275274
{
276275
int retval;
277276
void *tree;
277+
unsigned long size;
278278
struct tree_desc empty, real;
279279

280280
if (!t1)
281281
return 0;
282282

283-
tree = read_object_with_reference(t1->object.sha1, tree_type, &real.size, NULL);
283+
tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
284284
if (!tree)
285285
return 0;
286-
real.buf = tree;
287-
288-
empty.buf = "";
289-
empty.size = 0;
286+
init_tree_desc(&real, tree, size);
287+
init_tree_desc(&empty, "", 0);
290288

291289
tree_difference = REV_TREE_SAME;
292290
revs->pruning.has_changes = 0;

0 commit comments

Comments
 (0)