tests for various pack index features
[git/dkf.git] / builtin-cat-file.c
blobd61d3d5b74ae23bb1ab10b23f518fa7a85ac5481
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "exec_cmd.h"
8 #include "tag.h"
9 #include "tree.h"
10 #include "builtin.h"
12 static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size)
14 /* the parser in tag.c is useless here. */
15 const char *endp = buf + size;
16 const char *cp = buf;
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;
25 /* Found the tagger line. Copy out the contents
26 * of the buffer so far.
28 write_or_die(1, buf, cp - buf);
31 * Do something intelligent, like pretty-printing
32 * the date.
34 while (cp < endp) {
35 if (*cp++ == '\n') {
36 /* tagger to cp is a line
37 * that has ident and time.
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 */
47 write_or_die(1, tagger,
48 cp - tagger);
49 break;
51 while (sp < cp &&
52 !('0' <= *sp && *sp <= '9'))
53 sp++;
54 write_or_die(1, tagger, sp - tagger);
55 date = strtoul(sp, &ep, 10);
56 tz = strtol(ep, NULL, 10);
57 sp = show_date(date, tz, 0);
58 write_or_die(1, sp, strlen(sp));
59 xwrite(1, "\n", 1);
60 break;
63 break;
65 if (cp < endp && *cp == '\n')
66 /* end of header */
67 break;
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.
75 if (cp < endp)
76 write_or_die(1, cp, endp - cp);
79 int cmd_cat_file(int argc, const char **argv, const char *prefix)
81 unsigned char sha1[20];
82 enum object_type type;
83 void *buf;
84 unsigned long size;
85 int opt;
87 git_config(git_default_config);
88 if (argc != 3)
89 usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");
90 if (get_sha1(argv[2], sha1))
91 die("Not a valid object name %s", argv[2]);
93 opt = 0;
94 if ( argv[1][0] == '-' ) {
95 opt = argv[1][1];
96 if ( !opt || argv[1][2] )
97 opt = -1; /* Not a single character option */
100 buf = NULL;
101 switch (opt) {
102 case 't':
103 type = sha1_object_info(sha1, NULL);
104 if (type > 0) {
105 printf("%s\n", typename(type));
106 return 0;
108 break;
110 case 's':
111 type = sha1_object_info(sha1, &size);
112 if (type > 0) {
113 printf("%lu\n", size);
114 return 0;
116 break;
118 case 'e':
119 return !has_sha1_file(sha1);
121 case 'p':
122 type = sha1_object_info(sha1, NULL);
123 if (type < 0)
124 die("Not a valid object name %s", argv[2]);
126 /* custom pretty-print here */
127 if (type == OBJ_TREE)
128 return cmd_ls_tree(2, argv + 1, NULL);
130 buf = read_sha1_file(sha1, &type, &size);
131 if (!buf)
132 die("Cannot read object %s", argv[2]);
133 if (type == OBJ_TAG) {
134 pprint_tag(sha1, buf, size);
135 return 0;
138 /* otherwise just spit out the data */
139 break;
140 case 0:
141 buf = read_object_with_reference(sha1, argv[1], &size, NULL);
142 break;
144 default:
145 die("git-cat-file: unknown option: %s\n", argv[1]);
148 if (!buf)
149 die("git-cat-file %s: bad file", argv[2]);
151 write_or_die(1, buf, size);
152 return 0;