t1006: modernize output comparisons
[git/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"
15d8e565 11#include "parse-options.h"
e5fba602
CP
12#include "diff.h"
13#include "userdiff.h"
00c8fd49 14#include "streaming.h"
15d8e565
MB
15
16#define BATCH 1
17#define BATCH_CHECK 2
a0f15fa5 18
9cf71b17 19static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
e83c5163
LT
20{
21 unsigned char sha1[20];
21666f1a 22 enum object_type type;
e5fba602 23 char *buf;
e83c5163 24 unsigned long size;
e5fba602 25 struct object_context obj_context;
2b6854c8 26
33bd598c 27 if (get_sha1_with_context(obj_name, 0, sha1, &obj_context))
2b6854c8 28 die("Not a valid object name %s", obj_name);
7950571a 29
7950571a
PA
30 buf = NULL;
31 switch (opt) {
32 case 't':
21666f1a
NP
33 type = sha1_object_info(sha1, NULL);
34 if (type > 0) {
35 printf("%s\n", typename(type));
f2a06330 36 return 0;
11e7d5c5 37 }
7950571a
PA
38 break;
39
40 case 's':
21666f1a
NP
41 type = sha1_object_info(sha1, &size);
42 if (type > 0) {
7950571a
PA
43 printf("%lu\n", size);
44 return 0;
45 }
46 break;
47
48 case 'e':
49 return !has_sha1_file(sha1);
50
a0f15fa5 51 case 'p':
21666f1a
NP
52 type = sha1_object_info(sha1, NULL);
53 if (type < 0)
2b6854c8 54 die("Not a valid object name %s", obj_name);
a0f15fa5
JH
55
56 /* custom pretty-print here */
2b6854c8 57 if (type == OBJ_TREE) {
66dbfd55
GV
58 const char *ls_args[3] = { NULL };
59 ls_args[0] = "ls-tree";
60 ls_args[1] = obj_name;
2b6854c8
SP
61 return cmd_ls_tree(2, ls_args, NULL);
62 }
a0f15fa5 63
00c8fd49
NTND
64 if (type == OBJ_BLOB)
65 return stream_blob_to_fd(1, sha1, NULL, 0);
21666f1a 66 buf = read_sha1_file(sha1, &type, &size);
a0f15fa5 67 if (!buf)
2b6854c8 68 die("Cannot read object %s", obj_name);
a0f15fa5
JH
69
70 /* otherwise just spit out the data */
71 break;
e5fba602
CP
72
73 case 'c':
74 if (!obj_context.path[0])
75 die("git cat-file --textconv %s: <object> must be <sha1:path>",
76 obj_name);
77
e5450100 78 if (!textconv_object(obj_context.path, obj_context.mode, sha1, 1, &buf, &size))
e5fba602
CP
79 die("git cat-file --textconv: unable to run textconv on %s",
80 obj_name);
81 break;
82
7950571a 83 case 0:
00c8fd49
NTND
84 if (type_from_string(exp_type) == OBJ_BLOB) {
85 unsigned char blob_sha1[20];
86 if (sha1_object_info(sha1, NULL) == OBJ_TAG) {
87 enum object_type type;
88 unsigned long size;
89 char *buffer = read_sha1_file(sha1, &type, &size);
90 if (memcmp(buffer, "object ", 7) ||
91 get_sha1_hex(buffer + 7, blob_sha1))
92 die("%s not a valid tag", sha1_to_hex(sha1));
93 free(buffer);
94 } else
95 hashcpy(blob_sha1, sha1);
96
97 if (sha1_object_info(blob_sha1, NULL) == OBJ_BLOB)
98 return stream_blob_to_fd(1, blob_sha1, NULL, 0);
99 /*
100 * we attempted to dereference a tag to a blob
101 * and failed; there may be new dereference
102 * mechanisms this code is not aware of.
103 * fall-back to the usual case.
104 */
105 }
2b6854c8 106 buf = read_object_with_reference(sha1, exp_type, &size, NULL);
7950571a
PA
107 break;
108
109 default:
d7530708 110 die("git cat-file: unknown option: %s", exp_type);
bf0c6e83
LT
111 }
112
11e7d5c5 113 if (!buf)
34baebce 114 die("git cat-file %s: bad file", obj_name);
11e7d5c5 115
7230e6d0 116 write_or_die(1, buf, size);
bf0c6e83 117 return 0;
e83c5163 118}
9cf71b17 119
a8128ed6 120static int batch_one_object(const char *obj_name, int print_contents)
05d5667f
AR
121{
122 unsigned char sha1[20];
3c076dbe 123 enum object_type type = 0;
05d5667f 124 unsigned long size;
803a7779 125 void *contents = NULL;
05d5667f
AR
126
127 if (!obj_name)
128 return 1;
129
130 if (get_sha1(obj_name, sha1)) {
131 printf("%s missing\n", obj_name);
422b2063 132 fflush(stdout);
05d5667f
AR
133 return 0;
134 }
135
15d8e565 136 if (print_contents == BATCH)
a8128ed6
AR
137 contents = read_sha1_file(sha1, &type, &size);
138 else
139 type = sha1_object_info(sha1, &size);
140
3c076dbe
LW
141 if (type <= 0) {
142 printf("%s missing\n", obj_name);
143 fflush(stdout);
dc4cd767
JM
144 if (print_contents == BATCH)
145 free(contents);
3c076dbe
LW
146 return 0;
147 }
05d5667f
AR
148
149 printf("%s %s %lu\n", sha1_to_hex(sha1), typename(type), size);
a8128ed6
AR
150 fflush(stdout);
151
15d8e565 152 if (print_contents == BATCH) {
a8128ed6
AR
153 write_or_die(1, contents, size);
154 printf("\n");
155 fflush(stdout);
5b8a94b1 156 free(contents);
a8128ed6 157 }
05d5667f
AR
158
159 return 0;
160}
161
a8128ed6 162static int batch_objects(int print_contents)
05d5667f 163{
f285a2d7 164 struct strbuf buf = STRBUF_INIT;
05d5667f 165
05d5667f 166 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
a8128ed6 167 int error = batch_one_object(buf.buf, print_contents);
05d5667f
AR
168 if (error)
169 return error;
170 }
171
172 return 0;
173}
174
15d8e565 175static const char * const cat_file_usage[] = {
d68faec7
NTND
176 N_("git cat-file (-t|-s|-e|-p|<type>|--textconv) <object>"),
177 N_("git cat-file (--batch|--batch-check) < <list_of_objects>"),
15d8e565
MB
178 NULL
179};
4814dbe8 180
e5fba602
CP
181static int git_cat_file_config(const char *var, const char *value, void *cb)
182{
6680a087 183 if (userdiff_config(var, value) < 0)
e5fba602 184 return -1;
e5fba602
CP
185
186 return git_default_config(var, value, cb);
187}
188
9cf71b17
AR
189int cmd_cat_file(int argc, const char **argv, const char *prefix)
190{
15d8e565 191 int opt = 0, batch = 0;
4814dbe8 192 const char *exp_type = NULL, *obj_name = NULL;
9cf71b17 193
15d8e565 194 const struct option options[] = {
d68faec7
NTND
195 OPT_GROUP(N_("<type> can be one of: blob, tree, commit, tag")),
196 OPT_SET_INT('t', NULL, &opt, N_("show object type"), 't'),
197 OPT_SET_INT('s', NULL, &opt, N_("show object size"), 's'),
15d8e565 198 OPT_SET_INT('e', NULL, &opt,
d68faec7
NTND
199 N_("exit with zero when there's no error"), 'e'),
200 OPT_SET_INT('p', NULL, &opt, N_("pretty-print object's content"), 'p'),
e5fba602 201 OPT_SET_INT(0, "textconv", &opt,
d68faec7 202 N_("for blob objects, run textconv on object's content"), 'c'),
15d8e565 203 OPT_SET_INT(0, "batch", &batch,
d68faec7 204 N_("show info and content of objects fed from the standard input"),
9517e6b8 205 BATCH),
15d8e565 206 OPT_SET_INT(0, "batch-check", &batch,
d68faec7 207 N_("show info about objects fed from the standard input"),
15d8e565
MB
208 BATCH_CHECK),
209 OPT_END()
210 };
a8128ed6 211
e5fba602 212 git_config(git_cat_file_config, NULL);
4814dbe8 213
15d8e565
MB
214 if (argc != 3 && argc != 2)
215 usage_with_options(cat_file_usage, options);
4814dbe8 216
37782920 217 argc = parse_options(argc, argv, prefix, options, cat_file_usage, 0);
05d5667f 218
15d8e565
MB
219 if (opt) {
220 if (argc == 1)
221 obj_name = argv[0];
222 else
223 usage_with_options(cat_file_usage, options);
224 }
225 if (!opt && !batch) {
226 if (argc == 2) {
227 exp_type = argv[0];
228 obj_name = argv[1];
229 } else
230 usage_with_options(cat_file_usage, options);
231 }
232 if (batch && (opt || argc)) {
233 usage_with_options(cat_file_usage, options);
9cf71b17
AR
234 }
235
15d8e565 236 if (batch)
a8128ed6 237 return batch_objects(batch);
05d5667f 238
9cf71b17
AR
239 return cat_one_file(opt, exp_type, obj_name);
240}