3071672c6ce2ffb7c1c7d43c1a728f8e020e296b
[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         int fd;
15
16         if (argc != 3 || get_sha1_hex(argv[2], sha1))
17                 usage("cat-file: cat-file [-t | tagname] <sha1>");
18         buf = read_sha1_file(sha1, type, &size);
19         if (!buf) {
20                 fprintf(stderr, "cat-file %s: bad file\n", argv[2]);
21                 exit(1);
22         }
23         if (!strcmp("-t", argv[1])) {
24                 buf = type;
25                 size = strlen(type);
26                 type[size] = '\n';
27                 size++;
28         } else if (strcmp(type, argv[1])) {
29                 fprintf(stderr, "cat-file %s: bad tag\n", argv[2]);
30                 exit(1);        /* bad tag */
31         }
32
33         while (size > 0) {
34                 long ret = write(1, buf, size);
35                 if (ret < 0) {
36                         if (errno == EAGAIN)
37                                 continue;
38                         /* Ignore epipe */
39                         if (errno == EPIPE)
40                                 break;
41                         fprintf(stderr, "cat-file: %s\n", strerror(errno));
42                         exit(1);
43                 }
44                 if (!ret) {
45                         fprintf(stderr, "cat-file: disk full?");
46                         exit(1);
47                 }
48                 size -= ret;
49                 buf += ret;
50         }
51         return 0;
52 }