Make git-send-pack exit with error when some refs couldn't be pushed out
[git/debian.git] / cat-file.c
blob7594108c6e5f7d8830c7b5ca6f45951b8b4bcc23
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;
14 int opt;
16 setup_git_directory();
17 if (argc != 3 || get_sha1(argv[2], sha1))
18 usage("git-cat-file [-t|-s|-e|<type>] <sha1>");
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 */
27 buf = NULL;
28 switch (opt) {
29 case 't':
30 if (!sha1_object_info(sha1, type, NULL)) {
31 printf("%s\n", type);
32 return 0;
34 break;
36 case 's':
37 if (!sha1_object_info(sha1, type, &size)) {
38 printf("%lu\n", size);
39 return 0;
41 break;
43 case 'e':
44 return !has_sha1_file(sha1);
46 case 0:
47 buf = read_object_with_reference(sha1, argv[1], &size, NULL);
48 break;
50 default:
51 die("git-cat-file: unknown option: %s\n", argv[1]);
54 if (!buf)
55 die("git-cat-file %s: bad file", argv[2]);
57 while (size > 0) {
58 long ret = write(1, buf, size);
59 if (ret < 0) {
60 if (errno == EAGAIN)
61 continue;
62 /* Ignore epipe */
63 if (errno == EPIPE)
64 break;
65 die("git-cat-file: %s", strerror(errno));
66 } else if (!ret) {
67 die("git-cat-file: disk full?");
69 size -= ret;
70 buf += ret;
72 return 0;