builtin/cat-file: convert some static functions to struct object_id
Convert all of the static functions that are not callbacks to use struct object_id. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
brian m. carlson committed
Sep 5, 2016 at 20:07 UTC
63ecb99e0d05bd4b73d14f439e489e70176fad43
1 file changed
+25
-25
builtin/cat-file.c
+25
-25
@@ -23,7 +23,7 @@ struct batch_options {
23
static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
24
int unknown_type)
25
{
26
- unsigned char sha1[20];
26
+ struct object_id oid;
27
enum object_type type;
28
char *buf;
29
unsigned long size;
@@ -35,14 +35,14 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
35
if (unknown_type)
36
flags |= LOOKUP_UNKNOWN_OBJECT;
37
38
- if (get_sha1_with_context(obj_name, 0, sha1, &obj_context))
38
+ if (get_sha1_with_context(obj_name, 0, oid.hash, &obj_context))
39
die("Not a valid object name %s", obj_name);
40
41
buf = NULL;
42
switch (opt) {
43
case 't':
44
oi.typename = &sb;
45
- if (sha1_object_info_extended(sha1, &oi, flags) < 0)
45
+ if (sha1_object_info_extended(oid.hash, &oi, flags) < 0)
46
die("git cat-file: could not get object info");
47
if (sb.len) {
48
printf("%s\n", sb.buf);
@@ -53,24 +53,24 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
53
54
case 's':
55
oi.sizep = &size;
56
- if (sha1_object_info_extended(sha1, &oi, flags) < 0)
56
+ if (sha1_object_info_extended(oid.hash, &oi, flags) < 0)
57
die("git cat-file: could not get object info");
58
printf("%lu\n", size);
59
return 0;
60
61
case 'e':
62
- return !has_sha1_file(sha1);
62
+ return !has_object_file(&oid);
63
64
case 'c':
65
if (!obj_context.path[0])
66
die("git cat-file --textconv %s: <object> must be <sha1:path>",
67
obj_name);
68
69
- if (textconv_object(obj_context.path, obj_context.mode, sha1, 1, &buf, &size))
69
+ if (textconv_object(obj_context.path, obj_context.mode, oid.hash, 1, &buf, &size))
70
break;
71
72
case 'p':
73
- type = sha1_object_info(sha1, NULL);
73
+ type = sha1_object_info(oid.hash, NULL);
74
if (type < 0)
75
die("Not a valid object name %s", obj_name);
76
@@ -83,8 +83,8 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
83
}
84
85
if (type == OBJ_BLOB)
86
- return stream_blob_to_fd(1, sha1, NULL, 0);
87
- buf = read_sha1_file(sha1, &type, &size);
86
+ return stream_blob_to_fd(1, oid.hash, NULL, 0);
87
+ buf = read_sha1_file(oid.hash, &type, &size);
88
if (!buf)
89
die("Cannot read object %s", obj_name);
90
@@ -93,19 +93,19 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
93
94
case 0:
95
if (type_from_string(exp_type) == OBJ_BLOB) {
96
- unsigned char blob_sha1[20];
97
- if (sha1_object_info(sha1, NULL) == OBJ_TAG) {
98
- char *buffer = read_sha1_file(sha1, &type, &size);
96
+ struct object_id blob_oid;
97
+ if (sha1_object_info(oid.hash, NULL) == OBJ_TAG) {
98
+ char *buffer = read_sha1_file(oid.hash, &type, &size);
99
const char *target;
100
if (!skip_prefix(buffer, "object ", &target) ||
101
- get_sha1_hex(target, blob_sha1))
102
- die("%s not a valid tag", sha1_to_hex(sha1));
101
+ get_oid_hex(target, &blob_oid))
102
+ die("%s not a valid tag", oid_to_hex(&oid));
103
free(buffer);
104
} else
105
- hashcpy(blob_sha1, sha1);
105
+ oidcpy(&blob_oid, &oid);
106
107
- if (sha1_object_info(blob_sha1, NULL) == OBJ_BLOB)
108
- return stream_blob_to_fd(1, blob_sha1, NULL, 0);
107
+ if (sha1_object_info(blob_oid.hash, NULL) == OBJ_BLOB)
108
+ return stream_blob_to_fd(1, blob_oid.hash, NULL, 0);
109
/*
110
* we attempted to dereference a tag to a blob
111
* and failed; there may be new dereference
@@ -113,7 +113,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
113
* fall-back to the usual case.
114
*/
115
}
116
- buf = read_object_with_reference(sha1, exp_type, &size, NULL);
116
+ buf = read_object_with_reference(oid.hash, exp_type, &size, NULL);
117
break;
118
119
default:
@@ -233,28 +233,28 @@ static void batch_write(struct batch_options *opt, const void *data, int len)
233
234
static void print_object_or_die(struct batch_options *opt, struct expand_data *data)
235
{
236
- const unsigned char *sha1 = data->oid.hash;
236
+ const struct object_id *oid = &data->oid;
237
238
assert(data->info.typep);
239
240
if (data->type == OBJ_BLOB) {
241
if (opt->buffer_output)
242
fflush(stdout);
243
- if (stream_blob_to_fd(1, sha1, NULL, 0) < 0)
244
- die("unable to stream %s to stdout", sha1_to_hex(sha1));
243
+ if (stream_blob_to_fd(1, oid->hash, NULL, 0) < 0)
244
+ die("unable to stream %s to stdout", oid_to_hex(oid));
245
}
246
else {
247
enum object_type type;
248
unsigned long size;
249
void *contents;
250
251
- contents = read_sha1_file(sha1, &type, &size);
251
+ contents = read_sha1_file(oid->hash, &type, &size);
252
if (!contents)
253
- die("object %s disappeared", sha1_to_hex(sha1));
253
+ die("object %s disappeared", oid_to_hex(oid));
254
if (type != data->type)
255
- die("object %s changed type!?", sha1_to_hex(sha1));
255
+ die("object %s changed type!?", oid_to_hex(oid));
256
if (data->info.sizep && size != data->size)
257
- die("object %s changed size!?", sha1_to_hex(sha1));
257
+ die("object %s changed size!?", oid_to_hex(oid));
258
259
batch_write(opt, contents, size);
260
free(contents);