@matheusbsbs / matheus-bagliano / commits / 61fdbcf98b
Fork 0

ls-tree: migrate to parse-options

Signed-off-by: Stephen Boyd <bebarino@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stephen Boyd committed Nov 13, 2009 at 20:34 UTC 61fdbcf98b4564ee00d5005bb99f871863d457e7
1 file changed +40 -60
builtin-ls-tree.c
+40 -60
@@ -9,6 +9,7 @@
9 #include "commit.h"
10 #include "quote.h"
11 #include "builtin.h"
12 +#include "parse-options.h"
13
14 static int line_termination = '\n';
15 #define LS_RECURSIVE 1
@@ -22,8 +23,10 @@ static const char **pathspec;
23 static int chomp_prefix;
24 static const char *ls_tree_prefix;
25
25 -static const char ls_tree_usage[] =
26 - "git ls-tree [-d] [-r] [-t] [-l] [-z] [--name-only] [--name-status] [--full-name] [--full-tree] [--abbrev[=<n>]] <tree-ish> [path...]";
26 +static const char * const ls_tree_usage[] = {
27 + "git ls-tree [<options>] <tree-ish> [path...]",
28 + NULL
29 +};
30
31 static int show_recursive(const char *base, int baselen, const char *pathname)
32 {
@@ -117,76 +120,53 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
120 {
121 unsigned char sha1[20];
122 struct tree *tree;
123 + int full_tree = 0;
124 + const struct option ls_tree_options[] = {
125 + OPT_BIT('d', NULL, &ls_options, "only show trees",
126 + LS_TREE_ONLY),
127 + OPT_BIT('r', NULL, &ls_options, "recurse into subtrees",
128 + LS_RECURSIVE),
129 + OPT_BIT('t', NULL, &ls_options, "show trees when recursing",
130 + LS_SHOW_TREES),
131 + OPT_SET_INT('z', NULL, &line_termination,
132 + "terminate entries with NUL byte", 0),
133 + OPT_BIT('l', "long", &ls_options, "include object size",
134 + LS_SHOW_SIZE),
135 + OPT_BIT(0, "name-only", &ls_options, "list only filenames",
136 + LS_NAME_ONLY),
137 + OPT_BIT(0, "name-status", &ls_options, "list only filenames",
138 + LS_NAME_ONLY),
139 + OPT_SET_INT(0, "full-name", &chomp_prefix,
140 + "use full path names", 0),
141 + OPT_BOOLEAN(0, "full-tree", &full_tree,
142 + "list entire tree; not just current directory "
143 + "(implies --full-name)"),
144 + OPT__ABBREV(&abbrev),
145 + OPT_END()
146 + };
147
148 git_config(git_default_config, NULL);
149 ls_tree_prefix = prefix;
150 if (prefix && *prefix)
151 chomp_prefix = strlen(prefix);
125 - while (1 < argc && argv[1][0] == '-') {
126 - switch (argv[1][1]) {
127 - case 'z':
128 - line_termination = 0;
129 - break;
130 - case 'r':
131 - ls_options |= LS_RECURSIVE;
132 - break;
133 - case 'd':
134 - ls_options |= LS_TREE_ONLY;
135 - break;
136 - case 't':
137 - ls_options |= LS_SHOW_TREES;
138 - break;
139 - case 'l':
140 - ls_options |= LS_SHOW_SIZE;
141 - break;
142 - case '-':
143 - if (!strcmp(argv[1]+2, "name-only") ||
144 - !strcmp(argv[1]+2, "name-status")) {
145 - ls_options |= LS_NAME_ONLY;
146 - break;
147 - }
148 - if (!strcmp(argv[1]+2, "long")) {
149 - ls_options |= LS_SHOW_SIZE;
150 - break;
151 - }
152 - if (!strcmp(argv[1]+2, "full-name")) {
153 - chomp_prefix = 0;
154 - break;
155 - }
156 - if (!strcmp(argv[1]+2, "full-tree")) {
157 - ls_tree_prefix = prefix = NULL;
158 - chomp_prefix = 0;
159 - break;
160 - }
161 - if (!prefixcmp(argv[1]+2, "abbrev=")) {
162 - abbrev = strtoul(argv[1]+9, NULL, 10);
163 - if (abbrev && abbrev < MINIMUM_ABBREV)
164 - abbrev = MINIMUM_ABBREV;
165 - else if (abbrev > 40)
166 - abbrev = 40;
167 - break;
168 - }
169 - if (!strcmp(argv[1]+2, "abbrev")) {
170 - abbrev = DEFAULT_ABBREV;
171 - break;
172 - }
173 - /* otherwise fallthru */
174 - default:
175 - usage(ls_tree_usage);
176 - }
177 - argc--; argv++;
152 +
153 + argc = parse_options(argc, argv, prefix, ls_tree_options,
154 + ls_tree_usage, 0);
155 + if (full_tree) {
156 + ls_tree_prefix = prefix = NULL;
157 + chomp_prefix = 0;
158 }
159 /* -d -r should imply -t, but -d by itself should not have to. */
160 if ( (LS_TREE_ONLY|LS_RECURSIVE) ==
161 ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options))
162 ls_options |= LS_SHOW_TREES;
163
184 - if (argc < 2)
185 - usage(ls_tree_usage);
186 - if (get_sha1(argv[1], sha1))
187 - die("Not a valid object name %s", argv[1]);
164 + if (argc < 1)
165 + usage_with_options(ls_tree_usage, ls_tree_options);
166 + if (get_sha1(argv[0], sha1))
167 + die("Not a valid object name %s", argv[0]);
168
189 - pathspec = get_pathspec(prefix, argv + 2);
169 + pathspec = get_pathspec(prefix, argv + 1);
170 tree = parse_tree_indirect(sha1);
171 if (!tree)
172 die("not a tree object");