@matheusbsbs / matheus-bagliano / commits / 548601adcc
Fork 0

use parse_options() in git hash-object

Signed-off-by: Dmitry Potapov <dpotapov@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Dmitry Potapov committed Aug 3, 2008 at 18:36 UTC 548601adcc638d96486e0f2a3dd399d4ca215eca
1 file changed +53 -69
hash-object.c
+53 -69
@@ -7,6 +7,7 @@
7 #include "cache.h"
8 #include "blob.h"
9 #include "quote.h"
10 +#include "parse-options.h"
11
12 static void hash_fd(int fd, const char *type, int write_object, const char *path)
13 {
@@ -49,87 +50,70 @@ static void hash_stdin_paths(const char *type, int write_objects)
50 strbuf_release(&nbuf);
51 }
52
52 -static const char hash_object_usage[] =
53 -"git hash-object [-t <type>] [-w] [--stdin] [--] <file>...\n"
54 -" or: git hash-object --stdin-paths < <list-of-paths>";
53 +static const char * const hash_object_usage[] = {
54 + "git hash-object [-t <type>] [-w] [--stdin] [--] <file>...",
55 + "git hash-object --stdin-paths < <list-of-paths>",
56 + NULL
57 +};
58
56 -int main(int argc, char **argv)
59 +static const char *type;
60 +static int write_object;
61 +static int hashstdin;
62 +static int stdin_paths;
63 +
64 +static const struct option hash_object_options[] = {
65 + OPT_STRING('t', NULL, &type, "type", "object type"),
66 + OPT_BOOLEAN('w', NULL, &write_object, "write the object into the object database"),
67 + OPT_BOOLEAN( 0 , "stdin", &hashstdin, "read the object from stdin"),
68 + OPT_BOOLEAN( 0 , "stdin-paths", &stdin_paths, "read file names from stdin"),
69 + OPT_END()
70 +};
71 +
72 +int main(int argc, const char **argv)
73 {
74 int i;
59 - const char *type = blob_type;
60 - int write_object = 0;
75 const char *prefix = NULL;
76 int prefix_length = -1;
63 - int no_more_flags = 0;
64 - int hashstdin = 0;
65 - int stdin_paths = 0;
77 + const char *errstr = NULL;
78 +
79 + type = blob_type;
80
81 git_config(git_default_config, NULL);
82
69 - for (i = 1 ; i < argc; i++) {
70 - if (!no_more_flags && argv[i][0] == '-') {
71 - if (!strcmp(argv[i], "-t")) {
72 - if (argc <= ++i)
73 - usage(hash_object_usage);
74 - type = argv[i];
75 - }
76 - else if (!strcmp(argv[i], "-w")) {
77 - if (prefix_length < 0) {
78 - prefix = setup_git_directory();
79 - prefix_length =
80 - prefix ? strlen(prefix) : 0;
81 - }
82 - write_object = 1;
83 - }
84 - else if (!strcmp(argv[i], "--")) {
85 - no_more_flags = 1;
86 - }
87 - else if (!strcmp(argv[i], "--help"))
88 - usage(hash_object_usage);
89 - else if (!strcmp(argv[i], "--stdin-paths")) {
90 - if (hashstdin) {
91 - error("Can't use --stdin-paths with --stdin");
92 - usage(hash_object_usage);
93 - }
94 - stdin_paths = 1;
95 -
96 - }
97 - else if (!strcmp(argv[i], "--stdin")) {
98 - if (stdin_paths) {
99 - error("Can't use %s with --stdin-paths", argv[i]);
100 - usage(hash_object_usage);
101 - }
102 - if (hashstdin)
103 - die("Multiple --stdin arguments are not supported");
104 - hashstdin = 1;
105 - }
106 - else
107 - usage(hash_object_usage);
108 - }
109 - else {
110 - const char *arg = argv[i];
111 -
112 - if (stdin_paths) {
113 - error("Can't specify files (such as \"%s\") with --stdin-paths", arg);
114 - usage(hash_object_usage);
115 - }
116 -
117 - if (hashstdin) {
118 - hash_fd(0, type, write_object, NULL);
119 - hashstdin = 0;
120 - }
121 - if (0 <= prefix_length)
122 - arg = prefix_filename(prefix, prefix_length,
123 - arg);
124 - hash_object(arg, type, write_object);
125 - no_more_flags = 1;
126 - }
83 + argc = parse_options(argc, argv, hash_object_options, hash_object_usage, 0);
84 +
85 + if (write_object) {
86 + prefix = setup_git_directory();
87 + prefix_length = prefix ? strlen(prefix) : 0;
88 }
89
129 - if (stdin_paths)
130 - hash_stdin_paths(type, write_object);
90 + if (stdin_paths) {
91 + if (hashstdin)
92 + errstr = "Can't use --stdin-paths with --stdin";
93 + else if (argc)
94 + errstr = "Can't specify files with --stdin-paths";
95 + }
96 + else if (hashstdin > 1)
97 + errstr = "Multiple --stdin arguments are not supported";
98 +
99 + if (errstr) {
100 + error (errstr);
101 + usage_with_options(hash_object_usage, hash_object_options);
102 + }
103
104 if (hashstdin)
105 hash_fd(0, type, write_object, NULL);
106 +
107 + for (i = 0 ; i < argc; i++) {
108 + const char *arg = argv[i];
109 +
110 + if (0 <= prefix_length)
111 + arg = prefix_filename(prefix, prefix_length, arg);
112 + hash_object(arg, type, write_object);
113 + }
114 +
115 + if (stdin_paths)
116 + hash_stdin_paths(type, write_object);
117 +
118 return 0;
119 }