ls-tree.con commit Move couple of ifdefs after "include config.mk" (b34403a)
   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;
  15const char **pathspec;
  16
  17static const char ls_tree_usage[] =
  18        "git-ls-tree [-d] [-r] [-z] <tree-ish> [path...]";
  19
  20static int show_tree(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
  21{
  22        const char *type = "blob";
  23
  24        if (S_ISDIR(mode)) {
  25                const char **s;
  26                if (ls_options & LS_RECURSIVE)
  27                        return READ_TREE_RECURSIVE;
  28                s = pathspec;
  29                if (s) {
  30                        for (;;) {
  31                                const char *spec = *s++;
  32                                int len, speclen;
  33
  34                                if (!spec)
  35                                        break;
  36                                if (strncmp(base, spec, baselen))
  37                                        continue;
  38                                len = strlen(pathname);
  39                                spec += baselen;
  40                                speclen = strlen(spec);
  41                                if (speclen <= len)
  42                                        continue;
  43                                if (memcmp(pathname, spec, len))
  44                                        continue;
  45                                return READ_TREE_RECURSIVE;
  46                        }
  47                }
  48                type = "tree";
  49        }
  50
  51        printf("%06o %s %s\t", mode, type, sha1_to_hex(sha1));
  52        write_name_quoted(base, baselen, pathname, line_termination, stdout);
  53        putchar(line_termination);
  54        return 0;
  55}
  56
  57int main(int argc, const char **argv)
  58{
  59        const char *prefix;
  60        unsigned char sha1[20];
  61        char *buf;
  62        unsigned long size;
  63
  64        prefix = setup_git_directory();
  65        while (1 < argc && argv[1][0] == '-') {
  66                switch (argv[1][1]) {
  67                case 'z':
  68                        line_termination = 0;
  69                        break;
  70                case 'r':
  71                        ls_options |= LS_RECURSIVE;
  72                        break;
  73                case 'd':
  74                        ls_options |= LS_TREE_ONLY;
  75                        break;
  76                default:
  77                        usage(ls_tree_usage);
  78                }
  79                argc--; argv++;
  80        }
  81
  82        if (argc < 2)
  83                usage(ls_tree_usage);
  84        if (get_sha1(argv[1], sha1) < 0)
  85                usage(ls_tree_usage);
  86
  87        pathspec = get_pathspec(prefix, argv + 2);
  88        buf = read_object_with_reference(sha1, "tree", &size, NULL);
  89        if (!buf)
  90                die("not a tree object");
  91        read_tree_recursive(buf, size, "", 0, 0, pathspec, show_tree);
  92
  93        return 0;
  94}