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 static int ls_options
= 0;
15 const char **pathspec
;
17 static const char ls_tree_usage
[] =
18 "git-ls-tree [-d] [-r] [-z] <tree-ish> [path...]";
20 static int show_tree(unsigned char *sha1
, const char *base
, int baselen
, const char *pathname
, unsigned mode
, int stage
)
22 const char *type
= "blob";
26 if (ls_options
& LS_RECURSIVE
)
27 return READ_TREE_RECURSIVE
;
31 const char *spec
= *s
++;
36 if (strncmp(base
, spec
, baselen
))
38 len
= strlen(pathname
);
40 speclen
= strlen(spec
);
43 if (memcmp(pathname
, spec
, len
))
45 return READ_TREE_RECURSIVE
;
51 printf("%06o %s %s\t%.*s%s%c", mode
, type
, sha1_to_hex(sha1
), baselen
, base
, pathname
, line_termination
);
55 int main(int argc
, const char **argv
)
58 unsigned char sha1
[20];
62 prefix
= setup_git_directory();
63 while (1 < argc
&& argv
[1][0] == '-') {
69 ls_options
|= LS_RECURSIVE
;
72 ls_options
|= LS_TREE_ONLY
;
82 if (get_sha1(argv
[1], sha1
) < 0)
85 pathspec
= get_pathspec(prefix
, argv
+ 2);
86 buf
= read_object_with_reference(sha1
, "tree", &size
, NULL
);
88 die("not a tree object");
89 read_tree_recursive(buf
, size
, "", 0, 0, pathspec
, show_tree
);