@matheusbsbs / matheus-bagliano / commits / 9cf71b176c
Fork 0

git-cat-file: Small refactor of cmd_cat_file

I separated the logic of parsing the arguments from the logic of fetching and outputting the data. cat_one_file now does the latter. Signed-off-by: Adam Roben <aroben@apple.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Adam Roben committed Apr 23, 2008 at 15:17 UTC 9cf71b176cb175589c3ea828f7364893cc45b82f
1 file changed +22 -16
builtin-cat-file.c
+22 -16
@@ -76,31 +76,16 @@ static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long
76 write_or_die(1, cp, endp - cp);
77 }
78
79 -int cmd_cat_file(int argc, const char **argv, const char *prefix)
79 +static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
80 {
81 unsigned char sha1[20];
82 enum object_type type;
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>");
91 - exp_type = argv[1];
92 - obj_name = argv[2];
85
86 if (get_sha1(obj_name, sha1))
87 die("Not a valid object name %s", obj_name);
88
97 - opt = 0;
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 -
89 buf = NULL;
90 switch (opt) {
91 case 't':
@@ -157,3 +142,24 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
142 write_or_die(1, buf, size);
143 return 0;
144 }
145 +
146 +int cmd_cat_file(int argc, const char **argv, const char *prefix)
147 +{
148 + int opt;
149 + const char *exp_type, *obj_name;
150 +
151 + git_config(git_default_config);
152 + if (argc != 3)
153 + usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");
154 + exp_type = argv[1];
155 + obj_name = argv[2];
156 +
157 + opt = 0;
158 + if ( exp_type[0] == '-' ) {
159 + opt = exp_type[1];
160 + if ( !opt || exp_type[2] )
161 + opt = -1; /* Not a single character option */
162 + }
163 +
164 + return cat_one_file(opt, exp_type, obj_name);
165 +}