]>
Commit | Line | Data |
---|---|---|
8bc9a0c7 LT |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
e83c5163 | 6 | #include "cache.h" |
a0f15fa5 | 7 | #include "exec_cmd.h" |
8e440259 PE |
8 | #include "tag.h" |
9 | #include "tree.h" | |
f81daefe | 10 | #include "builtin.h" |
a0f15fa5 JH |
11 | |
12 | static void flush_buffer(const char *buf, unsigned long size) | |
13 | { | |
14 | while (size > 0) { | |
15 | long ret = xwrite(1, buf, size); | |
16 | if (ret < 0) { | |
17 | /* Ignore epipe */ | |
18 | if (errno == EPIPE) | |
19 | break; | |
20 | die("git-cat-file: %s", strerror(errno)); | |
21 | } else if (!ret) { | |
22 | die("git-cat-file: disk full?"); | |
23 | } | |
24 | size -= ret; | |
25 | buf += ret; | |
26 | } | |
27 | } | |
28 | ||
eddd1c8c | 29 | static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size) |
a0f15fa5 JH |
30 | { |
31 | /* the parser in tag.c is useless here. */ | |
32 | const char *endp = buf + size; | |
33 | const char *cp = buf; | |
34 | ||
35 | while (cp < endp) { | |
36 | char c = *cp++; | |
37 | if (c != '\n') | |
38 | continue; | |
39 | if (7 <= endp - cp && !memcmp("tagger ", cp, 7)) { | |
40 | const char *tagger = cp; | |
41 | ||
42 | /* Found the tagger line. Copy out the contents | |
43 | * of the buffer so far. | |
44 | */ | |
45 | flush_buffer(buf, cp - buf); | |
46 | ||
47 | /* | |
48 | * Do something intelligent, like pretty-printing | |
49 | * the date. | |
50 | */ | |
51 | while (cp < endp) { | |
52 | if (*cp++ == '\n') { | |
53 | /* tagger to cp is a line | |
54 | * that has ident and time. | |
55 | */ | |
56 | const char *sp = tagger; | |
57 | char *ep; | |
58 | unsigned long date; | |
59 | long tz; | |
60 | while (sp < cp && *sp != '>') | |
61 | sp++; | |
62 | if (sp == cp) { | |
63 | /* give up */ | |
64 | flush_buffer(tagger, | |
65 | cp - tagger); | |
66 | break; | |
67 | } | |
68 | while (sp < cp && | |
69 | !('0' <= *sp && *sp <= '9')) | |
70 | sp++; | |
71 | flush_buffer(tagger, sp - tagger); | |
72 | date = strtoul(sp, &ep, 10); | |
73 | tz = strtol(ep, NULL, 10); | |
74 | sp = show_date(date, tz); | |
75 | flush_buffer(sp, strlen(sp)); | |
76 | xwrite(1, "\n", 1); | |
77 | break; | |
78 | } | |
79 | } | |
80 | break; | |
81 | } | |
82 | if (cp < endp && *cp == '\n') | |
83 | /* end of header */ | |
84 | break; | |
85 | } | |
86 | /* At this point, we have copied out the header up to the end of | |
87 | * the tagger line and cp points at one past \n. It could be the | |
88 | * next header line after the tagger line, or it could be another | |
89 | * \n that marks the end of the headers. We need to copy out the | |
90 | * remainder as is. | |
91 | */ | |
92 | if (cp < endp) | |
93 | flush_buffer(cp, endp - cp); | |
a0f15fa5 | 94 | } |
e83c5163 | 95 | |
a633fca0 | 96 | int cmd_cat_file(int argc, const char **argv, const char *prefix) |
e83c5163 LT |
97 | { |
98 | unsigned char sha1[20]; | |
99 | char type[20]; | |
100 | void *buf; | |
101 | unsigned long size; | |
7950571a | 102 | int opt; |
e83c5163 | 103 | |
84a9b58c | 104 | git_config(git_default_config); |
31fff305 | 105 | if (argc != 3) |
a0f15fa5 | 106 | usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>"); |
31fff305 DL |
107 | if (get_sha1(argv[2], sha1)) |
108 | die("Not a valid object name %s", argv[2]); | |
7950571a PA |
109 | |
110 | opt = 0; | |
111 | if ( argv[1][0] == '-' ) { | |
112 | opt = argv[1][1]; | |
113 | if ( !opt || argv[1][2] ) | |
114 | opt = -1; /* Not a single character option */ | |
115 | } | |
116 | ||
117 | buf = NULL; | |
118 | switch (opt) { | |
119 | case 't': | |
120 | if (!sha1_object_info(sha1, type, NULL)) { | |
121 | printf("%s\n", type); | |
f2a06330 | 122 | return 0; |
11e7d5c5 | 123 | } |
7950571a PA |
124 | break; |
125 | ||
126 | case 's': | |
127 | if (!sha1_object_info(sha1, type, &size)) { | |
128 | printf("%lu\n", size); | |
129 | return 0; | |
130 | } | |
131 | break; | |
132 | ||
133 | case 'e': | |
134 | return !has_sha1_file(sha1); | |
135 | ||
a0f15fa5 | 136 | case 'p': |
31fff305 | 137 | if (sha1_object_info(sha1, type, NULL)) |
a0f15fa5 JH |
138 | die("Not a valid object name %s", argv[2]); |
139 | ||
140 | /* custom pretty-print here */ | |
8e440259 | 141 | if (!strcmp(type, tree_type)) |
b931aa5a | 142 | return cmd_ls_tree(2, argv + 1, NULL); |
a0f15fa5 JH |
143 | |
144 | buf = read_sha1_file(sha1, type, &size); | |
145 | if (!buf) | |
146 | die("Cannot read object %s", argv[2]); | |
eddd1c8c DR |
147 | if (!strcmp(type, tag_type)) { |
148 | pprint_tag(sha1, buf, size); | |
149 | return 0; | |
150 | } | |
a0f15fa5 JH |
151 | |
152 | /* otherwise just spit out the data */ | |
153 | break; | |
7950571a | 154 | case 0: |
11e7d5c5 | 155 | buf = read_object_with_reference(sha1, argv[1], &size, NULL); |
7950571a PA |
156 | break; |
157 | ||
158 | default: | |
159 | die("git-cat-file: unknown option: %s\n", argv[1]); | |
bf0c6e83 LT |
160 | } |
161 | ||
11e7d5c5 | 162 | if (!buf) |
bab5583a | 163 | die("git-cat-file %s: bad file", argv[2]); |
11e7d5c5 | 164 | |
a0f15fa5 | 165 | flush_buffer(buf, size); |
bf0c6e83 | 166 | return 0; |
e83c5163 | 167 | } |