ls-tree.con commit ls-tree: major rewrite to do pathspec (3c5e846)
   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 = 0;
  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        int retval = 0;
  23
  24        if (S_ISDIR(mode)) {
  25                type = "tree";
  26                if (ls_options & LS_RECURSIVE)
  27                        retval = READ_TREE_RECURSIVE;
  28        }
  29
  30        printf("%06o %s %s\t%.*s%s%c", mode, type, sha1_to_hex(sha1), baselen, base, pathname, line_termination);
  31        return retval;
  32}
  33
  34int main(int argc, const char **argv)
  35{
  36        const char **path, *prefix;
  37        unsigned char sha1[20];
  38        char *buf;
  39        unsigned long size;
  40
  41        prefix = setup_git_directory();
  42        while (1 < argc && argv[1][0] == '-') {
  43                switch (argv[1][1]) {
  44                case 'z':
  45                        line_termination = 0;
  46                        break;
  47                case 'r':
  48                        ls_options |= LS_RECURSIVE;
  49                        break;
  50                case 'd':
  51                        ls_options |= LS_TREE_ONLY;
  52                        break;
  53                default:
  54                        usage(ls_tree_usage);
  55                }
  56                argc--; argv++;
  57        }
  58
  59        if (argc < 2)
  60                usage(ls_tree_usage);
  61        if (get_sha1(argv[1], sha1) < 0)
  62                usage(ls_tree_usage);
  63
  64        path = get_pathspec(prefix, argv + 2);
  65        buf = read_object_with_reference(sha1, "tree", &size, NULL);
  66        if (!buf)
  67                die("not a tree object");
  68        read_tree_recursive(buf, size, "", 0, 0, path, show_tree);
  69
  70        return 0;
  71}