cat-file: make it work in a subdirectory.
[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
5da22bc1 15 setup_git_directory();
3c249c95 16 if (argc != 3 || get_sha1(argv[2], sha1))
f16ebbdd 17 usage("git-cat-file [-t | -s | <type>] <sha1>");
11e7d5c5 18
62bb9960 19 if (!strcmp("-t", argv[1]) || !strcmp("-s", argv[1])) {
c62266f3
JH
20 if (!sha1_object_info(sha1, type,
21 argv[1][1] == 's' ? &size : NULL)) {
62bb9960
JH
22 switch (argv[1][1]) {
23 case 't':
24 printf("%s\n", type);
25 break;
26 case 's':
27 printf("%lu\n", size);
28 break;
29 }
f2a06330 30 return 0;
11e7d5c5 31 }
f2a06330 32 buf = NULL;
11e7d5c5
LT
33 } else {
34 buf = read_object_with_reference(sha1, argv[1], &size, NULL);
bf0c6e83
LT
35 }
36
11e7d5c5 37 if (!buf)
bab5583a 38 die("git-cat-file %s: bad file", argv[2]);
11e7d5c5 39
bf0c6e83
LT
40 while (size > 0) {
41 long ret = write(1, buf, size);
42 if (ret < 0) {
43 if (errno == EAGAIN)
44 continue;
45 /* Ignore epipe */
46 if (errno == EPIPE)
47 break;
bab5583a 48 die("git-cat-file: %s", strerror(errno));
2de381f9 49 } else if (!ret) {
bab5583a 50 die("git-cat-file: disk full?");
bf0c6e83
LT
51 }
52 size -= ret;
53 buf += ret;
54 }
55 return 0;
e83c5163 56}