2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
11 static int line_termination
= '\n';
12 #define LS_RECURSIVE 1
13 #define LS_TREE_ONLY 2
14 #define LS_SHOW_TREES 4
15 #define LS_NAME_ONLY 8
16 static int abbrev
= 0;
17 static int ls_options
= 0;
18 const char **pathspec
;
19 static int chomp_prefix
= 0;
20 static const char *prefix
;
22 static const char ls_tree_usage
[] =
23 "git-ls-tree [-d] [-r] [-t] [-z] [--name-only] [--name-status] [--full-name] [--abbrev[=<n>]] <tree-ish> [path...]";
25 static int show_recursive(const char *base
, int baselen
, const char *pathname
)
29 if (ls_options
& LS_RECURSIVE
)
37 const char *spec
= *s
++;
42 if (strncmp(base
, spec
, baselen
))
44 len
= strlen(pathname
);
46 speclen
= strlen(spec
);
49 if (memcmp(pathname
, spec
, len
))
55 static int show_tree(unsigned char *sha1
, const char *base
, int baselen
,
56 const char *pathname
, unsigned mode
, int stage
)
59 const char *type
= "blob";
62 if (show_recursive(base
, baselen
, pathname
)) {
63 retval
= READ_TREE_RECURSIVE
;
64 if (!(ls_options
& LS_SHOW_TREES
))
69 else if (ls_options
& LS_TREE_ONLY
)
73 (baselen
< chomp_prefix
|| memcmp(prefix
, base
, chomp_prefix
)))
76 if (!(ls_options
& LS_NAME_ONLY
))
77 printf("%06o %s %s\t", mode
, type
,
78 abbrev
? find_unique_abbrev(sha1
,abbrev
)
80 write_name_quoted(base
+ chomp_prefix
, baselen
- chomp_prefix
,
82 line_termination
, stdout
);
83 putchar(line_termination
);
87 int main(int argc
, const char **argv
)
89 unsigned char sha1
[20];
92 prefix
= setup_git_directory();
93 git_config(git_default_config
);
94 if (prefix
&& *prefix
)
95 chomp_prefix
= strlen(prefix
);
96 while (1 < argc
&& argv
[1][0] == '-') {
102 ls_options
|= LS_RECURSIVE
;
105 ls_options
|= LS_TREE_ONLY
;
108 ls_options
|= LS_SHOW_TREES
;
111 if (!strcmp(argv
[1]+2, "name-only") ||
112 !strcmp(argv
[1]+2, "name-status")) {
113 ls_options
|= LS_NAME_ONLY
;
116 if (!strcmp(argv
[1]+2, "full-name")) {
120 if (!strncmp(argv
[1]+2, "abbrev=",7)) {
121 abbrev
= strtoul(argv
[1]+9, NULL
, 10);
122 if (abbrev
&& abbrev
< MINIMUM_ABBREV
)
123 abbrev
= MINIMUM_ABBREV
;
124 else if (abbrev
> 40)
128 if (!strcmp(argv
[1]+2, "abbrev")) {
129 abbrev
= DEFAULT_ABBREV
;
132 /* otherwise fallthru */
134 usage(ls_tree_usage
);
138 /* -d -r should imply -t, but -d by itself should not have to. */
139 if ( (LS_TREE_ONLY
|LS_RECURSIVE
) ==
140 ((LS_TREE_ONLY
|LS_RECURSIVE
) & ls_options
))
141 ls_options
|= LS_SHOW_TREES
;
144 usage(ls_tree_usage
);
145 if (get_sha1(argv
[1], sha1
) < 0)
146 usage(ls_tree_usage
);
148 pathspec
= get_pathspec(prefix
, argv
+ 2);
149 tree
= parse_tree_indirect(sha1
);
151 die("not a tree object");
152 read_tree_recursive(tree
, "", 0, 0, pathspec
, show_tree
);