Cleanup variables in cat-file
I want to add new command line options to cat-file, but to do that we need to change how we handle argv[] first. This is a simple cleanup that assigns names to the two arguments we currently care about. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Shawn O. Pearce committed
Apr 21, 2007 at 21:14 UTC
2b6854c863ae83dd8b4766a159a45a02e883a41f
1 file changed
+18
-12
builtin-cat-file.c
+18
-12
@@ -83,17 +83,21 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
83
void *buf;
84
unsigned long size;
85
int opt;
86
+ const char *exp_type, *obj_name;
87
88
git_config(git_default_config);
89
if (argc != 3)
90
usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");
90
- if (get_sha1(argv[2], sha1))
91
- die("Not a valid object name %s", argv[2]);
91
+ exp_type = argv[1];
92
+ obj_name = argv[2];
93
+
94
+ if (get_sha1(obj_name, sha1))
95
+ die("Not a valid object name %s", obj_name);
96
97
opt = 0;
94
- if ( argv[1][0] == '-' ) {
95
- opt = argv[1][1];
96
- if ( !opt || argv[1][2] )
98
+ if ( exp_type[0] == '-' ) {
99
+ opt = exp_type[1];
100
+ if ( !opt || exp_type[2] )
101
opt = -1; /* Not a single character option */
102
}
103
@@ -121,15 +125,17 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
125
case 'p':
126
type = sha1_object_info(sha1, NULL);
127
if (type < 0)
124
- die("Not a valid object name %s", argv[2]);
128
+ die("Not a valid object name %s", obj_name);
129
130
/* custom pretty-print here */
127
- if (type == OBJ_TREE)
128
- return cmd_ls_tree(2, argv + 1, NULL);
131
+ if (type == OBJ_TREE) {
132
+ const char *ls_args[3] = {"ls-tree", obj_name, NULL};
133
+ return cmd_ls_tree(2, ls_args, NULL);
134
+ }
135
136
buf = read_sha1_file(sha1, &type, &size);
137
if (!buf)
132
- die("Cannot read object %s", argv[2]);
138
+ die("Cannot read object %s", obj_name);
139
if (type == OBJ_TAG) {
140
pprint_tag(sha1, buf, size);
141
return 0;
@@ -138,15 +144,15 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
144
/* otherwise just spit out the data */
145
break;
146
case 0:
141
- buf = read_object_with_reference(sha1, argv[1], &size, NULL);
147
+ buf = read_object_with_reference(sha1, exp_type, &size, NULL);
148
break;
149
150
default:
145
- die("git-cat-file: unknown option: %s\n", argv[1]);
151
+ die("git-cat-file: unknown option: %s\n", exp_type);
152
}
153
154
if (!buf)
149
- die("git-cat-file %s: bad file", argv[2]);
155
+ die("git-cat-file %s: bad file", obj_name);
156
157
write_or_die(1, buf, size);
158
return 0;