🌐 AI搜索 & 代理 主页
blob: 96d66b43043ee1e2381bfb80bb2bc1c7063ecc38 [file] [log] [blame]
Linus Torvalds8bc9a0c2005-04-07 22:16:101/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
Linus Torvaldse83c5162005-04-07 22:13:136#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;
H. Peter Anvin79505712005-12-04 01:57:4814 int opt;
Linus Torvaldse83c5162005-04-07 22:13:1315
Junio C Hamano5da22bc2005-08-30 16:07:5016 setup_git_directory();
Linus Torvalds3c249c92005-05-01 23:36:5617 if (argc != 3 || get_sha1(argv[2], sha1))
H. Peter Anvin79505712005-12-04 01:57:4818 usage("git-cat-file [-t|-s|-e|<type>] <sha1>");
Linus Torvalds11e7d5c2005-05-02 02:28:1819
H. Peter Anvin79505712005-12-04 01:57:4820 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 }
26
27 buf = NULL;
28 switch (opt) {
29 case 't':
30 if (!sha1_object_info(sha1, type, NULL)) {
31 printf("%s\n", type);
Junio C Hamanof2a06332005-06-28 06:58:4532 return 0;
Linus Torvalds11e7d5c2005-05-02 02:28:1833 }
H. Peter Anvin79505712005-12-04 01:57:4834 break;
35
36 case 's':
37 if (!sha1_object_info(sha1, type, &size)) {
38 printf("%lu\n", size);
39 return 0;
40 }
41 break;
42
43 case 'e':
44 return !has_sha1_file(sha1);
45
46 case 0:
Linus Torvalds11e7d5c2005-05-02 02:28:1847 buf = read_object_with_reference(sha1, argv[1], &size, NULL);
H. Peter Anvin79505712005-12-04 01:57:4848 break;
49
50 default:
51 die("git-cat-file: unknown option: %s\n", argv[1]);
Linus Torvalds11e7d5c2005-05-02 02:28:1852 }
53
Petr Baudis2de381f2005-04-13 09:28:4854 if (!buf)
Alexey Nezhdanovbab55832005-05-02 04:23:0455 die("git-cat-file %s: bad file", argv[2]);
Linus Torvaldsbf0c6e82005-04-08 16:16:3856
57 while (size > 0) {
Junio C Hamano1c15afb2005-12-20 00:18:2858 long ret = xwrite(1, buf, size);
Linus Torvaldsbf0c6e82005-04-08 16:16:3859 if (ret < 0) {
Linus Torvaldsbf0c6e82005-04-08 16:16:3860 /* Ignore epipe */
61 if (errno == EPIPE)
62 break;
Alexey Nezhdanovbab55832005-05-02 04:23:0463 die("git-cat-file: %s", strerror(errno));
Petr Baudis2de381f2005-04-13 09:28:4864 } else if (!ret) {
Alexey Nezhdanovbab55832005-05-02 04:23:0465 die("git-cat-file: disk full?");
Linus Torvaldsbf0c6e82005-04-08 16:16:3866 }
67 size -= ret;
68 buf += ret;
69 }
70 return 0;
Linus Torvaldse83c5162005-04-07 22:13:1371}