blob: 6c16bfa1ae4ae6207a74a8caa95564c649c4b0dd [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];
82 char type[20];
83 void *buf;
84 unsigned long size;
H. Peter Anvin79505712005-12-03 17:57:48 -080085 int opt;
Linus Torvaldse83c5162005-04-07 15:13:13 -070086
Junio C Hamano84a9b582006-03-23 23:41:18 -080087 git_config(git_default_config);
Dmitry V. Levin31fff302006-05-09 01:43:38 +040088 if (argc != 3)
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -080089 usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");
Dmitry V. Levin31fff302006-05-09 01:43:38 +040090 if (get_sha1(argv[2], sha1))
91 die("Not a valid object name %s", argv[2]);
Linus Torvalds11e7d5c2005-05-01 19:28:18 -070092
H. Peter Anvin79505712005-12-03 17:57:48 -080093 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 */
98 }
99
100 buf = NULL;
101 switch (opt) {
102 case 't':
103 if (!sha1_object_info(sha1, type, NULL)) {
104 printf("%s\n", type);
Junio C Hamanof2a06332005-06-27 23:58:45 -0700105 return 0;
Linus Torvalds11e7d5c2005-05-01 19:28:18 -0700106 }
H. Peter Anvin79505712005-12-03 17:57:48 -0800107 break;
108
109 case 's':
110 if (!sha1_object_info(sha1, type, &size)) {
111 printf("%lu\n", size);
112 return 0;
113 }
114 break;
115
116 case 'e':
117 return !has_sha1_file(sha1);
118
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -0800119 case 'p':
Dmitry V. Levin31fff302006-05-09 01:43:38 +0400120 if (sha1_object_info(sha1, type, NULL))
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -0800121 die("Not a valid object name %s", argv[2]);
122
123 /* custom pretty-print here */
Peter Eriksen8e440252006-04-02 14:44:09 +0200124 if (!strcmp(type, tree_type))
Petr Baudisb931aa52006-05-26 18:59:17 +0200125 return cmd_ls_tree(2, argv + 1, NULL);
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -0800126
127 buf = read_sha1_file(sha1, type, &size);
128 if (!buf)
129 die("Cannot read object %s", argv[2]);
David Rientjeseddd1c82006-08-14 13:19:15 -0700130 if (!strcmp(type, tag_type)) {
131 pprint_tag(sha1, buf, size);
132 return 0;
133 }
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -0800134
135 /* otherwise just spit out the data */
136 break;
H. Peter Anvin79505712005-12-03 17:57:48 -0800137 case 0:
Linus Torvalds11e7d5c2005-05-01 19:28:18 -0700138 buf = read_object_with_reference(sha1, argv[1], &size, NULL);
H. Peter Anvin79505712005-12-03 17:57:48 -0800139 break;
140
141 default:
142 die("git-cat-file: unknown option: %s\n", argv[1]);
Linus Torvalds11e7d5c2005-05-01 19:28:18 -0700143 }
144
Petr Baudis2de381f2005-04-13 02:28:48 -0700145 if (!buf)
Alexey Nezhdanovbab55832005-05-01 21:23:04 -0700146 die("git-cat-file %s: bad file", argv[2]);
Linus Torvaldsbf0c6e82005-04-08 09:16:38 -0700147
Rene Scharfe7230e6d2006-08-21 20:43:43 +0200148 write_or_die(1, buf, size);
Linus Torvaldsbf0c6e82005-04-08 09:16:38 -0700149 return 0;
Linus Torvaldse83c5162005-04-07 15:13:13 -0700150}