ls-tree.con commit [PATCH] Consolidate the error handling (2de381f)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include "cache.h"
   7
   8static int list(unsigned char *sha1)
   9{
  10        void *buffer;
  11        unsigned long size;
  12        char type[20];
  13
  14        buffer = read_sha1_file(sha1, type, &size);
  15        if (!buffer)
  16                die("unable to read sha1 file");
  17        if (strcmp(type, "tree"))
  18                die("expected a 'tree' node");
  19        while (size) {
  20                int len = strlen(buffer)+1;
  21                unsigned char *sha1 = buffer + len;
  22                char *path = strchr(buffer, ' ')+1;
  23                unsigned int mode;
  24                unsigned char *type;
  25
  26                if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
  27                        die("corrupt 'tree' file");
  28                buffer = sha1 + 20;
  29                size -= len + 20;
  30                /* XXX: We do some ugly mode heuristics here.
  31                 * It seems not worth it to read each file just to get this
  32                 * and the file size. -- pasky@ucw.cz */
  33                type = S_ISDIR(mode) ? "tree" : "blob";
  34                printf("%03o\t%s\t%s\t%s\n", mode, type, sha1_to_hex(sha1), path);
  35        }
  36        return 0;
  37}
  38
  39int main(int argc, char **argv)
  40{
  41        unsigned char sha1[20];
  42
  43        if (argc != 2)
  44                usage("ls-tree <key>");
  45        if (get_sha1_hex(argv[1], sha1) < 0)
  46                usage("ls-tree <key>");
  47        sha1_file_directory = getenv(DB_ENVIRONMENT);
  48        if (!sha1_file_directory)
  49                sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
  50        if (list(sha1) < 0)
  51                die("list failed");
  52        return 0;
  53}