]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-cat-file.c
Reuse fixup_pack_header_footer in index-pack
[thirdparty/git.git] / builtin-cat-file.c
CommitLineData
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 11
eddd1c8c 12static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size)
a0f15fa5
JH
13{
14 /* the parser in tag.c is useless here. */
15 const char *endp = buf + size;
16 const char *cp = buf;
17
18 while (cp < endp) {
19 char c = *cp++;
20 if (c != '\n')
21 continue;
22 if (7 <= endp - cp && !memcmp("tagger ", cp, 7)) {
23 const char *tagger = cp;
24
25 /* Found the tagger line. Copy out the contents
26 * of the buffer so far.
27 */
7230e6d0 28 write_or_die(1, buf, cp - buf);
a0f15fa5
JH
29
30 /*
31 * Do something intelligent, like pretty-printing
32 * the date.
33 */
34 while (cp < endp) {
35 if (*cp++ == '\n') {
36 /* tagger to cp is a line
37 * that has ident and time.
38 */
39 const char *sp = tagger;
40 char *ep;
41 unsigned long date;
42 long tz;
43 while (sp < cp && *sp != '>')
44 sp++;
45 if (sp == cp) {
46 /* give up */
7230e6d0 47 write_or_die(1, tagger,
a0f15fa5
JH
48 cp - tagger);
49 break;
50 }
51 while (sp < cp &&
52 !('0' <= *sp && *sp <= '9'))
53 sp++;
7230e6d0 54 write_or_die(1, tagger, sp - tagger);
a0f15fa5
JH
55 date = strtoul(sp, &ep, 10);
56 tz = strtol(ep, NULL, 10);
9a8e35e9 57 sp = show_date(date, tz, 0);
7230e6d0 58 write_or_die(1, sp, strlen(sp));
a0f15fa5
JH
59 xwrite(1, "\n", 1);
60 break;
61 }
62 }
63 break;
64 }
65 if (cp < endp && *cp == '\n')
66 /* end of header */
67 break;
68 }
69 /* At this point, we have copied out the header up to the end of
70 * the tagger line and cp points at one past \n. It could be the
71 * next header line after the tagger line, or it could be another
72 * \n that marks the end of the headers. We need to copy out the
73 * remainder as is.
74 */
75 if (cp < endp)
7230e6d0 76 write_or_die(1, cp, endp - cp);
a0f15fa5 77}
e83c5163 78
a633fca0 79int cmd_cat_file(int argc, const char **argv, const char *prefix)
e83c5163
LT
80{
81 unsigned char sha1[20];
21666f1a 82 enum object_type type;
e83c5163
LT
83 void *buf;
84 unsigned long size;
7950571a 85 int opt;
2b6854c8 86 const char *exp_type, *obj_name;
e83c5163 87
84a9b58c 88 git_config(git_default_config);
31fff305 89 if (argc != 3)
a0f15fa5 90 usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");
2b6854c8
SP
91 exp_type = argv[1];
92 obj_name = argv[2];
93
94 if (get_sha1(obj_name, sha1))
95 die("Not a valid object name %s", obj_name);
7950571a
PA
96
97 opt = 0;
2b6854c8
SP
98 if ( exp_type[0] == '-' ) {
99 opt = exp_type[1];
100 if ( !opt || exp_type[2] )
7950571a
PA
101 opt = -1; /* Not a single character option */
102 }
103
104 buf = NULL;
105 switch (opt) {
106 case 't':
21666f1a
NP
107 type = sha1_object_info(sha1, NULL);
108 if (type > 0) {
109 printf("%s\n", typename(type));
f2a06330 110 return 0;
11e7d5c5 111 }
7950571a
PA
112 break;
113
114 case 's':
21666f1a
NP
115 type = sha1_object_info(sha1, &size);
116 if (type > 0) {
7950571a
PA
117 printf("%lu\n", size);
118 return 0;
119 }
120 break;
121
122 case 'e':
123 return !has_sha1_file(sha1);
124
a0f15fa5 125 case 'p':
21666f1a
NP
126 type = sha1_object_info(sha1, NULL);
127 if (type < 0)
2b6854c8 128 die("Not a valid object name %s", obj_name);
a0f15fa5
JH
129
130 /* custom pretty-print here */
2b6854c8
SP
131 if (type == OBJ_TREE) {
132 const char *ls_args[3] = {"ls-tree", obj_name, NULL};
133 return cmd_ls_tree(2, ls_args, NULL);
134 }
a0f15fa5 135
21666f1a 136 buf = read_sha1_file(sha1, &type, &size);
a0f15fa5 137 if (!buf)
2b6854c8 138 die("Cannot read object %s", obj_name);
21666f1a 139 if (type == OBJ_TAG) {
eddd1c8c
DR
140 pprint_tag(sha1, buf, size);
141 return 0;
142 }
a0f15fa5
JH
143
144 /* otherwise just spit out the data */
145 break;
7950571a 146 case 0:
2b6854c8 147 buf = read_object_with_reference(sha1, exp_type, &size, NULL);
7950571a
PA
148 break;
149
150 default:
2b6854c8 151 die("git-cat-file: unknown option: %s\n", exp_type);
bf0c6e83
LT
152 }
153
11e7d5c5 154 if (!buf)
2b6854c8 155 die("git-cat-file %s: bad file", obj_name);
11e7d5c5 156
7230e6d0 157 write_or_die(1, buf, size);
bf0c6e83 158 return 0;
e83c5163 159}