Make "cat-file" use "read_object_with_reference()"
[git/spearce.git] / cat-file.c
blob6bbb0ca0174265626a228b143fe348f4eb186f0c
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
8 int main(int argc, char **argv)
10 unsigned char sha1[20];
11 char type[20];
12 void *buf;
13 unsigned long size;
15 if (argc != 3 || get_sha1(argv[2], sha1))
16 usage("cat-file [-t | tagname] <sha1>");
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';
25 } else {
26 buf = read_object_with_reference(sha1, argv[1], &size, NULL);
29 if (!buf)
30 die("cat-file %s: bad file", argv[2]);
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?");
44 size -= ret;
45 buf += ret;
47 return 0;