9
9
#include "commit.h"
10
10
#include "quote.h"
11
11
#include "builtin.h"
12
+ #include "parse-options.h"
12
13
13
14
static int line_termination = '\n' ;
14
15
#define LS_RECURSIVE 1
@@ -22,8 +23,10 @@ static const char **pathspec;
22
23
static int chomp_prefix ;
23
24
static const char * ls_tree_prefix ;
24
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
+ };
27
30
28
31
static int show_recursive (const char * base , int baselen , const char * pathname )
29
32
{
@@ -117,76 +120,53 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
117
120
{
118
121
unsigned char sha1 [20 ];
119
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
+ };
120
147
121
148
git_config (git_default_config , NULL );
122
149
ls_tree_prefix = prefix ;
123
150
if (prefix && * prefix )
124
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 ;
178
158
}
179
159
/* -d -r should imply -t, but -d by itself should not have to. */
180
160
if ( (LS_TREE_ONLY |LS_RECURSIVE ) ==
181
161
((LS_TREE_ONLY |LS_RECURSIVE ) & ls_options ))
182
162
ls_options |= LS_SHOW_TREES ;
183
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 ]);
188
168
189
- pathspec = get_pathspec (prefix , argv + 2 );
169
+ pathspec = get_pathspec (prefix , argv + 1 );
190
170
tree = parse_tree_indirect (sha1 );
191
171
if (!tree )
192
172
die ("not a tree object" );
0 commit comments