fsck-cache: fix SIGSEGV on bad tag object
[git/git.git] / cat-file.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c5163
LT
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;
e83c5163 14
3c249c95 15 if (argc != 3 || get_sha1(argv[2], sha1))
bab5583a 16 usage("git-cat-file [-t | tagname] <sha1>");
11e7d5c5 17
bf0c6e83 18 if (!strcmp("-t", argv[1])) {
11e7d5c5
LT
19 buf = read_sha1_file(sha1, type, &size);
20 if (buf) {
21 buf = type;
22 size = strlen(type);
23 type[size] = '\n';
fa9e9c7b 24 size++;
11e7d5c5
LT
25 }
26 } else {
27 buf = read_object_with_reference(sha1, argv[1], &size, NULL);
bf0c6e83
LT
28 }
29
11e7d5c5 30 if (!buf)
bab5583a 31 die("git-cat-file %s: bad file", argv[2]);
11e7d5c5 32
bf0c6e83
LT
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;
bab5583a 41 die("git-cat-file: %s", strerror(errno));
2de381f9 42 } else if (!ret) {
bab5583a 43 die("git-cat-file: disk full?");
bf0c6e83
LT
44 }
45 size -= ret;
46 buf += ret;
47 }
48 return 0;
e83c5163 49}