blob: 3fba6b9e743545868368a0e554466fca3814316a [file] [log] [blame]
Linus Torvalds8bc9a0c2005-04-07 15:16:10 -07001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
Linus Torvaldse83c5162005-04-07 15:13:13 -07006#include "cache.h"
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -08007#include "exec_cmd.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02008#include "tag.h"
9#include "tree.h"
Timo Hirvonenf81daef2006-05-24 14:08:46 +030010#include "builtin.h"
Michele Ballabio15d8e562008-05-23 16:19:42 +020011#include "parse-options.h"
12
13#define BATCH 1
14#define BATCH_CHECK 2
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -080015
David Rientjeseddd1c82006-08-14 13:19:15 -070016static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size)
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -080017{
18 /* the parser in tag.c is useless here. */
19 const char *endp = buf + size;
20 const char *cp = buf;
21
22 while (cp < endp) {
23 char c = *cp++;
24 if (c != '\n')
25 continue;
26 if (7 <= endp - cp && !memcmp("tagger ", cp, 7)) {
27 const char *tagger = cp;
28
29 /* Found the tagger line. Copy out the contents
30 * of the buffer so far.
31 */
Rene Scharfe7230e6d2006-08-21 20:43:43 +020032 write_or_die(1, buf, cp - buf);
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -080033
34 /*
35 * Do something intelligent, like pretty-printing
36 * the date.
37 */
38 while (cp < endp) {
39 if (*cp++ == '\n') {
40 /* tagger to cp is a line
41 * that has ident and time.
42 */
43 const char *sp = tagger;
44 char *ep;
45 unsigned long date;
46 long tz;
47 while (sp < cp && *sp != '>')
48 sp++;
49 if (sp == cp) {
50 /* give up */
Rene Scharfe7230e6d2006-08-21 20:43:43 +020051 write_or_die(1, tagger,
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -080052 cp - tagger);
53 break;
54 }
55 while (sp < cp &&
56 !('0' <= *sp && *sp <= '9'))
57 sp++;
Rene Scharfe7230e6d2006-08-21 20:43:43 +020058 write_or_die(1, tagger, sp - tagger);
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -080059 date = strtoul(sp, &ep, 10);
60 tz = strtol(ep, NULL, 10);
Linus Torvalds9a8e35e2006-08-26 15:45:26 -070061 sp = show_date(date, tz, 0);
Rene Scharfe7230e6d2006-08-21 20:43:43 +020062 write_or_die(1, sp, strlen(sp));
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -080063 xwrite(1, "\n", 1);
64 break;
65 }
66 }
67 break;
68 }
69 if (cp < endp && *cp == '\n')
70 /* end of header */
71 break;
72 }
73 /* At this point, we have copied out the header up to the end of
74 * the tagger line and cp points at one past \n. It could be the
75 * next header line after the tagger line, or it could be another
76 * \n that marks the end of the headers. We need to copy out the
77 * remainder as is.
78 */
79 if (cp < endp)
Rene Scharfe7230e6d2006-08-21 20:43:43 +020080 write_or_die(1, cp, endp - cp);
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -080081}
Linus Torvaldse83c5162005-04-07 15:13:13 -070082
Adam Roben9cf71b12008-04-23 15:17:44 -040083static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
Linus Torvaldse83c5162005-04-07 15:13:13 -070084{
85 unsigned char sha1[20];
Nicolas Pitre21666f12007-02-26 14:55:59 -050086 enum object_type type;
Linus Torvaldse83c5162005-04-07 15:13:13 -070087 void *buf;
88 unsigned long size;
Shawn O. Pearce2b6854c2007-04-21 21:14:39 -040089
90 if (get_sha1(obj_name, sha1))
91 die("Not a valid object name %s", obj_name);
Linus Torvalds11e7d5c2005-05-01 19:28:18 -070092
H. Peter Anvin79505712005-12-03 17:57:48 -080093 buf = NULL;
94 switch (opt) {
95 case 't':
Nicolas Pitre21666f12007-02-26 14:55:59 -050096 type = sha1_object_info(sha1, NULL);
97 if (type > 0) {
98 printf("%s\n", typename(type));
Junio C Hamanof2a06332005-06-27 23:58:45 -070099 return 0;
Linus Torvalds11e7d5c2005-05-01 19:28:18 -0700100 }
H. Peter Anvin79505712005-12-03 17:57:48 -0800101 break;
102
103 case 's':
Nicolas Pitre21666f12007-02-26 14:55:59 -0500104 type = sha1_object_info(sha1, &size);
105 if (type > 0) {
H. Peter Anvin79505712005-12-03 17:57:48 -0800106 printf("%lu\n", size);
107 return 0;
108 }
109 break;
110
111 case 'e':
112 return !has_sha1_file(sha1);
113
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -0800114 case 'p':
Nicolas Pitre21666f12007-02-26 14:55:59 -0500115 type = sha1_object_info(sha1, NULL);
116 if (type < 0)
Shawn O. Pearce2b6854c2007-04-21 21:14:39 -0400117 die("Not a valid object name %s", obj_name);
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -0800118
119 /* custom pretty-print here */
Shawn O. Pearce2b6854c2007-04-21 21:14:39 -0400120 if (type == OBJ_TREE) {
121 const char *ls_args[3] = {"ls-tree", obj_name, NULL};
122 return cmd_ls_tree(2, ls_args, NULL);
123 }
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -0800124
Nicolas Pitre21666f12007-02-26 14:55:59 -0500125 buf = read_sha1_file(sha1, &type, &size);
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -0800126 if (!buf)
Shawn O. Pearce2b6854c2007-04-21 21:14:39 -0400127 die("Cannot read object %s", obj_name);
Nicolas Pitre21666f12007-02-26 14:55:59 -0500128 if (type == OBJ_TAG) {
David Rientjeseddd1c82006-08-14 13:19:15 -0700129 pprint_tag(sha1, buf, size);
130 return 0;
131 }
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -0800132
133 /* otherwise just spit out the data */
134 break;
H. Peter Anvin79505712005-12-03 17:57:48 -0800135 case 0:
Shawn O. Pearce2b6854c2007-04-21 21:14:39 -0400136 buf = read_object_with_reference(sha1, exp_type, &size, NULL);
H. Peter Anvin79505712005-12-03 17:57:48 -0800137 break;
138
139 default:
Heikki Orsila34baebc2008-08-30 14:12:53 +0300140 die("git cat-file: unknown option: %s\n", exp_type);
Linus Torvalds11e7d5c2005-05-01 19:28:18 -0700141 }
142
Petr Baudis2de381f2005-04-13 02:28:48 -0700143 if (!buf)
Heikki Orsila34baebc2008-08-30 14:12:53 +0300144 die("git cat-file %s: bad file", obj_name);
Linus Torvaldsbf0c6e82005-04-08 09:16:38 -0700145
Rene Scharfe7230e6d2006-08-21 20:43:43 +0200146 write_or_die(1, buf, size);
Linus Torvaldsbf0c6e82005-04-08 09:16:38 -0700147 return 0;
Linus Torvaldse83c5162005-04-07 15:13:13 -0700148}
Adam Roben9cf71b12008-04-23 15:17:44 -0400149
Adam Robena8128ed2008-04-23 15:17:47 -0400150static int batch_one_object(const char *obj_name, int print_contents)
Adam Roben05d56672008-04-23 15:17:46 -0400151{
152 unsigned char sha1[20];
Lea Wiemann3c076db2008-06-09 02:02:21 +0200153 enum object_type type = 0;
Adam Roben05d56672008-04-23 15:17:46 -0400154 unsigned long size;
Adam Robena8128ed2008-04-23 15:17:47 -0400155 void *contents = contents;
Adam Roben05d56672008-04-23 15:17:46 -0400156
157 if (!obj_name)
158 return 1;
159
160 if (get_sha1(obj_name, sha1)) {
161 printf("%s missing\n", obj_name);
Lea Wiemann422b2062008-06-03 20:34:17 +0200162 fflush(stdout);
Adam Roben05d56672008-04-23 15:17:46 -0400163 return 0;
164 }
165
Michele Ballabio15d8e562008-05-23 16:19:42 +0200166 if (print_contents == BATCH)
Adam Robena8128ed2008-04-23 15:17:47 -0400167 contents = read_sha1_file(sha1, &type, &size);
168 else
169 type = sha1_object_info(sha1, &size);
170
Lea Wiemann3c076db2008-06-09 02:02:21 +0200171 if (type <= 0) {
172 printf("%s missing\n", obj_name);
173 fflush(stdout);
174 return 0;
175 }
Adam Roben05d56672008-04-23 15:17:46 -0400176
177 printf("%s %s %lu\n", sha1_to_hex(sha1), typename(type), size);
Adam Robena8128ed2008-04-23 15:17:47 -0400178 fflush(stdout);
179
Michele Ballabio15d8e562008-05-23 16:19:42 +0200180 if (print_contents == BATCH) {
Adam Robena8128ed2008-04-23 15:17:47 -0400181 write_or_die(1, contents, size);
182 printf("\n");
183 fflush(stdout);
Björn Steinbrink5b8a94b2008-06-29 03:21:25 +0200184 free(contents);
Adam Robena8128ed2008-04-23 15:17:47 -0400185 }
Adam Roben05d56672008-04-23 15:17:46 -0400186
187 return 0;
188}
189
Adam Robena8128ed2008-04-23 15:17:47 -0400190static int batch_objects(int print_contents)
Adam Roben05d56672008-04-23 15:17:46 -0400191{
192 struct strbuf buf;
193
194 strbuf_init(&buf, 0);
195 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
Adam Robena8128ed2008-04-23 15:17:47 -0400196 int error = batch_one_object(buf.buf, print_contents);
Adam Roben05d56672008-04-23 15:17:46 -0400197 if (error)
198 return error;
199 }
200
201 return 0;
202}
203
Michele Ballabio15d8e562008-05-23 16:19:42 +0200204static const char * const cat_file_usage[] = {
Stephan Beyer1b1dd232008-07-13 15:36:15 +0200205 "git cat-file [-t|-s|-e|-p|<type>] <sha1>",
206 "git cat-file [--batch|--batch-check] < <list_of_sha1s>",
Michele Ballabio15d8e562008-05-23 16:19:42 +0200207 NULL
208};
Adam Roben4814dbe2008-04-23 15:17:45 -0400209
Adam Roben9cf71b12008-04-23 15:17:44 -0400210int cmd_cat_file(int argc, const char **argv, const char *prefix)
211{
Michele Ballabio15d8e562008-05-23 16:19:42 +0200212 int opt = 0, batch = 0;
Adam Roben4814dbe2008-04-23 15:17:45 -0400213 const char *exp_type = NULL, *obj_name = NULL;
Adam Roben9cf71b12008-04-23 15:17:44 -0400214
Michele Ballabio15d8e562008-05-23 16:19:42 +0200215 const struct option options[] = {
216 OPT_GROUP("<type> can be one of: blob, tree, commit, tag"),
217 OPT_SET_INT('t', NULL, &opt, "show object type", 't'),
218 OPT_SET_INT('s', NULL, &opt, "show object size", 's'),
219 OPT_SET_INT('e', NULL, &opt,
220 "exit with zero when there's no error", 'e'),
221 OPT_SET_INT('p', NULL, &opt, "pretty-print object's content", 'p'),
222 OPT_SET_INT(0, "batch", &batch,
223 "show info and content of objects feeded on stdin", BATCH),
224 OPT_SET_INT(0, "batch-check", &batch,
225 "show info about objects feeded on stdin",
226 BATCH_CHECK),
227 OPT_END()
228 };
229
Junio C Hamano9bd81e42008-05-25 14:25:02 -0700230 git_config(git_default_config, NULL);
Adam Roben9cf71b12008-04-23 15:17:44 -0400231
Michele Ballabio15d8e562008-05-23 16:19:42 +0200232 if (argc != 3 && argc != 2)
233 usage_with_options(cat_file_usage, options);
Adam Roben4814dbe2008-04-23 15:17:45 -0400234
Michele Ballabio15d8e562008-05-23 16:19:42 +0200235 argc = parse_options(argc, argv, options, cat_file_usage, 0);
Adam Robena8128ed2008-04-23 15:17:47 -0400236
Michele Ballabio15d8e562008-05-23 16:19:42 +0200237 if (opt) {
238 if (argc == 1)
239 obj_name = argv[0];
240 else
241 usage_with_options(cat_file_usage, options);
242 }
243 if (!opt && !batch) {
244 if (argc == 2) {
245 exp_type = argv[0];
246 obj_name = argv[1];
247 } else
248 usage_with_options(cat_file_usage, options);
249 }
250 if (batch && (opt || argc)) {
251 usage_with_options(cat_file_usage, options);
Adam Roben9cf71b12008-04-23 15:17:44 -0400252 }
253
Michele Ballabio15d8e562008-05-23 16:19:42 +0200254 if (batch)
Adam Robena8128ed2008-04-23 15:17:47 -0400255 return batch_objects(batch);
Adam Roben05d56672008-04-23 15:17:46 -0400256
Adam Roben9cf71b12008-04-23 15:17:44 -0400257 return cat_one_file(opt, exp_type, obj_name);
258}