GIT 1.1.0
[git/mingw/4msysgit/wingit-dll.git] / cat-file.c
blob96d66b43043ee1e2381bfb80bb2bc1c7063ecc38
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 = 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?");
67 size -= ret;
68 buf += ret;
70 return 0;