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