Code

git-format-patch: Always add a blank line between headers and body.
[git.git] / cat-file.c
1 /*
2  * GIT - The information manager from hell
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  */
6 #include "cache.h"
8 int main(int argc, char **argv)
9 {
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 */
25         }
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;
33                 }
34                 break;
36         case 's':
37                 if (!sha1_object_info(sha1, type, &size)) {
38                         printf("%lu\n", size);
39                         return 0;
40                 }
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]);
52         }
54         if (!buf)
55                 die("git-cat-file %s: bad file", argv[2]);
57         while (size > 0) {
58                 long ret = xwrite(1, buf, size);
59                 if (ret < 0) {
60                         /* Ignore epipe */
61                         if (errno == EPIPE)
62                                 break;
63                         die("git-cat-file: %s", strerror(errno));
64                 } else if (!ret) {
65                         die("git-cat-file: disk full?");
66                 }
67                 size -= ret;
68                 buf += ret;
69         }
70         return 0;
71 }