ls-tree: further cleanup to parallel ls-files.
[git/git.git] / ls-tree.c
CommitLineData
7912c070
PB
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
6af1f019
JH
7#include "blob.h"
8#include "tree.h"
22ddf719 9#include "quote.h"
7912c070 10
e99d59ff 11static int line_termination = '\n';
6af1f019
JH
12#define LS_RECURSIVE 1
13#define LS_TREE_ONLY 2
b45c569b 14static int ls_options = LS_RECURSIVE;
aa1c48df 15
3c5e8468
LT
16static const char ls_tree_usage[] =
17 "git-ls-tree [-d] [-r] [-z] <tree-ish> [path...]";
aa1c48df 18
3c5e8468 19static int show_tree(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
6af1f019 20{
3c5e8468 21 const char *type = "blob";
ab1630a3 22
3c5e8468 23 if (S_ISDIR(mode)) {
3c5e8468 24 if (ls_options & LS_RECURSIVE)
b45c569b
LT
25 return READ_TREE_RECURSIVE;
26 type = "tree";
6af1f019 27 }
ab1630a3 28
3c5e8468 29 printf("%06o %s %s\t%.*s%s%c", mode, type, sha1_to_hex(sha1), baselen, base, pathname, line_termination);
b45c569b 30 return 0;
6af1f019 31}
0f2303f7 32
3c5e8468 33int main(int argc, const char **argv)
6af1f019 34{
3c5e8468 35 const char **path, *prefix;
7912c070 36 unsigned char sha1[20];
3c5e8468
LT
37 char *buf;
38 unsigned long size;
7912c070 39
3c5e8468 40 prefix = setup_git_directory();
aa1c48df
JH
41 while (1 < argc && argv[1][0] == '-') {
42 switch (argv[1][1]) {
43 case 'z':
44 line_termination = 0;
45 break;
46 case 'r':
6af1f019
JH
47 ls_options |= LS_RECURSIVE;
48 break;
49 case 'd':
50 ls_options |= LS_TREE_ONLY;
aa1c48df
JH
51 break;
52 default:
0f2303f7 53 usage(ls_tree_usage);
aa1c48df
JH
54 }
55 argc--; argv++;
56 }
57
6d3a5077 58 if (argc < 2)
0f2303f7 59 usage(ls_tree_usage);
3c249c95 60 if (get_sha1(argv[1], sha1) < 0)
0f2303f7 61 usage(ls_tree_usage);
6af1f019 62
3c5e8468
LT
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);
68
7912c070
PB
69 return 0;
70}