[PATCH] git-cat-file: use sha1_object_info() on '-t'.
[git.git] / cat-file.c
1 /*
2  * GIT - The information manager from hell
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  */
6 #include "cache.h"
7
8 int main(int argc, char **argv)
9 {
10         unsigned char sha1[20];
11         char type[20];
12         void *buf;
13         unsigned long size;
14
15         if (argc != 3 || get_sha1(argv[2], sha1))
16                 usage("git-cat-file [-t | tagname] <sha1>");
17
18         if (!strcmp("-t", argv[1])) {
19                 if (!sha1_object_info(sha1, type, &size)) {
20                         printf("%s\n", type);
21                         return 0;
22                 }
23                 buf = NULL;
24         } else {
25                 buf = read_object_with_reference(sha1, argv[1], &size, NULL);
26         }
27
28         if (!buf)
29                 die("git-cat-file %s: bad file", argv[2]);
30
31         while (size > 0) {
32                 long ret = write(1, buf, size);
33                 if (ret < 0) {
34                         if (errno == EAGAIN)
35                                 continue;
36                         /* Ignore epipe */
37                         if (errno == EPIPE)
38                                 break;
39                         die("git-cat-file: %s", strerror(errno));
40                 } else if (!ret) {
41                         die("git-cat-file: disk full?");
42                 }
43                 size -= ret;
44                 buf += ret;
45         }
46         return 0;
47 }