2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
12 static void flush_buffer(const char *buf
, unsigned long size
)
15 long ret
= xwrite(1, buf
, size
);
20 die("git-cat-file: %s", strerror(errno
));
22 die("git-cat-file: disk full?");
29 static int pprint_tag(const unsigned char *sha1
, const char *buf
, unsigned long size
)
31 /* the parser in tag.c is useless here. */
32 const char *endp
= buf
+ size
;
39 if (7 <= endp
- cp
&& !memcmp("tagger ", cp
, 7)) {
40 const char *tagger
= cp
;
42 /* Found the tagger line. Copy out the contents
43 * of the buffer so far.
45 flush_buffer(buf
, cp
- buf
);
48 * Do something intelligent, like pretty-printing
53 /* tagger to cp is a line
54 * that has ident and time.
56 const char *sp
= tagger
;
60 while (sp
< cp
&& *sp
!= '>')
69 !('0' <= *sp
&& *sp
<= '9'))
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
));
82 if (cp
< endp
&& *cp
== '\n')
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
93 flush_buffer(cp
, endp
- cp
);
97 int cmd_cat_file(int argc
, const char **argv
, char **envp
)
99 unsigned char sha1
[20];
105 setup_git_directory();
106 git_config(git_default_config
);
108 usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");
109 if (get_sha1(argv
[2], sha1
))
110 die("Not a valid object name %s", argv
[2]);
113 if ( argv
[1][0] == '-' ) {
115 if ( !opt
|| argv
[1][2] )
116 opt
= -1; /* Not a single character option */
122 if (!sha1_object_info(sha1
, type
, NULL
)) {
123 printf("%s\n", type
);
129 if (!sha1_object_info(sha1
, type
, &size
)) {
130 printf("%lu\n", size
);
136 return !has_sha1_file(sha1
);
139 if (sha1_object_info(sha1
, type
, NULL
))
140 die("Not a valid object name %s", argv
[2]);
142 /* custom pretty-print here */
143 if (!strcmp(type
, tree_type
))
144 return cmd_ls_tree(2, argv
+ 1, NULL
);
146 buf
= read_sha1_file(sha1
, type
, &size
);
148 die("Cannot read object %s", argv
[2]);
149 if (!strcmp(type
, tag_type
))
150 return pprint_tag(sha1
, buf
, size
);
152 /* otherwise just spit out the data */
155 buf
= read_object_with_reference(sha1
, argv
[1], &size
, NULL
);
159 die("git-cat-file: unknown option: %s\n", argv
[1]);
163 die("git-cat-file %s: bad file", argv
[2]);
165 flush_buffer(buf
, size
);