Skip to content

Commit 21666f1

Browse files
Nicolas PitreJunio C Hamano
authored andcommitted
convert object type handling from a string to a number
We currently have two parallel notation for dealing with object types in the code: a string and a numerical value. One of them is obviously redundent, and the most used one requires more stack space and a bunch of strcmp() all over the place. This is an initial step for the removal of the version using a char array found in object reading code paths. The patch is unfortunately large but there is no sane way to split it in smaller parts without breaking the system. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent df84366 commit 21666f1

37 files changed

Lines changed: 266 additions & 290 deletions

archive-tar.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ static int write_tar_entry(const unsigned char *sha1,
262262
static struct strbuf path;
263263
int filenamelen = strlen(filename);
264264
void *buffer;
265-
char type[20];
265+
enum object_type type;
266266
unsigned long size;
267267

268268
if (!path.alloc) {
@@ -283,7 +283,7 @@ static int write_tar_entry(const unsigned char *sha1,
283283
buffer = NULL;
284284
size = 0;
285285
} else {
286-
buffer = read_sha1_file(sha1, type, &size);
286+
buffer = read_sha1_file(sha1, &type, &size);
287287
if (!buffer)
288288
die("cannot read %s", sha1_to_hex(sha1));
289289
}

archive-zip.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ static int write_zip_entry(const unsigned char *sha1,
167167
int pathlen;
168168
unsigned char *out;
169169
char *path;
170-
char type[20];
170+
enum object_type type;
171171
void *buffer = NULL;
172172
void *deflated = NULL;
173173

@@ -195,7 +195,7 @@ static int write_zip_entry(const unsigned char *sha1,
195195
if (S_ISREG(mode) && zlib_compression_level != 0)
196196
method = 8;
197197
result = 0;
198-
buffer = read_sha1_file(sha1, type, &size);
198+
buffer = read_sha1_file(sha1, &type, &size);
199199
if (!buffer)
200200
die("cannot read %s", sha1_to_hex(sha1));
201201
crc = crc32(crc, buffer, size);

blob.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ int parse_blob_buffer(struct blob *item, void *buffer, unsigned long size)
3030

3131
int parse_blob(struct blob *item)
3232
{
33-
char type[20];
33+
enum object_type type;
3434
void *buffer;
3535
unsigned long size;
3636
int ret;
3737

3838
if (item->object.parsed)
3939
return 0;
40-
buffer = read_sha1_file(item->object.sha1, type, &size);
40+
buffer = read_sha1_file(item->object.sha1, &type, &size);
4141
if (!buffer)
4242
return error("Could not read %s",
4343
sha1_to_hex(item->object.sha1));
44-
if (strcmp(type, blob_type))
44+
if (type != OBJ_BLOB)
4545
return error("Object %s not a blob",
4646
sha1_to_hex(item->object.sha1));
4747
ret = parse_blob_buffer(item, buffer, size);

builtin-apply.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,11 +1912,11 @@ static int apply_binary(struct buffer_desc *desc, struct patch *patch)
19121912

19131913
if (has_sha1_file(sha1)) {
19141914
/* We already have the postimage */
1915-
char type[10];
1915+
enum object_type type;
19161916
unsigned long size;
19171917

19181918
free(desc->buffer);
1919-
desc->buffer = read_sha1_file(sha1, type, &size);
1919+
desc->buffer = read_sha1_file(sha1, &type, &size);
19201920
if (!desc->buffer)
19211921
return error("the necessary postimage %s for "
19221922
"'%s' cannot be read",
@@ -1972,8 +1972,8 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *
19721972
buf = NULL;
19731973
if (cached) {
19741974
if (ce) {
1975-
char type[20];
1976-
buf = read_sha1_file(ce->sha1, type, &size);
1975+
enum object_type type;
1976+
buf = read_sha1_file(ce->sha1, &type, &size);
19771977
if (!buf)
19781978
return error("read of %s failed",
19791979
patch->old_name);

builtin-blame.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ struct origin {
8787
static char *fill_origin_blob(struct origin *o, mmfile_t *file)
8888
{
8989
if (!o->file.ptr) {
90-
char type[10];
90+
enum object_type type;
9191
num_read_blob++;
92-
file->ptr = read_sha1_file(o->blob_sha1, type,
92+
file->ptr = read_sha1_file(o->blob_sha1, &type,
9393
(unsigned long *)(&(file->size)));
9494
o->file = *file;
9595
}
@@ -263,16 +263,14 @@ static struct origin *get_origin(struct scoreboard *sb,
263263
static int fill_blob_sha1(struct origin *origin)
264264
{
265265
unsigned mode;
266-
char type[10];
267266

268267
if (!is_null_sha1(origin->blob_sha1))
269268
return 0;
270269
if (get_tree_entry(origin->commit->object.sha1,
271270
origin->path,
272271
origin->blob_sha1, &mode))
273272
goto error_out;
274-
if (sha1_object_info(origin->blob_sha1, type, NULL) ||
275-
strcmp(type, blob_type))
273+
if (sha1_object_info(origin->blob_sha1, NULL) != OBJ_BLOB)
276274
goto error_out;
277275
return 0;
278276
error_out:
@@ -1322,10 +1320,10 @@ static void get_commit_info(struct commit *commit,
13221320
* we now need to populate them for output.
13231321
*/
13241322
if (!commit->buffer) {
1325-
char type[20];
1323+
enum object_type type;
13261324
unsigned long size;
13271325
commit->buffer =
1328-
read_sha1_file(commit->object.sha1, type, &size);
1326+
read_sha1_file(commit->object.sha1, &type, &size);
13291327
}
13301328
ret->author = author_buf;
13311329
get_ac_line(commit->buffer, "\nauthor ",
@@ -2006,7 +2004,7 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con
20062004
buf[fin_size] = 0;
20072005
origin->file.ptr = buf;
20082006
origin->file.size = fin_size;
2009-
pretend_sha1_file(buf, fin_size, blob_type, origin->blob_sha1);
2007+
pretend_sha1_file(buf, fin_size, OBJ_BLOB, origin->blob_sha1);
20102008
commit->util = origin;
20112009

20122010
/*
@@ -2068,7 +2066,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
20682066
int show_stats = 0;
20692067
const char *revs_file = NULL;
20702068
const char *final_commit_name = NULL;
2071-
char type[10];
2069+
enum object_type type;
20722070
const char *bottomtop = NULL;
20732071
const char *contents_from = NULL;
20742072

@@ -2302,7 +2300,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
23022300
if (fill_blob_sha1(o))
23032301
die("no such path %s in %s", path, final_commit_name);
23042302

2305-
sb.final_buf = read_sha1_file(o->blob_sha1, type,
2303+
sb.final_buf = read_sha1_file(o->blob_sha1, &type,
23062304
&sb.final_buf_size);
23072305
}
23082306
num_read_blob++;

builtin-cat-file.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long
7979
int cmd_cat_file(int argc, const char **argv, const char *prefix)
8080
{
8181
unsigned char sha1[20];
82-
char type[20];
82+
enum object_type type;
8383
void *buf;
8484
unsigned long size;
8585
int opt;
@@ -100,14 +100,16 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
100100
buf = NULL;
101101
switch (opt) {
102102
case 't':
103-
if (!sha1_object_info(sha1, type, NULL)) {
104-
printf("%s\n", type);
103+
type = sha1_object_info(sha1, NULL);
104+
if (type > 0) {
105+
printf("%s\n", typename(type));
105106
return 0;
106107
}
107108
break;
108109

109110
case 's':
110-
if (!sha1_object_info(sha1, type, &size)) {
111+
type = sha1_object_info(sha1, &size);
112+
if (type > 0) {
111113
printf("%lu\n", size);
112114
return 0;
113115
}
@@ -117,17 +119,18 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
117119
return !has_sha1_file(sha1);
118120

119121
case 'p':
120-
if (sha1_object_info(sha1, type, NULL))
122+
type = sha1_object_info(sha1, NULL);
123+
if (type < 0)
121124
die("Not a valid object name %s", argv[2]);
122125

123126
/* custom pretty-print here */
124-
if (!strcmp(type, tree_type))
127+
if (type == OBJ_TREE)
125128
return cmd_ls_tree(2, argv + 1, NULL);
126129

127-
buf = read_sha1_file(sha1, type, &size);
130+
buf = read_sha1_file(sha1, &type, &size);
128131
if (!buf)
129132
die("Cannot read object %s", argv[2]);
130-
if (!strcmp(type, tag_type)) {
133+
if (type == OBJ_TAG) {
131134
pprint_tag(sha1, buf, size);
132135
return 0;
133136
}

builtin-commit-tree.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,10 @@ static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...)
4747

4848
static void check_valid(unsigned char *sha1, const char *expect)
4949
{
50-
char type[20];
51-
52-
if (sha1_object_info(sha1, type, NULL))
50+
enum object_type type = sha1_object_info(sha1, NULL);
51+
if (type < 0)
5352
die("%s is not a valid object", sha1_to_hex(sha1));
54-
if (expect && strcmp(type, expect))
53+
if (expect && type != type_from_string(expect))
5554
die("%s is not a valid '%s' object", sha1_to_hex(sha1),
5655
expect);
5756
}

builtin-for-each-ref.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ static void verify_format(const char *format)
173173
*/
174174
static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned long *sz, int *eaten)
175175
{
176-
char type[20];
177-
void *buf = read_sha1_file(sha1, type, sz);
176+
enum object_type type;
177+
void *buf = read_sha1_file(sha1, &type, sz);
178178

179179
if (buf)
180180
*obj = parse_object_buffer(sha1, type, *sz, buf, eaten);

builtin-grep.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char
8484
{
8585
unsigned long size;
8686
char *data;
87-
char type[20];
87+
enum object_type type;
8888
char *to_free = NULL;
8989
int hit;
9090

91-
data = read_sha1_file(sha1, type, &size);
91+
data = read_sha1_file(sha1, &type, &size);
9292
if (!data) {
9393
error("'%s': unable to read %s", name, sha1_to_hex(sha1));
9494
return 0;
@@ -380,10 +380,10 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
380380
else if (S_ISREG(entry.mode))
381381
hit |= grep_sha1(opt, entry.sha1, path_buf, tn_len);
382382
else if (S_ISDIR(entry.mode)) {
383-
char type[20];
383+
enum object_type type;
384384
struct tree_desc sub;
385385
void *data;
386-
data = read_sha1_file(entry.sha1, type, &sub.size);
386+
data = read_sha1_file(entry.sha1, &type, &sub.size);
387387
if (!data)
388388
die("unable to read tree (%s)",
389389
sha1_to_hex(entry.sha1));

builtin-log.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)
8989
static int show_object(const unsigned char *sha1, int suppress_header)
9090
{
9191
unsigned long size;
92-
char type[20];
93-
char *buf = read_sha1_file(sha1, type, &size);
92+
enum object_type type;
93+
char *buf = read_sha1_file(sha1, &type, &size);
9494
int offset = 0;
9595

9696
if (!buf)

0 commit comments

Comments
 (0)