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
15 if (argc != 3 || get_sha1_hex(argv[2], sha1))
16 usage("cat-file: cat-file [-t | tagname] <sha1>");
17 buf = read_sha1_file(sha1, type, &size);
18 if (!buf) {
19 fprintf(stderr, "cat-file %s: bad file\n", argv[2]);
20 exit(1);
21 }
22 if (!strcmp("-t", argv[1])) {
23 buf = type;
24 size = strlen(type);
25 type[size] = '\n';
26 size++;
27 } else if (strcmp(type, argv[1])) {
28 fprintf(stderr, "cat-file %s: bad tag\n", argv[2]);
29 exit(1); /* bad tag */
30 }
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 fprintf(stderr, "cat-file: %s\n", strerror(errno));
41 exit(1);
42 }
43 if (!ret) {
44 fprintf(stderr, "cat-file: disk full?");
45 exit(1);
46 }
47 size -= ret;
48 buf += ret;
49 }
50 return 0;
51}