2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
11 static void flush_buffer(const char *buf
, unsigned long size
)
14 long ret
= xwrite(1, buf
, size
);
19 die("git-cat-file: %s", strerror(errno
));
21 die("git-cat-file: disk full?");
28 static int pprint_tag(const unsigned char *sha1
, const char *buf
, unsigned long size
)
30 /* the parser in tag.c is useless here. */
31 const char *endp
= buf
+ size
;
38 if (7 <= endp
- cp
&& !memcmp("tagger ", cp
, 7)) {
39 const char *tagger
= cp
;
41 /* Found the tagger line. Copy out the contents
42 * of the buffer so far.
44 flush_buffer(buf
, cp
- buf
);
47 * Do something intelligent, like pretty-printing
52 /* tagger to cp is a line
53 * that has ident and time.
55 const char *sp
= tagger
;
59 while (sp
< cp
&& *sp
!= '>')
68 !('0' <= *sp
&& *sp
<= '9'))
70 flush_buffer(tagger
, sp
- tagger
);
71 date
= strtoul(sp
, &ep
, 10);
72 tz
= strtol(ep
, NULL
, 10);
73 sp
= show_date(date
, tz
);
74 flush_buffer(sp
, strlen(sp
));
81 if (cp
< endp
&& *cp
== '\n')
85 /* At this point, we have copied out the header up to the end of
86 * the tagger line and cp points at one past \n. It could be the
87 * next header line after the tagger line, or it could be another
88 * \n that marks the end of the headers. We need to copy out the
92 flush_buffer(cp
, endp
- cp
);
96 int main(int argc
, char **argv
)
98 unsigned char sha1
[20];
104 setup_git_directory();
105 git_config(git_default_config
);
107 usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");
108 if (get_sha1(argv
[2], sha1
))
109 die("Not a valid object name %s", argv
[2]);
112 if ( argv
[1][0] == '-' ) {
114 if ( !opt
|| argv
[1][2] )
115 opt
= -1; /* Not a single character option */
121 if (!sha1_object_info(sha1
, type
, NULL
)) {
122 printf("%s\n", type
);
128 if (!sha1_object_info(sha1
, type
, &size
)) {
129 printf("%lu\n", size
);
135 return !has_sha1_file(sha1
);
138 if (sha1_object_info(sha1
, type
, NULL
))
139 die("Not a valid object name %s", argv
[2]);
141 /* custom pretty-print here */
142 if (!strcmp(type
, tree_type
))
143 return execl_git_cmd("ls-tree", argv
[2], NULL
);
145 buf
= read_sha1_file(sha1
, type
, &size
);
147 die("Cannot read object %s", argv
[2]);
148 if (!strcmp(type
, tag_type
))
149 return pprint_tag(sha1
, buf
, size
);
151 /* otherwise just spit out the data */
154 buf
= read_object_with_reference(sha1
, argv
[1], &size
, NULL
);
158 die("git-cat-file: unknown option: %s\n", argv
[1]);
162 die("git-cat-file %s: bad file", argv
[2]);
164 flush_buffer(buf
, size
);