Make "update-cache" a bit friendlier to use (and harder to mis-use).
[git/gitweb.git] / cat-file.c
blob3829fb6097ad9840cca7654f0b8a718100d590ac
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;
15 if (argc != 3 || get_sha1_hex(argv[2], sha1))
16 usage("cat-file: cat-file [-t | tagname] <sha1>");
17 buf = read_sha1_file(sha1, type, &size);
18 if (!buf) {
19 fprintf(stderr, "cat-file %s: bad file\n", argv[2]);
20 exit(1);
22 if (!strcmp("-t", argv[1])) {
23 buf = type;
24 size = strlen(type);
25 type[size] = '\n';
26 size++;
27 } else if (strcmp(type, argv[1])) {
28 fprintf(stderr, "cat-file %s: bad tag\n", argv[2]);
29 exit(1); /* bad tag */
32 while (size > 0) {
33 long ret = write(1, buf, size);
34 if (ret < 0) {
35 if (errno == EAGAIN)
36 continue;
37 /* Ignore epipe */
38 if (errno == EPIPE)
39 break;
40 fprintf(stderr, "cat-file: %s\n", strerror(errno));
41 exit(1);
43 if (!ret) {
44 fprintf(stderr, "cat-file: disk full?");
45 exit(1);
47 size -= ret;
48 buf += ret;
50 return 0;