Merge branch 'lt/fix-apply' into maint
[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;
7950571a 14 int opt;
e83c5163 15
5da22bc1 16 setup_git_directory();
3c249c95 17 if (argc != 3 || get_sha1(argv[2], sha1))
7950571a
PA
18 usage("git-cat-file [-t|-s|-e|<type>] <sha1>");
19
20 opt = 0;
21 if ( argv[1][0] == '-' ) {
22 opt = argv[1][1];
23 if ( !opt || argv[1][2] )
24 opt = -1; /* Not a single character option */
25 }
26
27 buf = NULL;
28 switch (opt) {
29 case 't':
30 if (!sha1_object_info(sha1, type, NULL)) {
31 printf("%s\n", type);
f2a06330 32 return 0;
11e7d5c5 33 }
7950571a
PA
34 break;
35
36 case 's':
37 if (!sha1_object_info(sha1, type, &size)) {
38 printf("%lu\n", size);
39 return 0;
40 }
41 break;
42
43 case 'e':
44 return !has_sha1_file(sha1);
45
46 case 0:
11e7d5c5 47 buf = read_object_with_reference(sha1, argv[1], &size, NULL);
7950571a
PA
48 break;
49
50 default:
51 die("git-cat-file: unknown option: %s\n", argv[1]);
bf0c6e83
LT
52 }
53
11e7d5c5 54 if (!buf)
bab5583a 55 die("git-cat-file %s: bad file", argv[2]);
11e7d5c5 56
bf0c6e83 57 while (size > 0) {
1c15afb9 58 long ret = xwrite(1, buf, size);
bf0c6e83 59 if (ret < 0) {
bf0c6e83
LT
60 /* Ignore epipe */
61 if (errno == EPIPE)
62 break;
bab5583a 63 die("git-cat-file: %s", strerror(errno));
2de381f9 64 } else if (!ret) {
bab5583a 65 die("git-cat-file: disk full?");
bf0c6e83
LT
66 }
67 size -= ret;
68 buf += ret;
69 }
70 return 0;
e83c5163 71}