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 ls_options
= 0;
17 const char **pathspec
;
18 static int chomp_prefix
= 0;
19 static const char *prefix
;
21 static const char ls_tree_usage
[] =
22 "git-ls-tree [-d] [-r] [-t] [-z] [--name-only] [--name-status] [--full-name] <tree-ish> [path...]";
24 static int show_recursive(const char *base
, int baselen
, const char *pathname
)
28 if (ls_options
& LS_RECURSIVE
)
36 const char *spec
= *s
++;
41 if (strncmp(base
, spec
, baselen
))
43 len
= strlen(pathname
);
45 speclen
= strlen(spec
);
48 if (memcmp(pathname
, spec
, len
))
54 static int show_tree(unsigned char *sha1
, const char *base
, int baselen
,
55 const char *pathname
, unsigned mode
, int stage
)
58 const char *type
= "blob";
61 if (show_recursive(base
, baselen
, pathname
)) {
62 retval
= READ_TREE_RECURSIVE
;
63 if (!(ls_options
& LS_SHOW_TREES
))
68 else if (ls_options
& LS_TREE_ONLY
)
72 (baselen
< chomp_prefix
|| memcmp(prefix
, base
, chomp_prefix
)))
75 if (!(ls_options
& LS_NAME_ONLY
))
76 printf("%06o %s %s\t", mode
, type
, sha1_to_hex(sha1
));
77 write_name_quoted(base
+ chomp_prefix
, baselen
- chomp_prefix
,
79 line_termination
, stdout
);
80 putchar(line_termination
);
84 int main(int argc
, const char **argv
)
86 unsigned char sha1
[20];
89 prefix
= setup_git_directory();
90 git_config(git_default_config
);
91 if (prefix
&& *prefix
)
92 chomp_prefix
= strlen(prefix
);
93 while (1 < argc
&& argv
[1][0] == '-') {
99 ls_options
|= LS_RECURSIVE
;
102 ls_options
|= LS_TREE_ONLY
;
105 ls_options
|= LS_SHOW_TREES
;
108 if (!strcmp(argv
[1]+2, "name-only") ||
109 !strcmp(argv
[1]+2, "name-status")) {
110 ls_options
|= LS_NAME_ONLY
;
113 if (!strcmp(argv
[1]+2, "full-name")) {
117 /* otherwise fallthru */
119 usage(ls_tree_usage
);
123 /* -d -r should imply -t, but -d by itself should not have to. */
124 if ( (LS_TREE_ONLY
|LS_RECURSIVE
) ==
125 ((LS_TREE_ONLY
|LS_RECURSIVE
) & ls_options
))
126 ls_options
|= LS_SHOW_TREES
;
129 usage(ls_tree_usage
);
130 if (get_sha1(argv
[1], sha1
) < 0)
131 usage(ls_tree_usage
);
133 pathspec
= get_pathspec(prefix
, argv
+ 2);
134 tree
= parse_tree_indirect(sha1
);
136 die("not a tree object");
137 read_tree_recursive(tree
, "", 0, 0, pathspec
, show_tree
);