ls-tree: further cleanup to parallel ls-files.
[git/mingw.git] / ls-tree.c
blobcf0dbbc7e4070d81cd52027ef5c1cc983949395a
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "blob.h"
8 #include "tree.h"
9 #include "quote.h"
11 static int line_termination = '\n';
12 #define LS_RECURSIVE 1
13 #define LS_TREE_ONLY 2
14 static int ls_options = LS_RECURSIVE;
16 static const char ls_tree_usage[] =
17 "git-ls-tree [-d] [-r] [-z] <tree-ish> [path...]";
19 static int show_tree(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
21 const char *type = "blob";
23 if (S_ISDIR(mode)) {
24 if (ls_options & LS_RECURSIVE)
25 return READ_TREE_RECURSIVE;
26 type = "tree";
29 printf("%06o %s %s\t%.*s%s%c", mode, type, sha1_to_hex(sha1), baselen, base, pathname, line_termination);
30 return 0;
33 int main(int argc, const char **argv)
35 const char **path, *prefix;
36 unsigned char sha1[20];
37 char *buf;
38 unsigned long size;
40 prefix = setup_git_directory();
41 while (1 < argc && argv[1][0] == '-') {
42 switch (argv[1][1]) {
43 case 'z':
44 line_termination = 0;
45 break;
46 case 'r':
47 ls_options |= LS_RECURSIVE;
48 break;
49 case 'd':
50 ls_options |= LS_TREE_ONLY;
51 break;
52 default:
53 usage(ls_tree_usage);
55 argc--; argv++;
58 if (argc < 2)
59 usage(ls_tree_usage);
60 if (get_sha1(argv[1], sha1) < 0)
61 usage(ls_tree_usage);
63 path = get_pathspec(prefix, argv + 2);
64 buf = read_object_with_reference(sha1, "tree", &size, NULL);
65 if (!buf)
66 die("not a tree object");
67 read_tree_recursive(buf, size, "", 0, 0, path, show_tree);
69 return 0;