cat-file.con commit binary diff: further updates. (0660626)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include "cache.h"
   7#include "exec_cmd.h"
   8#include "tag.h"
   9#include "tree.h"
  10
  11static void flush_buffer(const char *buf, unsigned long size)
  12{
  13        while (size > 0) {
  14                long ret = xwrite(1, buf, size);
  15                if (ret < 0) {
  16                        /* Ignore epipe */
  17                        if (errno == EPIPE)
  18                                break;
  19                        die("git-cat-file: %s", strerror(errno));
  20                } else if (!ret) {
  21                        die("git-cat-file: disk full?");
  22                }
  23                size -= ret;
  24                buf += ret;
  25        }
  26}
  27
  28static int pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size)
  29{
  30        /* the parser in tag.c is useless here. */
  31        const char *endp = buf + size;
  32        const char *cp = buf;
  33
  34        while (cp < endp) {
  35                char c = *cp++;
  36                if (c != '\n')
  37                        continue;
  38                if (7 <= endp - cp && !memcmp("tagger ", cp, 7)) {
  39                        const char *tagger = cp;
  40
  41                        /* Found the tagger line.  Copy out the contents
  42                         * of the buffer so far.
  43                         */
  44                        flush_buffer(buf, cp - buf);
  45
  46                        /*
  47                         * Do something intelligent, like pretty-printing
  48                         * the date.
  49                         */
  50                        while (cp < endp) {
  51                                if (*cp++ == '\n') {
  52                                        /* tagger to cp is a line
  53                                         * that has ident and time.
  54                                         */
  55                                        const char *sp = tagger;
  56                                        char *ep;
  57                                        unsigned long date;
  58                                        long tz;
  59                                        while (sp < cp && *sp != '>')
  60                                                sp++;
  61                                        if (sp == cp) {
  62                                                /* give up */
  63                                                flush_buffer(tagger,
  64                                                             cp - tagger);
  65                                                break;
  66                                        }
  67                                        while (sp < cp &&
  68                                               !('0' <= *sp && *sp <= '9'))
  69                                                sp++;
  70                                        flush_buffer(tagger, sp - tagger);
  71                                        date = strtoul(sp, &ep, 10);
  72                                        tz = strtol(ep, NULL, 10);
  73                                        sp = show_date(date, tz);
  74                                        flush_buffer(sp, strlen(sp));
  75                                        xwrite(1, "\n", 1);
  76                                        break;
  77                                }
  78                        }
  79                        break;
  80                }
  81                if (cp < endp && *cp == '\n')
  82                        /* end of header */
  83                        break;
  84        }
  85        /* At this point, we have copied out the header up to the end of
  86         * the tagger line and cp points at one past \n.  It could be the
  87         * next header line after the tagger line, or it could be another
  88         * \n that marks the end of the headers.  We need to copy out the
  89         * remainder as is.
  90         */
  91        if (cp < endp)
  92                flush_buffer(cp, endp - cp);
  93        return 0;
  94}
  95
  96int main(int argc, char **argv)
  97{
  98        unsigned char sha1[20];
  99        char type[20];
 100        void *buf;
 101        unsigned long size;
 102        int opt;
 103
 104        setup_git_directory();
 105        git_config(git_default_config);
 106        if (argc != 3 || get_sha1(argv[2], sha1))
 107                usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");
 108
 109        opt = 0;
 110        if ( argv[1][0] == '-' ) {
 111                opt = argv[1][1];
 112                if ( !opt || argv[1][2] )
 113                        opt = -1; /* Not a single character option */
 114        }
 115
 116        buf = NULL;
 117        switch (opt) {
 118        case 't':
 119                if (!sha1_object_info(sha1, type, NULL)) {
 120                        printf("%s\n", type);
 121                        return 0;
 122                }
 123                break;
 124
 125        case 's':
 126                if (!sha1_object_info(sha1, type, &size)) {
 127                        printf("%lu\n", size);
 128                        return 0;
 129                }
 130                break;
 131
 132        case 'e':
 133                return !has_sha1_file(sha1);
 134
 135        case 'p':
 136                if (get_sha1(argv[2], sha1) ||
 137                    sha1_object_info(sha1, type, NULL))
 138                        die("Not a valid object name %s", argv[2]);
 139
 140                /* custom pretty-print here */
 141                if (!strcmp(type, tree_type))
 142                        return execl_git_cmd("ls-tree", argv[2], NULL);
 143
 144                buf = read_sha1_file(sha1, type, &size);
 145                if (!buf)
 146                        die("Cannot read object %s", argv[2]);
 147                if (!strcmp(type, tag_type))
 148                        return pprint_tag(sha1, buf, size);
 149
 150                /* otherwise just spit out the data */
 151                break;
 152        case 0:
 153                buf = read_object_with_reference(sha1, argv[1], &size, NULL);
 154                break;
 155
 156        default:
 157                die("git-cat-file: unknown option: %s\n", argv[1]);
 158        }
 159
 160        if (!buf)
 161                die("git-cat-file %s: bad file", argv[2]);
 162
 163        flush_buffer(buf, size);
 164        return 0;
 165}