blob: f132d583d3e2a2ac0fe696b66723c846902d0a19 [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"
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -080011
David Rientjeseddd1c82006-08-14 13:19:15 -070012static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size)
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -080013{
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 */
Rene Scharfe7230e6d2006-08-21 20:43:43 +020028 write_or_die(1, buf, cp - buf);
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -080029
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 */
Rene Scharfe7230e6d2006-08-21 20:43:43 +020047 write_or_die(1, tagger,
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -080048 cp - tagger);
49 break;
50 }
51 while (sp < cp &&
52 !('0' <= *sp && *sp <= '9'))
53 sp++;
Rene Scharfe7230e6d2006-08-21 20:43:43 +020054 write_or_die(1, tagger, sp - tagger);
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -080055 date = strtoul(sp, &ep, 10);
56 tz = strtol(ep, NULL, 10);
Linus Torvalds9a8e35e2006-08-26 15:45:26 -070057 sp = show_date(date, tz, 0);
Rene Scharfe7230e6d2006-08-21 20:43:43 +020058 write_or_die(1, sp, strlen(sp));
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -080059 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)
Rene Scharfe7230e6d2006-08-21 20:43:43 +020076 write_or_die(1, cp, endp - cp);
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -080077}
Linus Torvaldse83c5162005-04-07 15:13:13 -070078
Linus Torvaldsa633fca2006-07-28 22:44:25 -070079int cmd_cat_file(int argc, const char **argv, const char *prefix)
Linus Torvaldse83c5162005-04-07 15:13:13 -070080{
81 unsigned char sha1[20];
Nicolas Pitre21666f12007-02-26 14:55:59 -050082 enum object_type type;
Linus Torvaldse83c5162005-04-07 15:13:13 -070083 void *buf;
84 unsigned long size;
H. Peter Anvin79505712005-12-03 17:57:48 -080085 int opt;
Shawn O. Pearce2b6854c2007-04-21 21:14:39 -040086 const char *exp_type, *obj_name;
Linus Torvaldse83c5162005-04-07 15:13:13 -070087
Junio C Hamano84a9b582006-03-23 23:41:18 -080088 git_config(git_default_config);
Dmitry V. Levin31fff302006-05-09 01:43:38 +040089 if (argc != 3)
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -080090 usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");
Shawn O. Pearce2b6854c2007-04-21 21:14:39 -040091 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);
Linus Torvalds11e7d5c2005-05-01 19:28:18 -070096
H. Peter Anvin79505712005-12-03 17:57:48 -080097 opt = 0;
Shawn O. Pearce2b6854c2007-04-21 21:14:39 -040098 if ( exp_type[0] == '-' ) {
99 opt = exp_type[1];
100 if ( !opt || exp_type[2] )
H. Peter Anvin79505712005-12-03 17:57:48 -0800101 opt = -1; /* Not a single character option */
102 }
103
104 buf = NULL;
105 switch (opt) {
106 case 't':
Nicolas Pitre21666f12007-02-26 14:55:59 -0500107 type = sha1_object_info(sha1, NULL);
108 if (type > 0) {
109 printf("%s\n", typename(type));
Junio C Hamanof2a06332005-06-27 23:58:45 -0700110 return 0;
Linus Torvalds11e7d5c2005-05-01 19:28:18 -0700111 }
H. Peter Anvin79505712005-12-03 17:57:48 -0800112 break;
113
114 case 's':
Nicolas Pitre21666f12007-02-26 14:55:59 -0500115 type = sha1_object_info(sha1, &size);
116 if (type > 0) {
H. Peter Anvin79505712005-12-03 17:57:48 -0800117 printf("%lu\n", size);
118 return 0;
119 }
120 break;
121
122 case 'e':
123 return !has_sha1_file(sha1);
124
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -0800125 case 'p':
Nicolas Pitre21666f12007-02-26 14:55:59 -0500126 type = sha1_object_info(sha1, NULL);
127 if (type < 0)
Shawn O. Pearce2b6854c2007-04-21 21:14:39 -0400128 die("Not a valid object name %s", obj_name);
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -0800129
130 /* custom pretty-print here */
Shawn O. Pearce2b6854c2007-04-21 21:14:39 -0400131 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 }
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -0800135
Nicolas Pitre21666f12007-02-26 14:55:59 -0500136 buf = read_sha1_file(sha1, &type, &size);
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -0800137 if (!buf)
Shawn O. Pearce2b6854c2007-04-21 21:14:39 -0400138 die("Cannot read object %s", obj_name);
Nicolas Pitre21666f12007-02-26 14:55:59 -0500139 if (type == OBJ_TAG) {
David Rientjeseddd1c82006-08-14 13:19:15 -0700140 pprint_tag(sha1, buf, size);
141 return 0;
142 }
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -0800143
144 /* otherwise just spit out the data */
145 break;
H. Peter Anvin79505712005-12-03 17:57:48 -0800146 case 0:
Shawn O. Pearce2b6854c2007-04-21 21:14:39 -0400147 buf = read_object_with_reference(sha1, exp_type, &size, NULL);
H. Peter Anvin79505712005-12-03 17:57:48 -0800148 break;
149
150 default:
Shawn O. Pearce2b6854c2007-04-21 21:14:39 -0400151 die("git-cat-file: unknown option: %s\n", exp_type);
Linus Torvalds11e7d5c2005-05-01 19:28:18 -0700152 }
153
Petr Baudis2de381f2005-04-13 02:28:48 -0700154 if (!buf)
Shawn O. Pearce2b6854c2007-04-21 21:14:39 -0400155 die("git-cat-file %s: bad file", obj_name);
Linus Torvaldsbf0c6e82005-04-08 09:16:38 -0700156
Rene Scharfe7230e6d2006-08-21 20:43:43 +0200157 write_or_die(1, buf, size);
Linus Torvaldsbf0c6e82005-04-08 09:16:38 -0700158 return 0;
Linus Torvaldse83c5162005-04-07 15:13:13 -0700159}