ls-tree.con commit ls-tree: further cleanup to parallel ls-files. (b45c569)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include "cache.h"
   7#include "blob.h"
   8#include "tree.h"
   9#include "quote.h"
  10
  11static int line_termination = '\n';
  12#define LS_RECURSIVE 1
  13#define LS_TREE_ONLY 2
  14static int ls_options = LS_RECURSIVE;
  15
  16static const char ls_tree_usage[] =
  17        "git-ls-tree [-d] [-r] [-z] <tree-ish> [path...]";
  18
  19static int show_tree(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
  20{
  21        const char *type = "blob";
  22
  23        if (S_ISDIR(mode)) {
  24                if (ls_options & LS_RECURSIVE)
  25                        return READ_TREE_RECURSIVE;
  26                type = "tree";
  27        }
  28
  29        printf("%06o %s %s\t%.*s%s%c", mode, type, sha1_to_hex(sha1), baselen, base, pathname, line_termination);
  30        return 0;
  31}
  32
  33int main(int argc, const char **argv)
  34{
  35        const char **path, *prefix;
  36        unsigned char sha1[20];
  37        char *buf;
  38        unsigned long size;
  39
  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);
  54                }
  55                argc--; argv++;
  56        }
  57
  58        if (argc < 2)
  59                usage(ls_tree_usage);
  60        if (get_sha1(argv[1], sha1) < 0)
  61                usage(ls_tree_usage);
  62
  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
  69        return 0;
  70}