blob: bfdfb51c7cb7b3cba47ae77b9041814fe9307407 [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 */
John Cai03eae9a2024-09-13 21:16:15 +00006#define USE_THE_REPOSITORY_VARIABLE
Elijah Newrenbc5c5ec2023-05-16 06:33:57 +00007#include "builtin.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07008#include "config.h"
Elijah Newren73359a92023-04-11 03:00:40 +00009#include "convert.h"
Jeff Smith3a35cb22017-05-24 00:15:10 -050010#include "diff.h"
Elijah Newren32a8f512023-03-21 06:26:03 +000011#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +000012#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +000013#include "hex.h"
Elijah Newrenb5fa6082023-02-24 00:09:29 +000014#include "ident.h"
Michele Ballabio15d8e562008-05-23 16:19:42 +020015#include "parse-options.h"
Clément Poulaine5fba602010-06-15 17:50:28 +020016#include "userdiff.h"
Nguyễn Thái Ngọc Duy00c8fd42012-03-07 17:54:17 +070017#include "streaming.h"
Jeff Kingfe299ec2020-03-30 10:03:46 -040018#include "oid-array.h"
Jonathan Tan7709f462017-08-18 15:20:38 -070019#include "packfile.h"
Elijah Newren87bed172023-04-11 00:41:53 -070020#include "object-file.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -070021#include "object-name.h"
Elijah Newrena034e912023-05-16 06:34:06 +000022#include "object-store-ll.h"
Elijah Newrencbeab742023-02-24 00:09:33 +000023#include "replace-object.h"
Christian Couderb14ed5a2019-06-25 15:40:31 +020024#include "promisor-remote.h"
Siddharth Asthanaec031da2022-07-19 01:21:02 +053025#include "mailmap.h"
Elijah Newrend48be352023-03-21 06:26:07 +000026#include "write-or-die.h"
Michele Ballabio15d8e562008-05-23 16:19:42 +020027
John Caiac4e58c2022-02-18 18:23:12 +000028enum batch_mode {
29 BATCH_MODE_CONTENTS,
30 BATCH_MODE_INFO,
John Cai440c7052022-02-18 18:23:14 +000031 BATCH_MODE_QUEUE_AND_DISPATCH,
John Caiac4e58c2022-02-18 18:23:12 +000032};
33
Jeff Kingbfd15592015-06-22 06:41:03 -040034struct batch_options {
35 int enabled;
36 int follow_symlinks;
John Caiac4e58c2022-02-18 18:23:12 +000037 enum batch_mode batch_mode;
Jeff Kingfc4937c2015-06-22 06:45:17 -040038 int buffer_output;
Jeff King6a951932015-06-22 06:45:59 -040039 int all_objects;
Jeff King0750bb52018-08-10 19:24:57 -040040 int unordered;
John Caia2c75522022-02-18 18:23:11 +000041 int transform_mode; /* may be 'w' or 'c' for --filters or --textconv */
Patrick Steinhardt3217f522023-06-06 07:19:41 +020042 char input_delim;
Patrick Steinhardtf79e1882023-06-06 07:19:45 +020043 char output_delim;
Jeff Kingbfd15592015-06-22 06:41:03 -040044 const char *format;
45};
46
Johannes Schindelin7bcf3412016-09-09 12:10:50 +020047static const char *force_path;
48
Siddharth Asthanaec031da2022-07-19 01:21:02 +053049static struct string_list mailmap = STRING_LIST_INIT_NODUP;
50static int use_mailmap;
51
52static char *replace_idents_using_mailmap(char *, size_t *);
53
54static char *replace_idents_using_mailmap(char *object_buf, size_t *size)
55{
56 struct strbuf sb = STRBUF_INIT;
57 const char *headers[] = { "author ", "committer ", "tagger ", NULL };
58
59 strbuf_attach(&sb, object_buf, *size, *size + 1);
60 apply_mailmap_to_header(&sb, headers, &mailmap);
61 *size = sb.len;
62 return strbuf_detach(&sb, NULL);
63}
64
Johannes Schindelinb9e62f62016-08-24 14:23:39 +020065static int filter_object(const char *path, unsigned mode,
Junio C Hamano7889ed22016-09-21 15:15:18 -070066 const struct object_id *oid,
Johannes Schindelinb9e62f62016-08-24 14:23:39 +020067 char **buf, unsigned long *size)
68{
69 enum object_type type;
70
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +020071 *buf = repo_read_object_file(the_repository, oid, &type, size);
Johannes Schindelinb9e62f62016-08-24 14:23:39 +020072 if (!*buf)
73 return error(_("cannot read object %s '%s'"),
Junio C Hamano7889ed22016-09-21 15:15:18 -070074 oid_to_hex(oid), path);
Johannes Schindelinb9e62f62016-08-24 14:23:39 +020075 if ((type == OBJ_BLOB) && S_ISREG(mode)) {
76 struct strbuf strbuf = STRBUF_INIT;
brian m. carlsonc397aac2020-03-16 18:05:03 +000077 struct checkout_metadata meta;
78
79 init_checkout_metadata(&meta, NULL, NULL, oid);
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +020080 if (convert_to_working_tree(the_repository->index, path, *buf, *size, &strbuf, &meta)) {
Johannes Schindelinb9e62f62016-08-24 14:23:39 +020081 free(*buf);
82 *size = strbuf.len;
83 *buf = strbuf_detach(&strbuf, NULL);
84 }
85 }
86
87 return 0;
88}
89
Jeff King98f425b2018-10-30 19:23:38 -040090static int stream_blob(const struct object_id *oid)
91{
92 if (stream_blob_to_fd(1, oid, NULL, 0))
93 die("unable to stream %s to stdout", oid_to_hex(oid));
94 return 0;
95}
96
Karthik Nayak39e4ae32015-05-03 20:00:01 +053097static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
98 int unknown_type)
Linus Torvaldse83c5162005-04-07 15:13:13 -070099{
Ævar Arnfjörð Bjarmason27472b52022-07-01 12:42:59 +0200100 int ret;
brian m. carlson63ecb992016-09-05 20:07:57 +0000101 struct object_id oid;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500102 enum object_type type;
Clément Poulaine5fba602010-06-15 17:50:28 +0200103 char *buf;
Linus Torvaldse83c5162005-04-07 15:13:13 -0700104 unsigned long size;
Patrick Steinhardtf87c55c2024-06-11 11:19:59 +0200105 struct object_context obj_context = {0};
Jeff King27b5c1a2016-08-11 05:24:35 -0400106 struct object_info oi = OBJECT_INFO_INIT;
Karthik Nayak39e4ae32015-05-03 20:00:01 +0530107 struct strbuf sb = STRBUF_INIT;
Jonathan Tan1f0c0d32017-06-21 17:40:19 -0700108 unsigned flags = OBJECT_INFO_LOOKUP_REPLACE;
Eric W. Biedermand6222a22023-10-01 21:40:27 -0500109 unsigned get_oid_flags =
110 GET_OID_RECORD_PATH |
111 GET_OID_ONLY_TO_DIE |
112 GET_OID_HASH_ANY;
Johannes Schindelin7bcf3412016-09-09 12:10:50 +0200113 const char *path = force_path;
Ævar Arnfjörð Bjarmason245b9482021-12-28 14:28:50 +0100114 const int opt_cw = (opt == 'c' || opt == 'w');
115 if (!path && opt_cw)
116 get_oid_flags |= GET_OID_REQUIRE_PATH;
Karthik Nayak39e4ae32015-05-03 20:00:01 +0530117
118 if (unknown_type)
Jonathan Tan19fc5e82017-06-21 17:40:18 -0700119 flags |= OBJECT_INFO_ALLOW_UNKNOWN_TYPE;
Shawn O. Pearce2b6854c2007-04-21 21:14:39 -0400120
Ævar Arnfjörð Bjarmason245b9482021-12-28 14:28:50 +0100121 if (get_oid_with_context(the_repository, obj_name, get_oid_flags, &oid,
122 &obj_context))
Shawn O. Pearce2b6854c2007-04-21 21:14:39 -0400123 die("Not a valid object name %s", obj_name);
Linus Torvalds11e7d5c2005-05-01 19:28:18 -0700124
Johannes Schindelin7bcf3412016-09-09 12:10:50 +0200125 if (!path)
126 path = obj_context.path;
127 if (obj_context.mode == S_IFINVALID)
128 obj_context.mode = 0100644;
129
H. Peter Anvin79505712005-12-03 17:57:48 -0800130 buf = NULL;
131 switch (opt) {
132 case 't':
Brandon Williams6ca32f42018-02-14 10:59:23 -0800133 oi.type_name = &sb;
Stefan Beller7ecd8692018-04-25 11:20:58 -0700134 if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
Karthik Nayak39e4ae32015-05-03 20:00:01 +0530135 die("git cat-file: could not get object info");
136 if (sb.len) {
137 printf("%s\n", sb.buf);
138 strbuf_release(&sb);
Ævar Arnfjörð Bjarmason27472b52022-07-01 12:42:59 +0200139 ret = 0;
140 goto cleanup;
Linus Torvalds11e7d5c2005-05-01 19:28:18 -0700141 }
H. Peter Anvin79505712005-12-03 17:57:48 -0800142 break;
143
144 case 's':
Karthik Nayak39e4ae32015-05-03 20:00:01 +0530145 oi.sizep = &size;
Siddharth Asthana49050a02022-12-20 11:31:12 +0530146
147 if (use_mailmap) {
148 oi.typep = &type;
149 oi.contentp = (void**)&buf;
150 }
151
Stefan Beller7ecd8692018-04-25 11:20:58 -0700152 if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
Karthik Nayak39e4ae32015-05-03 20:00:01 +0530153 die("git cat-file: could not get object info");
Siddharth Asthana49050a02022-12-20 11:31:12 +0530154
155 if (use_mailmap && (type == OBJ_COMMIT || type == OBJ_TAG)) {
156 size_t s = size;
157 buf = replace_idents_using_mailmap(buf, &s);
158 size = cast_size_t_to_ulong(s);
159 }
160
Torsten Bögershausenca473ce2018-11-11 08:05:04 +0100161 printf("%"PRIuMAX"\n", (uintmax_t)size);
Ævar Arnfjörð Bjarmason27472b52022-07-01 12:42:59 +0200162 ret = 0;
163 goto cleanup;
H. Peter Anvin79505712005-12-03 17:57:48 -0800164
165 case 'e':
Patrick Steinhardtf87c55c2024-06-11 11:19:59 +0200166 ret = !repo_has_object_file(the_repository, &oid);
167 goto cleanup;
H. Peter Anvin79505712005-12-03 17:57:48 -0800168
Johannes Schindelinb9e62f62016-08-24 14:23:39 +0200169 case 'w':
Johannes Schindelinb9e62f62016-08-24 14:23:39 +0200170
Johannes Schindelin7bcf3412016-09-09 12:10:50 +0200171 if (filter_object(path, obj_context.mode,
Ævar Arnfjörð Bjarmason27472b52022-07-01 12:42:59 +0200172 &oid, &buf, &size)) {
173 ret = -1;
174 goto cleanup;
175 }
Johannes Schindelinb9e62f62016-08-24 14:23:39 +0200176 break;
177
Michael J Gruber3ac21612013-05-10 17:10:13 +0200178 case 'c':
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +0200179 if (textconv_object(the_repository, path, obj_context.mode,
180 &oid, 1, &buf, &size))
Michael J Gruber3ac21612013-05-10 17:10:13 +0200181 break;
Jeff King1cf01a32017-09-21 02:25:41 -0400182 /* else fallthrough */
Michael J Gruber3ac21612013-05-10 17:10:13 +0200183
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -0800184 case 'p':
Stefan Beller0df8e962018-04-25 11:20:59 -0700185 type = oid_object_info(the_repository, &oid, NULL);
Nicolas Pitre21666f12007-02-26 14:55:59 -0500186 if (type < 0)
Shawn O. Pearce2b6854c2007-04-21 21:14:39 -0400187 die("Not a valid object name %s", obj_name);
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -0800188
189 /* custom pretty-print here */
Shawn O. Pearce2b6854c2007-04-21 21:14:39 -0400190 if (type == OBJ_TREE) {
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000191 const char *ls_args[3] = { NULL };
192 ls_args[0] = "ls-tree";
193 ls_args[1] = obj_name;
John Cai9b1cb502024-09-13 21:16:14 +0000194 ret = cmd_ls_tree(2, ls_args, NULL, the_repository);
Ævar Arnfjörð Bjarmason27472b52022-07-01 12:42:59 +0200195 goto cleanup;
Shawn O. Pearce2b6854c2007-04-21 21:14:39 -0400196 }
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -0800197
Ævar Arnfjörð Bjarmason27472b52022-07-01 12:42:59 +0200198 if (type == OBJ_BLOB) {
199 ret = stream_blob(&oid);
200 goto cleanup;
201 }
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200202 buf = repo_read_object_file(the_repository, &oid, &type,
203 &size);
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -0800204 if (!buf)
Shawn O. Pearce2b6854c2007-04-21 21:14:39 -0400205 die("Cannot read object %s", obj_name);
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -0800206
Siddharth Asthanaec031da2022-07-19 01:21:02 +0530207 if (use_mailmap) {
208 size_t s = size;
209 buf = replace_idents_using_mailmap(buf, &s);
210 size = cast_size_t_to_ulong(s);
211 }
212
Junio C Hamanoa0f15fa2006-03-01 16:43:19 -0800213 /* otherwise just spit out the data */
214 break;
Clément Poulaine5fba602010-06-15 17:50:28 +0200215
H. Peter Anvin79505712005-12-03 17:57:48 -0800216 case 0:
Ævar Arnfjörð Bjarmason6aea6ba2022-02-05 00:48:34 +0100217 {
218 enum object_type exp_type_id = type_from_string(exp_type);
219
220 if (exp_type_id == OBJ_BLOB) {
brian m. carlson63ecb992016-09-05 20:07:57 +0000221 struct object_id blob_oid;
Stefan Beller0df8e962018-04-25 11:20:59 -0700222 if (oid_object_info(the_repository, &oid, NULL) == OBJ_TAG) {
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200223 char *buffer = repo_read_object_file(the_repository,
224 &oid,
225 &type,
226 &size);
René Scharfee3f1da92014-10-04 20:54:50 +0200227 const char *target;
Johannes Schindelin568459b2024-02-05 14:35:53 +0000228
229 if (!buffer)
230 die(_("unable to read %s"), oid_to_hex(&oid));
231
René Scharfee3f1da92014-10-04 20:54:50 +0200232 if (!skip_prefix(buffer, "object ", &target) ||
Eric W. Biedermand6222a22023-10-01 21:40:27 -0500233 get_oid_hex_algop(target, &blob_oid,
234 &hash_algos[oid.algo]))
brian m. carlson63ecb992016-09-05 20:07:57 +0000235 die("%s not a valid tag", oid_to_hex(&oid));
Nguyễn Thái Ngọc Duy00c8fd42012-03-07 17:54:17 +0700236 free(buffer);
237 } else
brian m. carlson63ecb992016-09-05 20:07:57 +0000238 oidcpy(&blob_oid, &oid);
Nguyễn Thái Ngọc Duy00c8fd42012-03-07 17:54:17 +0700239
Ævar Arnfjörð Bjarmason27472b52022-07-01 12:42:59 +0200240 if (oid_object_info(the_repository, &blob_oid, NULL) == OBJ_BLOB) {
241 ret = stream_blob(&blob_oid);
242 goto cleanup;
243 }
Nguyễn Thái Ngọc Duy00c8fd42012-03-07 17:54:17 +0700244 /*
245 * we attempted to dereference a tag to a blob
246 * and failed; there may be new dereference
247 * mechanisms this code is not aware of.
248 * fall-back to the usual case.
249 */
250 }
Ævar Arnfjörð Bjarmason6aea6ba2022-02-05 00:48:34 +0100251 buf = read_object_with_reference(the_repository, &oid,
252 exp_type_id, &size, NULL);
Siddharth Asthanaec031da2022-07-19 01:21:02 +0530253
254 if (use_mailmap) {
255 size_t s = size;
256 buf = replace_idents_using_mailmap(buf, &s);
257 size = cast_size_t_to_ulong(s);
258 }
H. Peter Anvin79505712005-12-03 17:57:48 -0800259 break;
Ævar Arnfjörð Bjarmason6aea6ba2022-02-05 00:48:34 +0100260 }
H. Peter Anvin79505712005-12-03 17:57:48 -0800261 default:
Alexander Potashevd7530702009-01-04 21:38:41 +0300262 die("git cat-file: unknown option: %s", exp_type);
Linus Torvalds11e7d5c2005-05-01 19:28:18 -0700263 }
264
Petr Baudis2de381f2005-04-13 02:28:48 -0700265 if (!buf)
Heikki Orsila34baebc2008-08-30 14:12:53 +0300266 die("git cat-file %s: bad file", obj_name);
Linus Torvaldsbf0c6e82005-04-08 09:16:38 -0700267
Rene Scharfe7230e6d2006-08-21 20:43:43 +0200268 write_or_die(1, buf, size);
Ævar Arnfjörð Bjarmason27472b52022-07-01 12:42:59 +0200269 ret = 0;
270cleanup:
Johannes Schindelin05c2b7b2017-05-04 15:56:17 +0200271 free(buf);
Patrick Steinhardtf87c55c2024-06-11 11:19:59 +0200272 object_context_release(&obj_context);
Ævar Arnfjörð Bjarmason27472b52022-07-01 12:42:59 +0200273 return ret;
Linus Torvaldse83c5162005-04-07 15:13:13 -0700274}
Adam Roben9cf71b12008-04-23 15:17:44 -0400275
Jeff King93d2a602013-07-10 07:45:47 -0400276struct expand_data {
brian m. carlsoncd4f77b2016-09-05 20:07:56 +0000277 struct object_id oid;
Jeff King93d2a602013-07-10 07:45:47 -0400278 enum object_type type;
279 unsigned long size;
Nguyễn Thái Ngọc Duy166df262016-07-13 17:43:59 +0200280 off_t disk_size;
Jeff King97be0402013-08-02 04:59:07 -0700281 const char *rest;
brian m. carlsoncd4f77b2016-09-05 20:07:56 +0000282 struct object_id delta_base_oid;
Jeff King93d2a602013-07-10 07:45:47 -0400283
284 /*
285 * If mark_query is true, we do not expand anything, but rather
286 * just mark the object_info with items we wish to query.
287 */
288 int mark_query;
289
290 /*
Jeff King97be0402013-08-02 04:59:07 -0700291 * Whether to split the input on whitespace before feeding it to
292 * get_sha1; this is decided during the mark_query phase based on
293 * whether we have a %(rest) token in our format.
294 */
295 int split_on_whitespace;
296
297 /*
Jeff King93d2a602013-07-10 07:45:47 -0400298 * After a mark_query run, this object_info is set up to be
Jeff Kingc93206b2019-01-07 03:34:12 -0500299 * passed to oid_object_info_extended. It will point to the data
Jeff King93d2a602013-07-10 07:45:47 -0400300 * elements above, so you can retrieve the response from there.
301 */
302 struct object_info info;
Jeff King845de332016-05-18 12:55:23 -0400303
304 /*
305 * This flag will be true if the requested batch format and options
Jeff Kingc93206b2019-01-07 03:34:12 -0500306 * don't require us to call oid_object_info, which can then be
Jeff King845de332016-05-18 12:55:23 -0400307 * optimized out.
308 */
309 unsigned skip_object_info : 1;
Jeff King93d2a602013-07-10 07:45:47 -0400310};
311
312static int is_atom(const char *atom, const char *s, int slen)
313{
314 int alen = strlen(atom);
315 return alen == slen && !memcmp(atom, s, alen);
316}
317
René Scharfe7c43bdf2024-03-24 12:21:15 +0100318static int expand_atom(struct strbuf *sb, const char *atom, int len,
319 struct expand_data *data)
Jeff King93d2a602013-07-10 07:45:47 -0400320{
Jeff King93d2a602013-07-10 07:45:47 -0400321 if (is_atom("objectname", atom, len)) {
322 if (!data->mark_query)
brian m. carlsoncd4f77b2016-09-05 20:07:56 +0000323 strbuf_addstr(sb, oid_to_hex(&data->oid));
Jeff King93d2a602013-07-10 07:45:47 -0400324 } else if (is_atom("objecttype", atom, len)) {
Jeff King5b086402013-07-12 02:34:57 -0400325 if (data->mark_query)
326 data->info.typep = &data->type;
327 else
Brandon Williamsdebca9d2018-02-14 10:59:24 -0800328 strbuf_addstr(sb, type_name(data->type));
Jeff King93d2a602013-07-10 07:45:47 -0400329 } else if (is_atom("objectsize", atom, len)) {
330 if (data->mark_query)
331 data->info.sizep = &data->size;
332 else
Torsten Bögershausenca473ce2018-11-11 08:05:04 +0100333 strbuf_addf(sb, "%"PRIuMAX , (uintmax_t)data->size);
Jeff Kinga4ac1062013-07-10 07:46:25 -0400334 } else if (is_atom("objectsize:disk", atom, len)) {
335 if (data->mark_query)
336 data->info.disk_sizep = &data->disk_size;
337 else
Nguyễn Thái Ngọc Duy166df262016-07-13 17:43:59 +0200338 strbuf_addf(sb, "%"PRIuMAX, (uintmax_t)data->disk_size);
Jeff King97be0402013-08-02 04:59:07 -0700339 } else if (is_atom("rest", atom, len)) {
340 if (data->mark_query)
341 data->split_on_whitespace = 1;
342 else if (data->rest)
343 strbuf_addstr(sb, data->rest);
Jeff King65ea9c32013-12-21 09:25:22 -0500344 } else if (is_atom("deltabase", atom, len)) {
345 if (data->mark_query)
Jeff Kingb99b6bc2020-02-23 23:36:56 -0500346 data->info.delta_base_oid = &data->delta_base_oid;
Jeff King65ea9c32013-12-21 09:25:22 -0500347 else
brian m. carlsoncd4f77b2016-09-05 20:07:56 +0000348 strbuf_addstr(sb,
349 oid_to_hex(&data->delta_base_oid));
Jeff King93d2a602013-07-10 07:45:47 -0400350 } else
René Scharfe7c43bdf2024-03-24 12:21:15 +0100351 return 0;
352 return 1;
Jeff King93d2a602013-07-10 07:45:47 -0400353}
354
René Scharfe6f1e2d52023-06-17 22:43:17 +0200355static void expand_format(struct strbuf *sb, const char *start,
356 struct expand_data *data)
Jeff King93d2a602013-07-10 07:45:47 -0400357{
René Scharfe6f1e2d52023-06-17 22:43:17 +0200358 while (strbuf_expand_step(sb, &start)) {
359 const char *end;
Jeff King93d2a602013-07-10 07:45:47 -0400360
René Scharfe6f1e2d52023-06-17 22:43:17 +0200361 if (skip_prefix(start, "%", &start) || *start != '(')
362 strbuf_addch(sb, '%');
René Scharfe7c43bdf2024-03-24 12:21:15 +0100363 else if ((end = strchr(start + 1, ')')) &&
364 expand_atom(sb, start + 1, end - start - 1, data))
René Scharfe6f1e2d52023-06-17 22:43:17 +0200365 start = end + 1;
René Scharfe7c43bdf2024-03-24 12:21:15 +0100366 else
367 strbuf_expand_bad_format(start, "cat-file");
René Scharfe6f1e2d52023-06-17 22:43:17 +0200368 }
Jeff King93d2a602013-07-10 07:45:47 -0400369}
370
Jeff Kingfc4937c2015-06-22 06:45:17 -0400371static void batch_write(struct batch_options *opt, const void *data, int len)
372{
373 if (opt->buffer_output) {
374 if (fwrite(data, 1, len, stdout) != len)
375 die_errno("unable to write to stdout");
376 } else
377 write_or_die(1, data, len);
378}
379
380static void print_object_or_die(struct batch_options *opt, struct expand_data *data)
Jeff King98e20922013-07-10 07:38:24 -0400381{
brian m. carlson63ecb992016-09-05 20:07:57 +0000382 const struct object_id *oid = &data->oid;
Jeff King370c9262013-12-12 07:01:42 +0800383
Jeff King6554dfa2013-12-12 07:15:50 +0800384 assert(data->info.typep);
385
Jeff King370c9262013-12-12 07:01:42 +0800386 if (data->type == OBJ_BLOB) {
Jeff Kingfc4937c2015-06-22 06:45:17 -0400387 if (opt->buffer_output)
388 fflush(stdout);
John Caia2c75522022-02-18 18:23:11 +0000389 if (opt->transform_mode) {
Johannes Schindelin32145942016-09-09 12:10:54 +0200390 char *contents;
391 unsigned long size;
392
393 if (!data->rest)
Junio C Hamano7889ed22016-09-21 15:15:18 -0700394 die("missing path for '%s'", oid_to_hex(oid));
Johannes Schindelin32145942016-09-09 12:10:54 +0200395
John Caia2c75522022-02-18 18:23:11 +0000396 if (opt->transform_mode == 'w') {
Junio C Hamano7889ed22016-09-21 15:15:18 -0700397 if (filter_object(data->rest, 0100644, oid,
Johannes Schindelin32145942016-09-09 12:10:54 +0200398 &contents, &size))
399 die("could not convert '%s' %s",
Junio C Hamano7889ed22016-09-21 15:15:18 -0700400 oid_to_hex(oid), data->rest);
John Caia2c75522022-02-18 18:23:11 +0000401 } else if (opt->transform_mode == 'c') {
Johannes Schindelin32145942016-09-09 12:10:54 +0200402 enum object_type type;
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +0200403 if (!textconv_object(the_repository,
404 data->rest, 0100644, oid,
Johannes Schindelin32145942016-09-09 12:10:54 +0200405 1, &contents, &size))
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200406 contents = repo_read_object_file(the_repository,
407 oid,
408 &type,
409 &size);
Johannes Schindelin32145942016-09-09 12:10:54 +0200410 if (!contents)
411 die("could not convert '%s' %s",
Junio C Hamano7889ed22016-09-21 15:15:18 -0700412 oid_to_hex(oid), data->rest);
Johannes Schindelin32145942016-09-09 12:10:54 +0200413 } else
John Caia2c75522022-02-18 18:23:11 +0000414 BUG("invalid transform_mode: %c", opt->transform_mode);
Johannes Schindelin32145942016-09-09 12:10:54 +0200415 batch_write(opt, contents, size);
416 free(contents);
Jeff King98f425b2018-10-30 19:23:38 -0400417 } else {
418 stream_blob(oid);
419 }
Jeff King98e20922013-07-10 07:38:24 -0400420 }
421 else {
Jeff King370c9262013-12-12 07:01:42 +0800422 enum object_type type;
423 unsigned long size;
Jeff King98e20922013-07-10 07:38:24 -0400424 void *contents;
425
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200426 contents = repo_read_object_file(the_repository, oid, &type,
427 &size);
Johannes Schindelin568459b2024-02-05 14:35:53 +0000428 if (!contents)
429 die("object %s disappeared", oid_to_hex(oid));
Siddharth Asthanaec031da2022-07-19 01:21:02 +0530430
431 if (use_mailmap) {
432 size_t s = size;
433 contents = replace_idents_using_mailmap(contents, &s);
434 size = cast_size_t_to_ulong(s);
435 }
436
Jeff King370c9262013-12-12 07:01:42 +0800437 if (type != data->type)
brian m. carlson63ecb992016-09-05 20:07:57 +0000438 die("object %s changed type!?", oid_to_hex(oid));
Siddharth Asthanaec031da2022-07-19 01:21:02 +0530439 if (data->info.sizep && size != data->size && !use_mailmap)
brian m. carlson63ecb992016-09-05 20:07:57 +0000440 die("object %s changed size!?", oid_to_hex(oid));
Jeff King98e20922013-07-10 07:38:24 -0400441
Jeff Kingfc4937c2015-06-22 06:45:17 -0400442 batch_write(opt, contents, size);
Jeff King98e20922013-07-10 07:38:24 -0400443 free(contents);
444 }
445}
446
Patrick Steinhardtf79e1882023-06-06 07:19:45 +0200447static void print_default_format(struct strbuf *scratch, struct expand_data *data,
448 struct batch_options *opt)
John Caieb54a332022-03-15 02:40:36 +0000449{
Patrick Steinhardtf79e1882023-06-06 07:19:45 +0200450 strbuf_addf(scratch, "%s %s %"PRIuMAX"%c", oid_to_hex(&data->oid),
John Caieb54a332022-03-15 02:40:36 +0000451 type_name(data->type),
Patrick Steinhardtf79e1882023-06-06 07:19:45 +0200452 (uintmax_t)data->size, opt->output_delim);
John Caieb54a332022-03-15 02:40:36 +0000453}
454
Jeff Kingbf972892021-10-05 16:38:04 -0400455/*
456 * If "pack" is non-NULL, then "offset" is the byte offset within the pack from
457 * which the object may be accessed (though note that we may also rely on
458 * data->oid, too). If "pack" is NULL, then offset is ignored.
459 */
Jeff King79ed0a52018-08-14 14:20:22 -0400460static void batch_object_write(const char *obj_name,
461 struct strbuf *scratch,
462 struct batch_options *opt,
Jeff Kingbf972892021-10-05 16:38:04 -0400463 struct expand_data *data,
464 struct packed_git *pack,
465 off_t offset)
Jeff King44b877e2015-06-22 06:45:41 -0400466{
Jeff Kingbf972892021-10-05 16:38:04 -0400467 if (!data->skip_object_info) {
468 int ret;
469
Siddharth Asthanaa797c0e2022-12-20 11:31:13 +0530470 if (use_mailmap)
471 data->info.typep = &data->type;
472
Jeff Kingbf972892021-10-05 16:38:04 -0400473 if (pack)
474 ret = packed_object_info(the_repository, pack, offset,
475 &data->info);
476 else
477 ret = oid_object_info_extended(the_repository,
478 &data->oid, &data->info,
479 OBJECT_INFO_LOOKUP_REPLACE);
480 if (ret < 0) {
Patrick Steinhardtf79e1882023-06-06 07:19:45 +0200481 printf("%s missing%c",
482 obj_name ? obj_name : oid_to_hex(&data->oid), opt->output_delim);
Jeff Kingbf972892021-10-05 16:38:04 -0400483 fflush(stdout);
484 return;
485 }
Siddharth Asthanaa797c0e2022-12-20 11:31:13 +0530486
487 if (use_mailmap && (data->type == OBJ_COMMIT || data->type == OBJ_TAG)) {
488 size_t s = data->size;
489 char *buf = NULL;
490
491 buf = repo_read_object_file(the_repository, &data->oid, &data->type,
492 &data->size);
Johannes Schindelin568459b2024-02-05 14:35:53 +0000493 if (!buf)
494 die(_("unable to read %s"), oid_to_hex(&data->oid));
Siddharth Asthanaa797c0e2022-12-20 11:31:13 +0530495 buf = replace_idents_using_mailmap(buf, &s);
496 data->size = cast_size_t_to_ulong(s);
497
498 free(buf);
499 }
Jeff King44b877e2015-06-22 06:45:41 -0400500 }
501
Jeff King79ed0a52018-08-14 14:20:22 -0400502 strbuf_reset(scratch);
John Caieb54a332022-03-15 02:40:36 +0000503
504 if (!opt->format) {
Patrick Steinhardtf79e1882023-06-06 07:19:45 +0200505 print_default_format(scratch, data, opt);
John Caieb54a332022-03-15 02:40:36 +0000506 } else {
René Scharfe6f1e2d52023-06-17 22:43:17 +0200507 expand_format(scratch, opt->format, data);
Patrick Steinhardtf79e1882023-06-06 07:19:45 +0200508 strbuf_addch(scratch, opt->output_delim);
John Caieb54a332022-03-15 02:40:36 +0000509 }
510
Jeff King79ed0a52018-08-14 14:20:22 -0400511 batch_write(opt, scratch->buf, scratch->len);
Jeff King44b877e2015-06-22 06:45:41 -0400512
John Caiac4e58c2022-02-18 18:23:12 +0000513 if (opt->batch_mode == BATCH_MODE_CONTENTS) {
Jeff King44b877e2015-06-22 06:45:41 -0400514 print_object_or_die(opt, data);
Patrick Steinhardtf79e1882023-06-06 07:19:45 +0200515 batch_write(opt, &opt->output_delim, 1);
Jeff King44b877e2015-06-22 06:45:41 -0400516 }
517}
518
Jeff King79ed0a52018-08-14 14:20:22 -0400519static void batch_one_object(const char *obj_name,
520 struct strbuf *scratch,
521 struct batch_options *opt,
Jeff King82330952015-06-22 06:45:33 -0400522 struct expand_data *data)
Adam Roben05d56672008-04-23 15:17:46 -0400523{
Patrick Steinhardtf87c55c2024-06-11 11:19:59 +0200524 struct object_context ctx = {0};
Eric W. Biedermand6222a22023-10-01 21:40:27 -0500525 int flags =
526 GET_OID_HASH_ANY |
527 (opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0);
David Turnerd1dd94b2019-01-17 23:19:43 -0500528 enum get_oid_result result;
Adam Roben05d56672008-04-23 15:17:46 -0400529
Nguyễn Thái Ngọc Duy3a7a6982019-01-12 09:13:28 +0700530 result = get_oid_with_context(the_repository, obj_name,
531 flags, &data->oid, &ctx);
David Turner122d5342015-05-20 13:03:40 -0400532 if (result != FOUND) {
533 switch (result) {
534 case MISSING_OBJECT:
Patrick Steinhardtf79e1882023-06-06 07:19:45 +0200535 printf("%s missing%c", obj_name, opt->output_delim);
David Turner122d5342015-05-20 13:03:40 -0400536 break;
David Turnerd1dd94b2019-01-17 23:19:43 -0500537 case SHORT_NAME_AMBIGUOUS:
Patrick Steinhardtf79e1882023-06-06 07:19:45 +0200538 printf("%s ambiguous%c", obj_name, opt->output_delim);
David Turnerd1dd94b2019-01-17 23:19:43 -0500539 break;
David Turner122d5342015-05-20 13:03:40 -0400540 case DANGLING_SYMLINK:
Patrick Steinhardtf79e1882023-06-06 07:19:45 +0200541 printf("dangling %"PRIuMAX"%c%s%c",
542 (uintmax_t)strlen(obj_name),
543 opt->output_delim, obj_name, opt->output_delim);
David Turner122d5342015-05-20 13:03:40 -0400544 break;
545 case SYMLINK_LOOP:
Patrick Steinhardtf79e1882023-06-06 07:19:45 +0200546 printf("loop %"PRIuMAX"%c%s%c",
547 (uintmax_t)strlen(obj_name),
548 opt->output_delim, obj_name, opt->output_delim);
David Turner122d5342015-05-20 13:03:40 -0400549 break;
550 case NOT_DIR:
Patrick Steinhardtf79e1882023-06-06 07:19:45 +0200551 printf("notdir %"PRIuMAX"%c%s%c",
552 (uintmax_t)strlen(obj_name),
553 opt->output_delim, obj_name, opt->output_delim);
David Turner122d5342015-05-20 13:03:40 -0400554 break;
555 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +0200556 BUG("unknown get_sha1_with_context result %d\n",
David Turner122d5342015-05-20 13:03:40 -0400557 result);
558 break;
559 }
560 fflush(stdout);
Patrick Steinhardtf87c55c2024-06-11 11:19:59 +0200561
562 goto out;
David Turner122d5342015-05-20 13:03:40 -0400563 }
564
565 if (ctx.mode == 0) {
Patrick Steinhardtf79e1882023-06-06 07:19:45 +0200566 printf("symlink %"PRIuMAX"%c%s%c",
David Turner122d5342015-05-20 13:03:40 -0400567 (uintmax_t)ctx.symlink_path.len,
Patrick Steinhardtf79e1882023-06-06 07:19:45 +0200568 opt->output_delim, ctx.symlink_path.buf, opt->output_delim);
Lea Wiemann422b2062008-06-03 20:34:17 +0200569 fflush(stdout);
Patrick Steinhardtf87c55c2024-06-11 11:19:59 +0200570 goto out;
Adam Roben05d56672008-04-23 15:17:46 -0400571 }
572
Jeff Kingbf972892021-10-05 16:38:04 -0400573 batch_object_write(obj_name, scratch, opt, data, NULL, 0);
Patrick Steinhardtf87c55c2024-06-11 11:19:59 +0200574
575out:
576 object_context_release(&ctx);
Adam Roben05d56672008-04-23 15:17:46 -0400577}
578
Jeff King6a951932015-06-22 06:45:59 -0400579struct object_cb_data {
580 struct batch_options *opt;
581 struct expand_data *expand;
Jeff King0750bb52018-08-10 19:24:57 -0400582 struct oidset *seen;
Jeff King79ed0a52018-08-14 14:20:22 -0400583 struct strbuf *scratch;
Jeff King6a951932015-06-22 06:45:59 -0400584};
585
brian m. carlson1b7ba792017-03-31 01:39:59 +0000586static int batch_object_cb(const struct object_id *oid, void *vdata)
Jeff King6a951932015-06-22 06:45:59 -0400587{
Jeff King3115ee42015-06-22 07:06:32 -0400588 struct object_cb_data *data = vdata;
brian m. carlson1b7ba792017-03-31 01:39:59 +0000589 oidcpy(&data->expand->oid, oid);
Jeff Kingbf972892021-10-05 16:38:04 -0400590 batch_object_write(NULL, data->scratch, data->opt, data->expand,
591 NULL, 0);
Jeff King16ddcd42016-09-26 08:00:29 -0400592 return 0;
Jeff King6a951932015-06-22 06:45:59 -0400593}
594
Jeff Kingb1adb382018-08-10 19:17:14 -0400595static int collect_loose_object(const struct object_id *oid,
Jeff Kingbe252d32023-02-24 01:39:24 -0500596 const char *path UNUSED,
Jeff Kingb1adb382018-08-10 19:17:14 -0400597 void *data)
Jeff King6a951932015-06-22 06:45:59 -0400598{
brian m. carlson910650d2017-03-31 01:40:00 +0000599 oid_array_append(data, oid);
Jeff King3115ee42015-06-22 07:06:32 -0400600 return 0;
Jeff King6a951932015-06-22 06:45:59 -0400601}
602
Jeff Kingb1adb382018-08-10 19:17:14 -0400603static int collect_packed_object(const struct object_id *oid,
Jeff Kingbe252d32023-02-24 01:39:24 -0500604 struct packed_git *pack UNUSED,
605 uint32_t pos UNUSED,
Jeff Kingb1adb382018-08-10 19:17:14 -0400606 void *data)
Jeff King6a951932015-06-22 06:45:59 -0400607{
brian m. carlson910650d2017-03-31 01:40:00 +0000608 oid_array_append(data, oid);
Jeff King3115ee42015-06-22 07:06:32 -0400609 return 0;
Jeff King6a951932015-06-22 06:45:59 -0400610}
611
Jeff Kingbf972892021-10-05 16:38:04 -0400612static int batch_unordered_object(const struct object_id *oid,
613 struct packed_git *pack, off_t offset,
614 void *vdata)
Jeff King0750bb52018-08-10 19:24:57 -0400615{
616 struct object_cb_data *data = vdata;
617
Jeff Kingced9fff2018-08-14 14:14:27 -0400618 if (oidset_insert(data->seen, oid))
Jeff King0750bb52018-08-10 19:24:57 -0400619 return 0;
Jeff King0750bb52018-08-10 19:24:57 -0400620
Jeff King818e3932021-10-05 16:36:17 -0400621 oidcpy(&data->expand->oid, oid);
Jeff Kingbf972892021-10-05 16:38:04 -0400622 batch_object_write(NULL, data->scratch, data->opt, data->expand,
623 pack, offset);
Jeff King818e3932021-10-05 16:36:17 -0400624 return 0;
Jeff King0750bb52018-08-10 19:24:57 -0400625}
626
627static int batch_unordered_loose(const struct object_id *oid,
Jeff Kingbe252d32023-02-24 01:39:24 -0500628 const char *path UNUSED,
Jeff King0750bb52018-08-10 19:24:57 -0400629 void *data)
630{
Jeff Kingbf972892021-10-05 16:38:04 -0400631 return batch_unordered_object(oid, NULL, 0, data);
Jeff King0750bb52018-08-10 19:24:57 -0400632}
633
634static int batch_unordered_packed(const struct object_id *oid,
635 struct packed_git *pack,
636 uint32_t pos,
637 void *data)
638{
Jeff Kingbf972892021-10-05 16:38:04 -0400639 return batch_unordered_object(oid, pack,
640 nth_packed_object_offset(pack, pos),
641 data);
Jeff King0750bb52018-08-10 19:24:57 -0400642}
643
John Cai440c7052022-02-18 18:23:14 +0000644typedef void (*parse_cmd_fn_t)(struct batch_options *, const char *,
645 struct strbuf *, struct expand_data *);
646
647struct queued_cmd {
648 parse_cmd_fn_t fn;
649 char *line;
650};
651
652static void parse_cmd_contents(struct batch_options *opt,
653 const char *line,
654 struct strbuf *output,
655 struct expand_data *data)
656{
657 opt->batch_mode = BATCH_MODE_CONTENTS;
658 batch_one_object(line, output, opt, data);
659}
660
661static void parse_cmd_info(struct batch_options *opt,
662 const char *line,
663 struct strbuf *output,
664 struct expand_data *data)
665{
666 opt->batch_mode = BATCH_MODE_INFO;
667 batch_one_object(line, output, opt, data);
668}
669
670static void dispatch_calls(struct batch_options *opt,
671 struct strbuf *output,
672 struct expand_data *data,
673 struct queued_cmd *cmd,
674 int nr)
675{
676 int i;
677
678 if (!opt->buffer_output)
679 die(_("flush is only for --buffer mode"));
680
681 for (i = 0; i < nr; i++)
682 cmd[i].fn(opt, cmd[i].line, output, data);
683
684 fflush(stdout);
685}
686
687static void free_cmds(struct queued_cmd *cmd, size_t *nr)
688{
689 size_t i;
690
691 for (i = 0; i < *nr; i++)
692 FREE_AND_NULL(cmd[i].line);
693
694 *nr = 0;
695}
696
697
698static const struct parse_cmd {
699 const char *name;
700 parse_cmd_fn_t fn;
701 unsigned takes_args;
702} commands[] = {
703 { "contents", parse_cmd_contents, 1},
704 { "info", parse_cmd_info, 1},
705 { "flush", NULL, 0},
706};
707
708static void batch_objects_command(struct batch_options *opt,
709 struct strbuf *output,
710 struct expand_data *data)
711{
712 struct strbuf input = STRBUF_INIT;
713 struct queued_cmd *queued_cmd = NULL;
714 size_t alloc = 0, nr = 0;
715
Patrick Steinhardt3217f522023-06-06 07:19:41 +0200716 while (strbuf_getdelim_strip_crlf(&input, stdin, opt->input_delim) != EOF) {
717 int i;
John Cai440c7052022-02-18 18:23:14 +0000718 const struct parse_cmd *cmd = NULL;
719 const char *p = NULL, *cmd_end;
720 struct queued_cmd call = {0};
721
722 if (!input.len)
723 die(_("empty command in input"));
724 if (isspace(*input.buf))
725 die(_("whitespace before command: '%s'"), input.buf);
726
727 for (i = 0; i < ARRAY_SIZE(commands); i++) {
728 if (!skip_prefix(input.buf, commands[i].name, &cmd_end))
729 continue;
730
731 cmd = &commands[i];
732 if (cmd->takes_args) {
733 if (*cmd_end != ' ')
734 die(_("%s requires arguments"),
735 commands[i].name);
736
737 p = cmd_end + 1;
738 } else if (*cmd_end) {
739 die(_("%s takes no arguments"),
740 commands[i].name);
741 }
742
743 break;
744 }
745
746 if (!cmd)
747 die(_("unknown command: '%s'"), input.buf);
748
749 if (!strcmp(cmd->name, "flush")) {
750 dispatch_calls(opt, output, data, queued_cmd, nr);
751 free_cmds(queued_cmd, &nr);
752 } else if (!opt->buffer_output) {
753 cmd->fn(opt, p, output, data);
754 } else {
755 ALLOC_GROW(queued_cmd, nr + 1, alloc);
756 call.fn = cmd->fn;
757 call.line = xstrdup_or_null(p);
758 queued_cmd[nr++] = call;
759 }
760 }
761
762 if (opt->buffer_output &&
763 nr &&
764 !git_env_bool("GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT", 0)) {
765 dispatch_calls(opt, output, data, queued_cmd, nr);
766 free_cmds(queued_cmd, &nr);
767 }
768
Ævar Arnfjörð Bjarmasond90dafb2022-07-01 12:42:54 +0200769 free_cmds(queued_cmd, &nr);
John Cai440c7052022-02-18 18:23:14 +0000770 free(queued_cmd);
771 strbuf_release(&input);
772}
773
John Caieb54a332022-03-15 02:40:36 +0000774#define DEFAULT_FORMAT "%(objectname) %(objecttype) %(objectsize)"
775
Jeff Kingb71bd482013-07-10 07:38:58 -0400776static int batch_objects(struct batch_options *opt)
Adam Roben05d56672008-04-23 15:17:46 -0400777{
Jeff King54d2f0d2018-08-14 14:18:06 -0400778 struct strbuf input = STRBUF_INIT;
779 struct strbuf output = STRBUF_INIT;
Jeff King93d2a602013-07-10 07:45:47 -0400780 struct expand_data data;
Jeff Kinga42fcd12014-03-12 16:05:43 -0400781 int save_warning;
Jeff King07e23832014-01-07 17:10:15 -0500782 int retval = 0;
Jeff King93d2a602013-07-10 07:45:47 -0400783
Jeff King93d2a602013-07-10 07:45:47 -0400784 /*
785 * Expand once with our special mark_query flag, which will prime the
Jeff Kingc93206b2019-01-07 03:34:12 -0500786 * object_info to be handed to oid_object_info_extended for each
Jeff King93d2a602013-07-10 07:45:47 -0400787 * object.
788 */
789 memset(&data, 0, sizeof(data));
790 data.mark_query = 1;
René Scharfe6f1e2d52023-06-17 22:43:17 +0200791 expand_format(&output,
John Caieb54a332022-03-15 02:40:36 +0000792 opt->format ? opt->format : DEFAULT_FORMAT,
John Caieb54a332022-03-15 02:40:36 +0000793 &data);
Jeff King93d2a602013-07-10 07:45:47 -0400794 data.mark_query = 0;
Jeff King54d2f0d2018-08-14 14:18:06 -0400795 strbuf_release(&output);
John Caia2c75522022-02-18 18:23:11 +0000796 if (opt->transform_mode)
Johannes Schindelin32145942016-09-09 12:10:54 +0200797 data.split_on_whitespace = 1;
Adam Roben05d56672008-04-23 15:17:46 -0400798
John Caieb54a332022-03-15 02:40:36 +0000799 if (opt->format && !strcmp(opt->format, DEFAULT_FORMAT))
800 opt->format = NULL;
Jeff King25fba782013-07-12 02:20:05 -0400801 /*
Jeff King6554dfa2013-12-12 07:15:50 +0800802 * If we are printing out the object, then always fill in the type,
803 * since we will want to decide whether or not to stream.
804 */
John Caiac4e58c2022-02-18 18:23:12 +0000805 if (opt->batch_mode == BATCH_MODE_CONTENTS)
Jeff King6554dfa2013-12-12 07:15:50 +0800806 data.info.typep = &data.type;
807
Jeff King6a951932015-06-22 06:45:59 -0400808 if (opt->all_objects) {
ZheNing Huee02ac62021-06-03 16:29:26 +0000809 struct object_cb_data cb;
ZheNing Hue16acc82021-06-03 16:29:25 +0000810 struct object_info empty = OBJECT_INFO_INIT;
811
812 if (!memcmp(&data.info, &empty, sizeof(empty)))
813 data.skip_object_info = 1;
Jeff King3115ee42015-06-22 07:06:32 -0400814
Ævar Arnfjörð Bjarmasona5183d72023-03-28 15:58:53 +0200815 if (repo_has_promisor_remote(the_repository))
Christian Couderb14ed5a2019-06-25 15:40:31 +0200816 warning("This repository uses promisor remotes. Some objects may not be loaded.");
Jeff King3115ee42015-06-22 07:06:32 -0400817
Derrick Stoleed24eda42023-06-06 13:24:35 +0000818 disable_replace_refs();
Jeff King5c5b29b2021-10-05 16:36:07 -0400819
Jeff King6a951932015-06-22 06:45:59 -0400820 cb.opt = opt;
821 cb.expand = &data;
Jeff King79ed0a52018-08-14 14:20:22 -0400822 cb.scratch = &output;
Jeff King3115ee42015-06-22 07:06:32 -0400823
Jeff King0750bb52018-08-10 19:24:57 -0400824 if (opt->unordered) {
825 struct oidset seen = OIDSET_INIT;
826
827 cb.seen = &seen;
828
829 for_each_loose_object(batch_unordered_loose, &cb, 0);
830 for_each_packed_object(batch_unordered_packed, &cb,
831 FOR_EACH_OBJECT_PACK_ORDER);
832
833 oidset_clear(&seen);
834 } else {
835 struct oid_array sa = OID_ARRAY_INIT;
836
837 for_each_loose_object(collect_loose_object, &sa, 0);
838 for_each_packed_object(collect_packed_object, &sa, 0);
839
840 oid_array_for_each_unique(&sa, batch_object_cb, &cb);
841
842 oid_array_clear(&sa);
843 }
844
Jeff King79ed0a52018-08-14 14:20:22 -0400845 strbuf_release(&output);
Jeff King6a951932015-06-22 06:45:59 -0400846 return 0;
847 }
848
Jeff King6554dfa2013-12-12 07:15:50 +0800849 /*
Jeff King25fba782013-07-12 02:20:05 -0400850 * We are going to call get_sha1 on a potentially very large number of
851 * objects. In most large cases, these will be actual object sha1s. The
852 * cost to double-check that each one is not also a ref (just so we can
853 * warn) ends up dwarfing the actual cost of the object lookups
854 * themselves. We can work around it by just turning off the warning.
855 */
Jeff Kinga42fcd12014-03-12 16:05:43 -0400856 save_warning = warn_on_object_refname_ambiguity;
Jeff King25fba782013-07-12 02:20:05 -0400857 warn_on_object_refname_ambiguity = 0;
858
John Cai440c7052022-02-18 18:23:14 +0000859 if (opt->batch_mode == BATCH_MODE_QUEUE_AND_DISPATCH) {
860 batch_objects_command(opt, &output, &data);
861 goto cleanup;
862 }
863
Patrick Steinhardt3217f522023-06-06 07:19:41 +0200864 while (strbuf_getdelim_strip_crlf(&input, stdin, opt->input_delim) != EOF) {
Jeff King97be0402013-08-02 04:59:07 -0700865 if (data.split_on_whitespace) {
866 /*
867 * Split at first whitespace, tying off the beginning
868 * of the string and saving the remainder (or NULL) in
869 * data.rest.
870 */
Jeff King54d2f0d2018-08-14 14:18:06 -0400871 char *p = strpbrk(input.buf, " \t");
Jeff King97be0402013-08-02 04:59:07 -0700872 if (p) {
873 while (*p && strchr(" \t", *p))
874 *p++ = '\0';
875 }
876 data.rest = p;
877 }
878
Jeff King79ed0a52018-08-14 14:20:22 -0400879 batch_one_object(input.buf, &output, opt, &data);
Adam Roben05d56672008-04-23 15:17:46 -0400880 }
881
John Cai440c7052022-02-18 18:23:14 +0000882 cleanup:
Jeff King54d2f0d2018-08-14 14:18:06 -0400883 strbuf_release(&input);
Jeff King79ed0a52018-08-14 14:20:22 -0400884 strbuf_release(&output);
Jeff Kinga42fcd12014-03-12 16:05:43 -0400885 warn_on_object_refname_ambiguity = save_warning;
Jeff King07e23832014-01-07 17:10:15 -0500886 return retval;
Adam Roben05d56672008-04-23 15:17:46 -0400887}
888
Glen Chooa4e7e312023-06-28 19:26:22 +0000889static int git_cat_file_config(const char *var, const char *value,
890 const struct config_context *ctx, void *cb)
Clément Poulaine5fba602010-06-15 17:50:28 +0200891{
Jeff King6680a082012-02-07 13:23:02 -0500892 if (userdiff_config(var, value) < 0)
Clément Poulaine5fba602010-06-15 17:50:28 +0200893 return -1;
Clément Poulaine5fba602010-06-15 17:50:28 +0200894
Glen Chooa4e7e312023-06-28 19:26:22 +0000895 return git_default_config(var, value, ctx, cb);
Clément Poulaine5fba602010-06-15 17:50:28 +0200896}
897
Jeff Kingb71bd482013-07-10 07:38:58 -0400898static int batch_option_callback(const struct option *opt,
899 const char *arg,
900 int unset)
901{
902 struct batch_options *bo = opt->value;
903
Jeff King517fe802018-11-05 01:45:42 -0500904 BUG_ON_OPT_NEG(unset);
905
David Turner122d5342015-05-20 13:03:40 -0400906 if (bo->enabled) {
Jeff King0eb8d372018-11-05 01:43:44 -0500907 return error(_("only one batch option may be specified"));
Jeff Kingb71bd482013-07-10 07:38:58 -0400908 }
909
910 bo->enabled = 1;
John Caiac4e58c2022-02-18 18:23:12 +0000911
912 if (!strcmp(opt->long_name, "batch"))
913 bo->batch_mode = BATCH_MODE_CONTENTS;
914 else if (!strcmp(opt->long_name, "batch-check"))
915 bo->batch_mode = BATCH_MODE_INFO;
John Cai440c7052022-02-18 18:23:14 +0000916 else if (!strcmp(opt->long_name, "batch-command"))
917 bo->batch_mode = BATCH_MODE_QUEUE_AND_DISPATCH;
John Caiac4e58c2022-02-18 18:23:12 +0000918 else
919 BUG("%s given to batch-option-callback", opt->long_name);
920
Jeff King93d2a602013-07-10 07:45:47 -0400921 bo->format = arg;
Jeff Kingb71bd482013-07-10 07:38:58 -0400922
923 return 0;
924}
925
John Cai9b1cb502024-09-13 21:16:14 +0000926int cmd_cat_file(int argc,
927 const char **argv,
928 const char *prefix,
929 struct repository *repo UNUSED)
Adam Roben9cf71b12008-04-23 15:17:44 -0400930{
Jeff Kingb71bd482013-07-10 07:38:58 -0400931 int opt = 0;
Ævar Arnfjörð Bjarmasonb3fe4682021-12-28 14:28:47 +0100932 int opt_cw = 0;
933 int opt_epts = 0;
Adam Roben4814dbe2008-04-23 15:17:45 -0400934 const char *exp_type = NULL, *obj_name = NULL;
Jeff Kingb71bd482013-07-10 07:38:58 -0400935 struct batch_options batch = {0};
Karthik Nayak39e4ae32015-05-03 20:00:01 +0530936 int unknown_type = 0;
Patrick Steinhardt3217f522023-06-06 07:19:41 +0200937 int input_nul_terminated = 0;
Patrick Steinhardtf79e1882023-06-06 07:19:45 +0200938 int nul_terminated = 0;
Adam Roben9cf71b12008-04-23 15:17:44 -0400939
Ævar Arnfjörð Bjarmason5a404172021-12-28 14:28:45 +0100940 const char * const usage[] = {
941 N_("git cat-file <type> <object>"),
942 N_("git cat-file (-e | -p) <object>"),
Ævar Arnfjörð Bjarmason83dc4432022-01-10 23:08:45 +0100943 N_("git cat-file (-t | -s) [--allow-unknown-type] <object>"),
Štěpán Němeccebfaaa2023-10-09 19:56:04 +0200944 N_("git cat-file (--textconv | --filters)\n"
945 " [<rev>:<path|tree-ish> | --path=<path|tree-ish> <rev>]"),
John Cai440c7052022-02-18 18:23:14 +0000946 N_("git cat-file (--batch | --batch-check | --batch-command) [--batch-all-objects]\n"
Ævar Arnfjörð Bjarmason5a404172021-12-28 14:28:45 +0100947 " [--buffer] [--follow-symlinks] [--unordered]\n"
Patrick Steinhardtf79e1882023-06-06 07:19:45 +0200948 " [--textconv | --filters] [-Z]"),
Ævar Arnfjörð Bjarmason5a404172021-12-28 14:28:45 +0100949 NULL
950 };
Michele Ballabio15d8e562008-05-23 16:19:42 +0200951 const struct option options[] = {
Ævar Arnfjörð Bjarmason57d6a1c2021-12-28 14:28:48 +0100952 /* Simple queries */
953 OPT_GROUP(N_("Check object existence or emit object contents")),
Karthik Nayakb48158a2015-05-03 20:00:00 +0530954 OPT_CMDMODE('e', NULL, &opt,
Ævar Arnfjörð Bjarmason57d6a1c2021-12-28 14:28:48 +0100955 N_("check if <object> exists"), 'e'),
956 OPT_CMDMODE('p', NULL, &opt, N_("pretty-print <object> content"), 'p'),
957
958 OPT_GROUP(N_("Emit [broken] object attributes")),
959 OPT_CMDMODE('t', NULL, &opt, N_("show object type (one of 'blob', 'tree', 'commit', 'tag', ...)"), 't'),
960 OPT_CMDMODE('s', NULL, &opt, N_("show object size"), 's'),
Jeff Kingad42f282015-06-22 06:40:56 -0400961 OPT_BOOL(0, "allow-unknown-type", &unknown_type,
Karthik Nayak39e4ae32015-05-03 20:00:01 +0530962 N_("allow -s and -t to work with broken/corrupt objects")),
Siddharth Asthanaec031da2022-07-19 01:21:02 +0530963 OPT_BOOL(0, "use-mailmap", &use_mailmap, N_("use mail map file")),
964 OPT_ALIAS(0, "mailmap", "use-mailmap"),
Ævar Arnfjörð Bjarmason57d6a1c2021-12-28 14:28:48 +0100965 /* Batch mode */
966 OPT_GROUP(N_("Batch objects requested on stdin (or --batch-all-objects)")),
967 OPT_CALLBACK_F(0, "batch", &batch, N_("format"),
968 N_("show full <object> or <rev> contents"),
969 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
970 batch_option_callback),
971 OPT_CALLBACK_F(0, "batch-check", &batch, N_("format"),
972 N_("like --batch, but don't emit <contents>"),
973 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
974 batch_option_callback),
Patrick Steinhardtf79e1882023-06-06 07:19:45 +0200975 OPT_BOOL_F('z', NULL, &input_nul_terminated, N_("stdin is NUL-terminated"),
976 PARSE_OPT_HIDDEN),
977 OPT_BOOL('Z', NULL, &nul_terminated, N_("stdin and stdout is NUL-terminated")),
John Cai440c7052022-02-18 18:23:14 +0000978 OPT_CALLBACK_F(0, "batch-command", &batch, N_("format"),
979 N_("read commands from stdin"),
980 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
981 batch_option_callback),
Ævar Arnfjörð Bjarmason57d6a1c2021-12-28 14:28:48 +0100982 OPT_CMDMODE(0, "batch-all-objects", &opt,
983 N_("with --batch[-check]: ignores stdin, batches all known objects"), 'b'),
984 /* Batch-specific options */
985 OPT_GROUP(N_("Change or optimize batch output")),
Jeff Kingfc4937c2015-06-22 06:45:17 -0400986 OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")),
David Turner122d5342015-05-20 13:03:40 -0400987 OPT_BOOL(0, "follow-symlinks", &batch.follow_symlinks,
Ævar Arnfjörð Bjarmason57d6a1c2021-12-28 14:28:48 +0100988 N_("follow in-tree symlinks")),
Jeff King0750bb52018-08-10 19:24:57 -0400989 OPT_BOOL(0, "unordered", &batch.unordered,
Ævar Arnfjörð Bjarmason57d6a1c2021-12-28 14:28:48 +0100990 N_("do not order objects before emitting them")),
991 /* Textconv options, stand-ole*/
992 OPT_GROUP(N_("Emit object (blob or tree) with conversion or filter (stand-alone, or with batch)")),
993 OPT_CMDMODE(0, "textconv", &opt,
994 N_("run textconv on object's content"), 'c'),
995 OPT_CMDMODE(0, "filters", &opt,
996 N_("run filters on object's content"), 'w'),
997 OPT_STRING(0, "path", &force_path, N_("blob|tree"),
Ævar Arnfjörð Bjarmason83dc4432022-01-10 23:08:45 +0100998 N_("use a <path> for (--textconv | --filters); Not with 'batch'")),
Michele Ballabio15d8e562008-05-23 16:19:42 +0200999 OPT_END()
1000 };
1001
Clément Poulaine5fba602010-06-15 17:50:28 +02001002 git_config(git_cat_file_config, NULL);
Adam Roben9cf71b12008-04-23 15:17:44 -04001003
Jeff King6a36e1e2016-05-18 12:56:14 -04001004 batch.buffer_output = -1;
Adam Robena8128ed2008-04-23 15:17:47 -04001005
Ævar Arnfjörð Bjarmason485fd2c2021-12-28 14:28:46 +01001006 argc = parse_options(argc, argv, prefix, options, usage, 0);
Ævar Arnfjörð Bjarmasonb3fe4682021-12-28 14:28:47 +01001007 opt_cw = (opt == 'c' || opt == 'w');
1008 opt_epts = (opt == 'e' || opt == 'p' || opt == 't' || opt == 's');
1009
Siddharth Asthanaec031da2022-07-19 01:21:02 +05301010 if (use_mailmap)
1011 read_mailmap(&mailmap);
1012
Ævar Arnfjörð Bjarmasonb3fe4682021-12-28 14:28:47 +01001013 /* --batch-all-objects? */
1014 if (opt == 'b')
Ævar Arnfjörð Bjarmason485fd2c2021-12-28 14:28:46 +01001015 batch.all_objects = 1;
Adam Roben9cf71b12008-04-23 15:17:44 -04001016
Ævar Arnfjörð Bjarmasonb3fe4682021-12-28 14:28:47 +01001017 /* Option compatibility */
1018 if (force_path && !opt_cw)
1019 usage_msg_optf(_("'%s=<%s>' needs '%s' or '%s'"),
1020 usage, options,
1021 "--path", _("path|tree-ish"), "--filters",
1022 "--textconv");
David Turner122d5342015-05-20 13:03:40 -04001023
Ævar Arnfjörð Bjarmasonb3fe4682021-12-28 14:28:47 +01001024 /* Option compatibility with batch mode */
1025 if (batch.enabled)
1026 ;
1027 else if (batch.follow_symlinks)
1028 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
Ævar Arnfjörð Bjarmason5fb24902022-01-10 23:08:46 +01001029 "--follow-symlinks");
Ævar Arnfjörð Bjarmasonb3fe4682021-12-28 14:28:47 +01001030 else if (batch.buffer_output >= 0)
1031 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1032 "--buffer");
1033 else if (batch.all_objects)
1034 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1035 "--batch-all-objects");
Patrick Steinhardt3217f522023-06-06 07:19:41 +02001036 else if (input_nul_terminated)
Taylor Blaudb9d67f2022-07-22 19:29:05 -04001037 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1038 "-z");
Patrick Steinhardtf79e1882023-06-06 07:19:45 +02001039 else if (nul_terminated)
1040 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1041 "-Z");
Johannes Schindelin7bcf3412016-09-09 12:10:50 +02001042
Patrick Steinhardtf79e1882023-06-06 07:19:45 +02001043 batch.input_delim = batch.output_delim = '\n';
1044 if (input_nul_terminated)
1045 batch.input_delim = '\0';
1046 if (nul_terminated)
1047 batch.input_delim = batch.output_delim = '\0';
Linus Torvaldse83c5162005-04-07 15:13:13 -07001048
Ævar Arnfjörð Bjarmasonb3fe4682021-12-28 14:28:47 +01001049 /* Batch defaults */
Linus Torvaldse83c5162005-04-07 15:13:13 -07001050 if (batch.buffer_output < 0)
1051 batch.buffer_output = batch.all_objects;
1052
Kevin Lylese65b0c72024-09-03 22:06:47 +00001053 prepare_repo_settings(the_repository);
1054 the_repository->settings.command_requires_full_index = 0;
1055
Ævar Arnfjörð Bjarmasonb3fe4682021-12-28 14:28:47 +01001056 /* Return early if we're in batch mode? */
1057 if (batch.enabled) {
1058 if (opt_cw)
John Caia2c75522022-02-18 18:23:11 +00001059 batch.transform_mode = opt;
Ævar Arnfjörð Bjarmasonb3fe4682021-12-28 14:28:47 +01001060 else if (opt && opt != 'b')
1061 usage_msg_optf(_("'-%c' is incompatible with batch mode"),
1062 usage, options, opt);
1063 else if (argc)
1064 usage_msg_opt(_("batch modes take no arguments"), usage,
1065 options);
1066
Linus Torvaldse83c5162005-04-07 15:13:13 -07001067 return batch_objects(&batch);
Ævar Arnfjörð Bjarmasonb3fe4682021-12-28 14:28:47 +01001068 }
1069
1070 if (opt) {
1071 if (!argc && opt == 'c')
1072 usage_msg_optf(_("<rev> required with '%s'"),
1073 usage, options, "--textconv");
1074 else if (!argc && opt == 'w')
1075 usage_msg_optf(_("<rev> required with '%s'"),
1076 usage, options, "--filters");
1077 else if (!argc && opt_epts)
1078 usage_msg_optf(_("<object> required with '-%c'"),
1079 usage, options, opt);
1080 else if (argc == 1)
1081 obj_name = argv[0];
1082 else
1083 usage_msg_opt(_("too many arguments"), usage, options);
1084 } else if (!argc) {
1085 usage_with_options(usage, options);
1086 } else if (argc != 2) {
1087 usage_msg_optf(_("only two arguments allowed in <type> <object> mode, not %d"),
1088 usage, options, argc);
1089 } else if (argc) {
1090 exp_type = argv[0];
1091 obj_name = argv[1];
1092 }
Linus Torvaldse83c5162005-04-07 15:13:13 -07001093
1094 if (unknown_type && opt != 't' && opt != 's')
1095 die("git cat-file --allow-unknown-type: use with -s or -t");
1096 return cat_one_file(opt, exp_type, obj_name, unknown_type);
1097}