cat-file.con commit Merge http://www.kernel.org/pub/scm/gitk/gitk (5e80092)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include "cache.h"
   7
   8int main(int argc, char **argv)
   9{
  10        unsigned char sha1[20];
  11        char type[20];
  12        void *buf;
  13        unsigned long size;
  14        int opt;
  15
  16        setup_git_directory();
  17        if (argc != 3 || get_sha1(argv[2], sha1))
  18                usage("git-cat-file [-t|-s|-e|<type>] <sha1>");
  19
  20        opt = 0;
  21        if ( argv[1][0] == '-' ) {
  22                opt = argv[1][1];
  23                if ( !opt || argv[1][2] )
  24                        opt = -1; /* Not a single character option */
  25        }
  26
  27        buf = NULL;
  28        switch (opt) {
  29        case 't':
  30                if (!sha1_object_info(sha1, type, NULL)) {
  31                        printf("%s\n", type);
  32                        return 0;
  33                }
  34                break;
  35
  36        case 's':
  37                if (!sha1_object_info(sha1, type, &size)) {
  38                        printf("%lu\n", size);
  39                        return 0;
  40                }
  41                break;
  42
  43        case 'e':
  44                return !has_sha1_file(sha1);
  45
  46        case 0:
  47                buf = read_object_with_reference(sha1, argv[1], &size, NULL);
  48                break;
  49
  50        default:
  51                die("git-cat-file: unknown option: %s\n", argv[1]);
  52        }
  53
  54        if (!buf)
  55                die("git-cat-file %s: bad file", argv[2]);
  56
  57        while (size > 0) {
  58                long ret = write(1, buf, size);
  59                if (ret < 0) {
  60                        if (errno == EAGAIN)
  61                                continue;
  62                        /* Ignore epipe */
  63                        if (errno == EPIPE)
  64                                break;
  65                        die("git-cat-file: %s", strerror(errno));
  66                } else if (!ret) {
  67                        die("git-cat-file: disk full?");
  68                }
  69                size -= ret;
  70                buf += ret;
  71        }
  72        return 0;
  73}