Linus Torvalds | 8bc9a0c | 2005-04-07 15:16:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | */ |
John Cai | 03eae9a | 2024-09-13 21:16:15 +0000 | [diff] [blame] | 6 | #define USE_THE_REPOSITORY_VARIABLE |
Elijah Newren | bc5c5ec | 2023-05-16 06:33:57 +0000 | [diff] [blame] | 7 | #include "builtin.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 8 | #include "config.h" |
Elijah Newren | 73359a9 | 2023-04-11 03:00:40 +0000 | [diff] [blame] | 9 | #include "convert.h" |
Jeff Smith | 3a35cb2 | 2017-05-24 00:15:10 -0500 | [diff] [blame] | 10 | #include "diff.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 11 | #include "environment.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 12 | #include "gettext.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 13 | #include "hex.h" |
Elijah Newren | b5fa608 | 2023-02-24 00:09:29 +0000 | [diff] [blame] | 14 | #include "ident.h" |
Michele Ballabio | 15d8e56 | 2008-05-23 16:19:42 +0200 | [diff] [blame] | 15 | #include "parse-options.h" |
Clément Poulain | e5fba60 | 2010-06-15 17:50:28 +0200 | [diff] [blame] | 16 | #include "userdiff.h" |
Nguyễn Thái Ngọc Duy | 00c8fd4 | 2012-03-07 17:54:17 +0700 | [diff] [blame] | 17 | #include "streaming.h" |
Jeff King | fe299ec | 2020-03-30 10:03:46 -0400 | [diff] [blame] | 18 | #include "oid-array.h" |
Jonathan Tan | 7709f46 | 2017-08-18 15:20:38 -0700 | [diff] [blame] | 19 | #include "packfile.h" |
Elijah Newren | 87bed17 | 2023-04-11 00:41:53 -0700 | [diff] [blame] | 20 | #include "object-file.h" |
Elijah Newren | dabab1d | 2023-04-11 00:41:49 -0700 | [diff] [blame] | 21 | #include "object-name.h" |
Elijah Newren | a034e91 | 2023-05-16 06:34:06 +0000 | [diff] [blame] | 22 | #include "object-store-ll.h" |
Elijah Newren | cbeab74 | 2023-02-24 00:09:33 +0000 | [diff] [blame] | 23 | #include "replace-object.h" |
Christian Couder | b14ed5a | 2019-06-25 15:40:31 +0200 | [diff] [blame] | 24 | #include "promisor-remote.h" |
Siddharth Asthana | ec031da | 2022-07-19 01:21:02 +0530 | [diff] [blame] | 25 | #include "mailmap.h" |
Elijah Newren | d48be35 | 2023-03-21 06:26:07 +0000 | [diff] [blame] | 26 | #include "write-or-die.h" |
Michele Ballabio | 15d8e56 | 2008-05-23 16:19:42 +0200 | [diff] [blame] | 27 | |
John Cai | ac4e58c | 2022-02-18 18:23:12 +0000 | [diff] [blame] | 28 | enum batch_mode { |
| 29 | BATCH_MODE_CONTENTS, |
| 30 | BATCH_MODE_INFO, |
John Cai | 440c705 | 2022-02-18 18:23:14 +0000 | [diff] [blame] | 31 | BATCH_MODE_QUEUE_AND_DISPATCH, |
John Cai | ac4e58c | 2022-02-18 18:23:12 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
Jeff King | bfd1559 | 2015-06-22 06:41:03 -0400 | [diff] [blame] | 34 | struct batch_options { |
| 35 | int enabled; |
| 36 | int follow_symlinks; |
John Cai | ac4e58c | 2022-02-18 18:23:12 +0000 | [diff] [blame] | 37 | enum batch_mode batch_mode; |
Jeff King | fc4937c | 2015-06-22 06:45:17 -0400 | [diff] [blame] | 38 | int buffer_output; |
Jeff King | 6a95193 | 2015-06-22 06:45:59 -0400 | [diff] [blame] | 39 | int all_objects; |
Jeff King | 0750bb5 | 2018-08-10 19:24:57 -0400 | [diff] [blame] | 40 | int unordered; |
John Cai | a2c7552 | 2022-02-18 18:23:11 +0000 | [diff] [blame] | 41 | int transform_mode; /* may be 'w' or 'c' for --filters or --textconv */ |
Patrick Steinhardt | 3217f52 | 2023-06-06 07:19:41 +0200 | [diff] [blame] | 42 | char input_delim; |
Patrick Steinhardt | f79e188 | 2023-06-06 07:19:45 +0200 | [diff] [blame] | 43 | char output_delim; |
Jeff King | bfd1559 | 2015-06-22 06:41:03 -0400 | [diff] [blame] | 44 | const char *format; |
| 45 | }; |
| 46 | |
Johannes Schindelin | 7bcf341 | 2016-09-09 12:10:50 +0200 | [diff] [blame] | 47 | static const char *force_path; |
| 48 | |
Siddharth Asthana | ec031da | 2022-07-19 01:21:02 +0530 | [diff] [blame] | 49 | static struct string_list mailmap = STRING_LIST_INIT_NODUP; |
| 50 | static int use_mailmap; |
| 51 | |
| 52 | static char *replace_idents_using_mailmap(char *, size_t *); |
| 53 | |
| 54 | static 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 Schindelin | b9e62f6 | 2016-08-24 14:23:39 +0200 | [diff] [blame] | 65 | static int filter_object(const char *path, unsigned mode, |
Junio C Hamano | 7889ed2 | 2016-09-21 15:15:18 -0700 | [diff] [blame] | 66 | const struct object_id *oid, |
Johannes Schindelin | b9e62f6 | 2016-08-24 14:23:39 +0200 | [diff] [blame] | 67 | char **buf, unsigned long *size) |
| 68 | { |
| 69 | enum object_type type; |
| 70 | |
Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 15:58:50 +0200 | [diff] [blame] | 71 | *buf = repo_read_object_file(the_repository, oid, &type, size); |
Johannes Schindelin | b9e62f6 | 2016-08-24 14:23:39 +0200 | [diff] [blame] | 72 | if (!*buf) |
| 73 | return error(_("cannot read object %s '%s'"), |
Junio C Hamano | 7889ed2 | 2016-09-21 15:15:18 -0700 | [diff] [blame] | 74 | oid_to_hex(oid), path); |
Johannes Schindelin | b9e62f6 | 2016-08-24 14:23:39 +0200 | [diff] [blame] | 75 | if ((type == OBJ_BLOB) && S_ISREG(mode)) { |
| 76 | struct strbuf strbuf = STRBUF_INIT; |
brian m. carlson | c397aac | 2020-03-16 18:05:03 +0000 | [diff] [blame] | 77 | struct checkout_metadata meta; |
| 78 | |
| 79 | init_checkout_metadata(&meta, NULL, NULL, oid); |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 80 | if (convert_to_working_tree(the_repository->index, path, *buf, *size, &strbuf, &meta)) { |
Johannes Schindelin | b9e62f6 | 2016-08-24 14:23:39 +0200 | [diff] [blame] | 81 | free(*buf); |
| 82 | *size = strbuf.len; |
| 83 | *buf = strbuf_detach(&strbuf, NULL); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
Jeff King | 98f425b | 2018-10-30 19:23:38 -0400 | [diff] [blame] | 90 | static 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 Nayak | 39e4ae3 | 2015-05-03 20:00:01 +0530 | [diff] [blame] | 97 | static int cat_one_file(int opt, const char *exp_type, const char *obj_name, |
| 98 | int unknown_type) |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 99 | { |
Ævar Arnfjörð Bjarmason | 27472b5 | 2022-07-01 12:42:59 +0200 | [diff] [blame] | 100 | int ret; |
brian m. carlson | 63ecb99 | 2016-09-05 20:07:57 +0000 | [diff] [blame] | 101 | struct object_id oid; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 102 | enum object_type type; |
Clément Poulain | e5fba60 | 2010-06-15 17:50:28 +0200 | [diff] [blame] | 103 | char *buf; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 104 | unsigned long size; |
Patrick Steinhardt | f87c55c | 2024-06-11 11:19:59 +0200 | [diff] [blame] | 105 | struct object_context obj_context = {0}; |
Jeff King | 27b5c1a | 2016-08-11 05:24:35 -0400 | [diff] [blame] | 106 | struct object_info oi = OBJECT_INFO_INIT; |
Karthik Nayak | 39e4ae3 | 2015-05-03 20:00:01 +0530 | [diff] [blame] | 107 | struct strbuf sb = STRBUF_INIT; |
Jonathan Tan | 1f0c0d3 | 2017-06-21 17:40:19 -0700 | [diff] [blame] | 108 | unsigned flags = OBJECT_INFO_LOOKUP_REPLACE; |
Eric W. Biederman | d6222a2 | 2023-10-01 21:40:27 -0500 | [diff] [blame] | 109 | unsigned get_oid_flags = |
| 110 | GET_OID_RECORD_PATH | |
| 111 | GET_OID_ONLY_TO_DIE | |
| 112 | GET_OID_HASH_ANY; |
Johannes Schindelin | 7bcf341 | 2016-09-09 12:10:50 +0200 | [diff] [blame] | 113 | const char *path = force_path; |
Ævar Arnfjörð Bjarmason | 245b948 | 2021-12-28 14:28:50 +0100 | [diff] [blame] | 114 | const int opt_cw = (opt == 'c' || opt == 'w'); |
| 115 | if (!path && opt_cw) |
| 116 | get_oid_flags |= GET_OID_REQUIRE_PATH; |
Karthik Nayak | 39e4ae3 | 2015-05-03 20:00:01 +0530 | [diff] [blame] | 117 | |
| 118 | if (unknown_type) |
Jonathan Tan | 19fc5e8 | 2017-06-21 17:40:18 -0700 | [diff] [blame] | 119 | flags |= OBJECT_INFO_ALLOW_UNKNOWN_TYPE; |
Shawn O. Pearce | 2b6854c | 2007-04-21 21:14:39 -0400 | [diff] [blame] | 120 | |
Ævar Arnfjörð Bjarmason | 245b948 | 2021-12-28 14:28:50 +0100 | [diff] [blame] | 121 | if (get_oid_with_context(the_repository, obj_name, get_oid_flags, &oid, |
| 122 | &obj_context)) |
Shawn O. Pearce | 2b6854c | 2007-04-21 21:14:39 -0400 | [diff] [blame] | 123 | die("Not a valid object name %s", obj_name); |
Linus Torvalds | 11e7d5c | 2005-05-01 19:28:18 -0700 | [diff] [blame] | 124 | |
Johannes Schindelin | 7bcf341 | 2016-09-09 12:10:50 +0200 | [diff] [blame] | 125 | if (!path) |
| 126 | path = obj_context.path; |
| 127 | if (obj_context.mode == S_IFINVALID) |
| 128 | obj_context.mode = 0100644; |
| 129 | |
H. Peter Anvin | 7950571 | 2005-12-03 17:57:48 -0800 | [diff] [blame] | 130 | buf = NULL; |
| 131 | switch (opt) { |
| 132 | case 't': |
Brandon Williams | 6ca32f4 | 2018-02-14 10:59:23 -0800 | [diff] [blame] | 133 | oi.type_name = &sb; |
Stefan Beller | 7ecd869 | 2018-04-25 11:20:58 -0700 | [diff] [blame] | 134 | if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0) |
Karthik Nayak | 39e4ae3 | 2015-05-03 20:00:01 +0530 | [diff] [blame] | 135 | 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ð Bjarmason | 27472b5 | 2022-07-01 12:42:59 +0200 | [diff] [blame] | 139 | ret = 0; |
| 140 | goto cleanup; |
Linus Torvalds | 11e7d5c | 2005-05-01 19:28:18 -0700 | [diff] [blame] | 141 | } |
H. Peter Anvin | 7950571 | 2005-12-03 17:57:48 -0800 | [diff] [blame] | 142 | break; |
| 143 | |
| 144 | case 's': |
Karthik Nayak | 39e4ae3 | 2015-05-03 20:00:01 +0530 | [diff] [blame] | 145 | oi.sizep = &size; |
Siddharth Asthana | 49050a0 | 2022-12-20 11:31:12 +0530 | [diff] [blame] | 146 | |
| 147 | if (use_mailmap) { |
| 148 | oi.typep = &type; |
| 149 | oi.contentp = (void**)&buf; |
| 150 | } |
| 151 | |
Stefan Beller | 7ecd869 | 2018-04-25 11:20:58 -0700 | [diff] [blame] | 152 | if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0) |
Karthik Nayak | 39e4ae3 | 2015-05-03 20:00:01 +0530 | [diff] [blame] | 153 | die("git cat-file: could not get object info"); |
Siddharth Asthana | 49050a0 | 2022-12-20 11:31:12 +0530 | [diff] [blame] | 154 | |
| 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ögershausen | ca473ce | 2018-11-11 08:05:04 +0100 | [diff] [blame] | 161 | printf("%"PRIuMAX"\n", (uintmax_t)size); |
Ævar Arnfjörð Bjarmason | 27472b5 | 2022-07-01 12:42:59 +0200 | [diff] [blame] | 162 | ret = 0; |
| 163 | goto cleanup; |
H. Peter Anvin | 7950571 | 2005-12-03 17:57:48 -0800 | [diff] [blame] | 164 | |
| 165 | case 'e': |
Patrick Steinhardt | f87c55c | 2024-06-11 11:19:59 +0200 | [diff] [blame] | 166 | ret = !repo_has_object_file(the_repository, &oid); |
| 167 | goto cleanup; |
H. Peter Anvin | 7950571 | 2005-12-03 17:57:48 -0800 | [diff] [blame] | 168 | |
Johannes Schindelin | b9e62f6 | 2016-08-24 14:23:39 +0200 | [diff] [blame] | 169 | case 'w': |
Johannes Schindelin | b9e62f6 | 2016-08-24 14:23:39 +0200 | [diff] [blame] | 170 | |
Johannes Schindelin | 7bcf341 | 2016-09-09 12:10:50 +0200 | [diff] [blame] | 171 | if (filter_object(path, obj_context.mode, |
Ævar Arnfjörð Bjarmason | 27472b5 | 2022-07-01 12:42:59 +0200 | [diff] [blame] | 172 | &oid, &buf, &size)) { |
| 173 | ret = -1; |
| 174 | goto cleanup; |
| 175 | } |
Johannes Schindelin | b9e62f6 | 2016-08-24 14:23:39 +0200 | [diff] [blame] | 176 | break; |
| 177 | |
Michael J Gruber | 3ac2161 | 2013-05-10 17:10:13 +0200 | [diff] [blame] | 178 | case 'c': |
Nguyễn Thái Ngọc Duy | 6afaf80 | 2018-09-21 17:57:22 +0200 | [diff] [blame] | 179 | if (textconv_object(the_repository, path, obj_context.mode, |
| 180 | &oid, 1, &buf, &size)) |
Michael J Gruber | 3ac2161 | 2013-05-10 17:10:13 +0200 | [diff] [blame] | 181 | break; |
Jeff King | 1cf01a3 | 2017-09-21 02:25:41 -0400 | [diff] [blame] | 182 | /* else fallthrough */ |
Michael J Gruber | 3ac2161 | 2013-05-10 17:10:13 +0200 | [diff] [blame] | 183 | |
Junio C Hamano | a0f15fa | 2006-03-01 16:43:19 -0800 | [diff] [blame] | 184 | case 'p': |
Stefan Beller | 0df8e96 | 2018-04-25 11:20:59 -0700 | [diff] [blame] | 185 | type = oid_object_info(the_repository, &oid, NULL); |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 186 | if (type < 0) |
Shawn O. Pearce | 2b6854c | 2007-04-21 21:14:39 -0400 | [diff] [blame] | 187 | die("Not a valid object name %s", obj_name); |
Junio C Hamano | a0f15fa | 2006-03-01 16:43:19 -0800 | [diff] [blame] | 188 | |
| 189 | /* custom pretty-print here */ |
Shawn O. Pearce | 2b6854c | 2007-04-21 21:14:39 -0400 | [diff] [blame] | 190 | if (type == OBJ_TREE) { |
Gary V. Vaughan | 66dbfd5 | 2010-05-14 09:31:33 +0000 | [diff] [blame] | 191 | const char *ls_args[3] = { NULL }; |
| 192 | ls_args[0] = "ls-tree"; |
| 193 | ls_args[1] = obj_name; |
John Cai | 9b1cb50 | 2024-09-13 21:16:14 +0000 | [diff] [blame] | 194 | ret = cmd_ls_tree(2, ls_args, NULL, the_repository); |
Ævar Arnfjörð Bjarmason | 27472b5 | 2022-07-01 12:42:59 +0200 | [diff] [blame] | 195 | goto cleanup; |
Shawn O. Pearce | 2b6854c | 2007-04-21 21:14:39 -0400 | [diff] [blame] | 196 | } |
Junio C Hamano | a0f15fa | 2006-03-01 16:43:19 -0800 | [diff] [blame] | 197 | |
Ævar Arnfjörð Bjarmason | 27472b5 | 2022-07-01 12:42:59 +0200 | [diff] [blame] | 198 | if (type == OBJ_BLOB) { |
| 199 | ret = stream_blob(&oid); |
| 200 | goto cleanup; |
| 201 | } |
Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 15:58:50 +0200 | [diff] [blame] | 202 | buf = repo_read_object_file(the_repository, &oid, &type, |
| 203 | &size); |
Junio C Hamano | a0f15fa | 2006-03-01 16:43:19 -0800 | [diff] [blame] | 204 | if (!buf) |
Shawn O. Pearce | 2b6854c | 2007-04-21 21:14:39 -0400 | [diff] [blame] | 205 | die("Cannot read object %s", obj_name); |
Junio C Hamano | a0f15fa | 2006-03-01 16:43:19 -0800 | [diff] [blame] | 206 | |
Siddharth Asthana | ec031da | 2022-07-19 01:21:02 +0530 | [diff] [blame] | 207 | 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 Hamano | a0f15fa | 2006-03-01 16:43:19 -0800 | [diff] [blame] | 213 | /* otherwise just spit out the data */ |
| 214 | break; |
Clément Poulain | e5fba60 | 2010-06-15 17:50:28 +0200 | [diff] [blame] | 215 | |
H. Peter Anvin | 7950571 | 2005-12-03 17:57:48 -0800 | [diff] [blame] | 216 | case 0: |
Ævar Arnfjörð Bjarmason | 6aea6ba | 2022-02-05 00:48:34 +0100 | [diff] [blame] | 217 | { |
| 218 | enum object_type exp_type_id = type_from_string(exp_type); |
| 219 | |
| 220 | if (exp_type_id == OBJ_BLOB) { |
brian m. carlson | 63ecb99 | 2016-09-05 20:07:57 +0000 | [diff] [blame] | 221 | struct object_id blob_oid; |
Stefan Beller | 0df8e96 | 2018-04-25 11:20:59 -0700 | [diff] [blame] | 222 | if (oid_object_info(the_repository, &oid, NULL) == OBJ_TAG) { |
Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 15:58:50 +0200 | [diff] [blame] | 223 | char *buffer = repo_read_object_file(the_repository, |
| 224 | &oid, |
| 225 | &type, |
| 226 | &size); |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 227 | const char *target; |
Johannes Schindelin | 568459b | 2024-02-05 14:35:53 +0000 | [diff] [blame] | 228 | |
| 229 | if (!buffer) |
| 230 | die(_("unable to read %s"), oid_to_hex(&oid)); |
| 231 | |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 232 | if (!skip_prefix(buffer, "object ", &target) || |
Eric W. Biederman | d6222a2 | 2023-10-01 21:40:27 -0500 | [diff] [blame] | 233 | get_oid_hex_algop(target, &blob_oid, |
| 234 | &hash_algos[oid.algo])) |
brian m. carlson | 63ecb99 | 2016-09-05 20:07:57 +0000 | [diff] [blame] | 235 | die("%s not a valid tag", oid_to_hex(&oid)); |
Nguyễn Thái Ngọc Duy | 00c8fd4 | 2012-03-07 17:54:17 +0700 | [diff] [blame] | 236 | free(buffer); |
| 237 | } else |
brian m. carlson | 63ecb99 | 2016-09-05 20:07:57 +0000 | [diff] [blame] | 238 | oidcpy(&blob_oid, &oid); |
Nguyễn Thái Ngọc Duy | 00c8fd4 | 2012-03-07 17:54:17 +0700 | [diff] [blame] | 239 | |
Ævar Arnfjörð Bjarmason | 27472b5 | 2022-07-01 12:42:59 +0200 | [diff] [blame] | 240 | 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 Duy | 00c8fd4 | 2012-03-07 17:54:17 +0700 | [diff] [blame] | 244 | /* |
| 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ð Bjarmason | 6aea6ba | 2022-02-05 00:48:34 +0100 | [diff] [blame] | 251 | buf = read_object_with_reference(the_repository, &oid, |
| 252 | exp_type_id, &size, NULL); |
Siddharth Asthana | ec031da | 2022-07-19 01:21:02 +0530 | [diff] [blame] | 253 | |
| 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 Anvin | 7950571 | 2005-12-03 17:57:48 -0800 | [diff] [blame] | 259 | break; |
Ævar Arnfjörð Bjarmason | 6aea6ba | 2022-02-05 00:48:34 +0100 | [diff] [blame] | 260 | } |
H. Peter Anvin | 7950571 | 2005-12-03 17:57:48 -0800 | [diff] [blame] | 261 | default: |
Alexander Potashev | d753070 | 2009-01-04 21:38:41 +0300 | [diff] [blame] | 262 | die("git cat-file: unknown option: %s", exp_type); |
Linus Torvalds | 11e7d5c | 2005-05-01 19:28:18 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Petr Baudis | 2de381f | 2005-04-13 02:28:48 -0700 | [diff] [blame] | 265 | if (!buf) |
Heikki Orsila | 34baebc | 2008-08-30 14:12:53 +0300 | [diff] [blame] | 266 | die("git cat-file %s: bad file", obj_name); |
Linus Torvalds | bf0c6e8 | 2005-04-08 09:16:38 -0700 | [diff] [blame] | 267 | |
Rene Scharfe | 7230e6d | 2006-08-21 20:43:43 +0200 | [diff] [blame] | 268 | write_or_die(1, buf, size); |
Ævar Arnfjörð Bjarmason | 27472b5 | 2022-07-01 12:42:59 +0200 | [diff] [blame] | 269 | ret = 0; |
| 270 | cleanup: |
Johannes Schindelin | 05c2b7b | 2017-05-04 15:56:17 +0200 | [diff] [blame] | 271 | free(buf); |
Patrick Steinhardt | f87c55c | 2024-06-11 11:19:59 +0200 | [diff] [blame] | 272 | object_context_release(&obj_context); |
Ævar Arnfjörð Bjarmason | 27472b5 | 2022-07-01 12:42:59 +0200 | [diff] [blame] | 273 | return ret; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 274 | } |
Adam Roben | 9cf71b1 | 2008-04-23 15:17:44 -0400 | [diff] [blame] | 275 | |
Jeff King | 93d2a60 | 2013-07-10 07:45:47 -0400 | [diff] [blame] | 276 | struct expand_data { |
brian m. carlson | cd4f77b | 2016-09-05 20:07:56 +0000 | [diff] [blame] | 277 | struct object_id oid; |
Jeff King | 93d2a60 | 2013-07-10 07:45:47 -0400 | [diff] [blame] | 278 | enum object_type type; |
| 279 | unsigned long size; |
Nguyễn Thái Ngọc Duy | 166df26 | 2016-07-13 17:43:59 +0200 | [diff] [blame] | 280 | off_t disk_size; |
Jeff King | 97be040 | 2013-08-02 04:59:07 -0700 | [diff] [blame] | 281 | const char *rest; |
brian m. carlson | cd4f77b | 2016-09-05 20:07:56 +0000 | [diff] [blame] | 282 | struct object_id delta_base_oid; |
Jeff King | 93d2a60 | 2013-07-10 07:45:47 -0400 | [diff] [blame] | 283 | |
| 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 King | 97be040 | 2013-08-02 04:59:07 -0700 | [diff] [blame] | 291 | * 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 King | 93d2a60 | 2013-07-10 07:45:47 -0400 | [diff] [blame] | 298 | * After a mark_query run, this object_info is set up to be |
Jeff King | c93206b | 2019-01-07 03:34:12 -0500 | [diff] [blame] | 299 | * passed to oid_object_info_extended. It will point to the data |
Jeff King | 93d2a60 | 2013-07-10 07:45:47 -0400 | [diff] [blame] | 300 | * elements above, so you can retrieve the response from there. |
| 301 | */ |
| 302 | struct object_info info; |
Jeff King | 845de33 | 2016-05-18 12:55:23 -0400 | [diff] [blame] | 303 | |
| 304 | /* |
| 305 | * This flag will be true if the requested batch format and options |
Jeff King | c93206b | 2019-01-07 03:34:12 -0500 | [diff] [blame] | 306 | * don't require us to call oid_object_info, which can then be |
Jeff King | 845de33 | 2016-05-18 12:55:23 -0400 | [diff] [blame] | 307 | * optimized out. |
| 308 | */ |
| 309 | unsigned skip_object_info : 1; |
Jeff King | 93d2a60 | 2013-07-10 07:45:47 -0400 | [diff] [blame] | 310 | }; |
| 311 | |
| 312 | static 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é Scharfe | 7c43bdf | 2024-03-24 12:21:15 +0100 | [diff] [blame] | 318 | static int expand_atom(struct strbuf *sb, const char *atom, int len, |
| 319 | struct expand_data *data) |
Jeff King | 93d2a60 | 2013-07-10 07:45:47 -0400 | [diff] [blame] | 320 | { |
Jeff King | 93d2a60 | 2013-07-10 07:45:47 -0400 | [diff] [blame] | 321 | if (is_atom("objectname", atom, len)) { |
| 322 | if (!data->mark_query) |
brian m. carlson | cd4f77b | 2016-09-05 20:07:56 +0000 | [diff] [blame] | 323 | strbuf_addstr(sb, oid_to_hex(&data->oid)); |
Jeff King | 93d2a60 | 2013-07-10 07:45:47 -0400 | [diff] [blame] | 324 | } else if (is_atom("objecttype", atom, len)) { |
Jeff King | 5b08640 | 2013-07-12 02:34:57 -0400 | [diff] [blame] | 325 | if (data->mark_query) |
| 326 | data->info.typep = &data->type; |
| 327 | else |
Brandon Williams | debca9d | 2018-02-14 10:59:24 -0800 | [diff] [blame] | 328 | strbuf_addstr(sb, type_name(data->type)); |
Jeff King | 93d2a60 | 2013-07-10 07:45:47 -0400 | [diff] [blame] | 329 | } else if (is_atom("objectsize", atom, len)) { |
| 330 | if (data->mark_query) |
| 331 | data->info.sizep = &data->size; |
| 332 | else |
Torsten Bögershausen | ca473ce | 2018-11-11 08:05:04 +0100 | [diff] [blame] | 333 | strbuf_addf(sb, "%"PRIuMAX , (uintmax_t)data->size); |
Jeff King | a4ac106 | 2013-07-10 07:46:25 -0400 | [diff] [blame] | 334 | } 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 Duy | 166df26 | 2016-07-13 17:43:59 +0200 | [diff] [blame] | 338 | strbuf_addf(sb, "%"PRIuMAX, (uintmax_t)data->disk_size); |
Jeff King | 97be040 | 2013-08-02 04:59:07 -0700 | [diff] [blame] | 339 | } 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 King | 65ea9c3 | 2013-12-21 09:25:22 -0500 | [diff] [blame] | 344 | } else if (is_atom("deltabase", atom, len)) { |
| 345 | if (data->mark_query) |
Jeff King | b99b6bc | 2020-02-23 23:36:56 -0500 | [diff] [blame] | 346 | data->info.delta_base_oid = &data->delta_base_oid; |
Jeff King | 65ea9c3 | 2013-12-21 09:25:22 -0500 | [diff] [blame] | 347 | else |
brian m. carlson | cd4f77b | 2016-09-05 20:07:56 +0000 | [diff] [blame] | 348 | strbuf_addstr(sb, |
| 349 | oid_to_hex(&data->delta_base_oid)); |
Jeff King | 93d2a60 | 2013-07-10 07:45:47 -0400 | [diff] [blame] | 350 | } else |
René Scharfe | 7c43bdf | 2024-03-24 12:21:15 +0100 | [diff] [blame] | 351 | return 0; |
| 352 | return 1; |
Jeff King | 93d2a60 | 2013-07-10 07:45:47 -0400 | [diff] [blame] | 353 | } |
| 354 | |
René Scharfe | 6f1e2d5 | 2023-06-17 22:43:17 +0200 | [diff] [blame] | 355 | static void expand_format(struct strbuf *sb, const char *start, |
| 356 | struct expand_data *data) |
Jeff King | 93d2a60 | 2013-07-10 07:45:47 -0400 | [diff] [blame] | 357 | { |
René Scharfe | 6f1e2d5 | 2023-06-17 22:43:17 +0200 | [diff] [blame] | 358 | while (strbuf_expand_step(sb, &start)) { |
| 359 | const char *end; |
Jeff King | 93d2a60 | 2013-07-10 07:45:47 -0400 | [diff] [blame] | 360 | |
René Scharfe | 6f1e2d5 | 2023-06-17 22:43:17 +0200 | [diff] [blame] | 361 | if (skip_prefix(start, "%", &start) || *start != '(') |
| 362 | strbuf_addch(sb, '%'); |
René Scharfe | 7c43bdf | 2024-03-24 12:21:15 +0100 | [diff] [blame] | 363 | else if ((end = strchr(start + 1, ')')) && |
| 364 | expand_atom(sb, start + 1, end - start - 1, data)) |
René Scharfe | 6f1e2d5 | 2023-06-17 22:43:17 +0200 | [diff] [blame] | 365 | start = end + 1; |
René Scharfe | 7c43bdf | 2024-03-24 12:21:15 +0100 | [diff] [blame] | 366 | else |
| 367 | strbuf_expand_bad_format(start, "cat-file"); |
René Scharfe | 6f1e2d5 | 2023-06-17 22:43:17 +0200 | [diff] [blame] | 368 | } |
Jeff King | 93d2a60 | 2013-07-10 07:45:47 -0400 | [diff] [blame] | 369 | } |
| 370 | |
Jeff King | fc4937c | 2015-06-22 06:45:17 -0400 | [diff] [blame] | 371 | static 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 | |
| 380 | static void print_object_or_die(struct batch_options *opt, struct expand_data *data) |
Jeff King | 98e2092 | 2013-07-10 07:38:24 -0400 | [diff] [blame] | 381 | { |
brian m. carlson | 63ecb99 | 2016-09-05 20:07:57 +0000 | [diff] [blame] | 382 | const struct object_id *oid = &data->oid; |
Jeff King | 370c926 | 2013-12-12 07:01:42 +0800 | [diff] [blame] | 383 | |
Jeff King | 6554dfa | 2013-12-12 07:15:50 +0800 | [diff] [blame] | 384 | assert(data->info.typep); |
| 385 | |
Jeff King | 370c926 | 2013-12-12 07:01:42 +0800 | [diff] [blame] | 386 | if (data->type == OBJ_BLOB) { |
Jeff King | fc4937c | 2015-06-22 06:45:17 -0400 | [diff] [blame] | 387 | if (opt->buffer_output) |
| 388 | fflush(stdout); |
John Cai | a2c7552 | 2022-02-18 18:23:11 +0000 | [diff] [blame] | 389 | if (opt->transform_mode) { |
Johannes Schindelin | 3214594 | 2016-09-09 12:10:54 +0200 | [diff] [blame] | 390 | char *contents; |
| 391 | unsigned long size; |
| 392 | |
| 393 | if (!data->rest) |
Junio C Hamano | 7889ed2 | 2016-09-21 15:15:18 -0700 | [diff] [blame] | 394 | die("missing path for '%s'", oid_to_hex(oid)); |
Johannes Schindelin | 3214594 | 2016-09-09 12:10:54 +0200 | [diff] [blame] | 395 | |
John Cai | a2c7552 | 2022-02-18 18:23:11 +0000 | [diff] [blame] | 396 | if (opt->transform_mode == 'w') { |
Junio C Hamano | 7889ed2 | 2016-09-21 15:15:18 -0700 | [diff] [blame] | 397 | if (filter_object(data->rest, 0100644, oid, |
Johannes Schindelin | 3214594 | 2016-09-09 12:10:54 +0200 | [diff] [blame] | 398 | &contents, &size)) |
| 399 | die("could not convert '%s' %s", |
Junio C Hamano | 7889ed2 | 2016-09-21 15:15:18 -0700 | [diff] [blame] | 400 | oid_to_hex(oid), data->rest); |
John Cai | a2c7552 | 2022-02-18 18:23:11 +0000 | [diff] [blame] | 401 | } else if (opt->transform_mode == 'c') { |
Johannes Schindelin | 3214594 | 2016-09-09 12:10:54 +0200 | [diff] [blame] | 402 | enum object_type type; |
Nguyễn Thái Ngọc Duy | 6afaf80 | 2018-09-21 17:57:22 +0200 | [diff] [blame] | 403 | if (!textconv_object(the_repository, |
| 404 | data->rest, 0100644, oid, |
Johannes Schindelin | 3214594 | 2016-09-09 12:10:54 +0200 | [diff] [blame] | 405 | 1, &contents, &size)) |
Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 15:58:50 +0200 | [diff] [blame] | 406 | contents = repo_read_object_file(the_repository, |
| 407 | oid, |
| 408 | &type, |
| 409 | &size); |
Johannes Schindelin | 3214594 | 2016-09-09 12:10:54 +0200 | [diff] [blame] | 410 | if (!contents) |
| 411 | die("could not convert '%s' %s", |
Junio C Hamano | 7889ed2 | 2016-09-21 15:15:18 -0700 | [diff] [blame] | 412 | oid_to_hex(oid), data->rest); |
Johannes Schindelin | 3214594 | 2016-09-09 12:10:54 +0200 | [diff] [blame] | 413 | } else |
John Cai | a2c7552 | 2022-02-18 18:23:11 +0000 | [diff] [blame] | 414 | BUG("invalid transform_mode: %c", opt->transform_mode); |
Johannes Schindelin | 3214594 | 2016-09-09 12:10:54 +0200 | [diff] [blame] | 415 | batch_write(opt, contents, size); |
| 416 | free(contents); |
Jeff King | 98f425b | 2018-10-30 19:23:38 -0400 | [diff] [blame] | 417 | } else { |
| 418 | stream_blob(oid); |
| 419 | } |
Jeff King | 98e2092 | 2013-07-10 07:38:24 -0400 | [diff] [blame] | 420 | } |
| 421 | else { |
Jeff King | 370c926 | 2013-12-12 07:01:42 +0800 | [diff] [blame] | 422 | enum object_type type; |
| 423 | unsigned long size; |
Jeff King | 98e2092 | 2013-07-10 07:38:24 -0400 | [diff] [blame] | 424 | void *contents; |
| 425 | |
Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 15:58:50 +0200 | [diff] [blame] | 426 | contents = repo_read_object_file(the_repository, oid, &type, |
| 427 | &size); |
Johannes Schindelin | 568459b | 2024-02-05 14:35:53 +0000 | [diff] [blame] | 428 | if (!contents) |
| 429 | die("object %s disappeared", oid_to_hex(oid)); |
Siddharth Asthana | ec031da | 2022-07-19 01:21:02 +0530 | [diff] [blame] | 430 | |
| 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 King | 370c926 | 2013-12-12 07:01:42 +0800 | [diff] [blame] | 437 | if (type != data->type) |
brian m. carlson | 63ecb99 | 2016-09-05 20:07:57 +0000 | [diff] [blame] | 438 | die("object %s changed type!?", oid_to_hex(oid)); |
Siddharth Asthana | ec031da | 2022-07-19 01:21:02 +0530 | [diff] [blame] | 439 | if (data->info.sizep && size != data->size && !use_mailmap) |
brian m. carlson | 63ecb99 | 2016-09-05 20:07:57 +0000 | [diff] [blame] | 440 | die("object %s changed size!?", oid_to_hex(oid)); |
Jeff King | 98e2092 | 2013-07-10 07:38:24 -0400 | [diff] [blame] | 441 | |
Jeff King | fc4937c | 2015-06-22 06:45:17 -0400 | [diff] [blame] | 442 | batch_write(opt, contents, size); |
Jeff King | 98e2092 | 2013-07-10 07:38:24 -0400 | [diff] [blame] | 443 | free(contents); |
| 444 | } |
| 445 | } |
| 446 | |
Patrick Steinhardt | f79e188 | 2023-06-06 07:19:45 +0200 | [diff] [blame] | 447 | static void print_default_format(struct strbuf *scratch, struct expand_data *data, |
| 448 | struct batch_options *opt) |
John Cai | eb54a33 | 2022-03-15 02:40:36 +0000 | [diff] [blame] | 449 | { |
Patrick Steinhardt | f79e188 | 2023-06-06 07:19:45 +0200 | [diff] [blame] | 450 | strbuf_addf(scratch, "%s %s %"PRIuMAX"%c", oid_to_hex(&data->oid), |
John Cai | eb54a33 | 2022-03-15 02:40:36 +0000 | [diff] [blame] | 451 | type_name(data->type), |
Patrick Steinhardt | f79e188 | 2023-06-06 07:19:45 +0200 | [diff] [blame] | 452 | (uintmax_t)data->size, opt->output_delim); |
John Cai | eb54a33 | 2022-03-15 02:40:36 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Jeff King | bf97289 | 2021-10-05 16:38:04 -0400 | [diff] [blame] | 455 | /* |
| 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 King | 79ed0a5 | 2018-08-14 14:20:22 -0400 | [diff] [blame] | 460 | static void batch_object_write(const char *obj_name, |
| 461 | struct strbuf *scratch, |
| 462 | struct batch_options *opt, |
Jeff King | bf97289 | 2021-10-05 16:38:04 -0400 | [diff] [blame] | 463 | struct expand_data *data, |
| 464 | struct packed_git *pack, |
| 465 | off_t offset) |
Jeff King | 44b877e | 2015-06-22 06:45:41 -0400 | [diff] [blame] | 466 | { |
Jeff King | bf97289 | 2021-10-05 16:38:04 -0400 | [diff] [blame] | 467 | if (!data->skip_object_info) { |
| 468 | int ret; |
| 469 | |
Siddharth Asthana | a797c0e | 2022-12-20 11:31:13 +0530 | [diff] [blame] | 470 | if (use_mailmap) |
| 471 | data->info.typep = &data->type; |
| 472 | |
Jeff King | bf97289 | 2021-10-05 16:38:04 -0400 | [diff] [blame] | 473 | 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 Steinhardt | f79e188 | 2023-06-06 07:19:45 +0200 | [diff] [blame] | 481 | printf("%s missing%c", |
| 482 | obj_name ? obj_name : oid_to_hex(&data->oid), opt->output_delim); |
Jeff King | bf97289 | 2021-10-05 16:38:04 -0400 | [diff] [blame] | 483 | fflush(stdout); |
| 484 | return; |
| 485 | } |
Siddharth Asthana | a797c0e | 2022-12-20 11:31:13 +0530 | [diff] [blame] | 486 | |
| 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 Schindelin | 568459b | 2024-02-05 14:35:53 +0000 | [diff] [blame] | 493 | if (!buf) |
| 494 | die(_("unable to read %s"), oid_to_hex(&data->oid)); |
Siddharth Asthana | a797c0e | 2022-12-20 11:31:13 +0530 | [diff] [blame] | 495 | buf = replace_idents_using_mailmap(buf, &s); |
| 496 | data->size = cast_size_t_to_ulong(s); |
| 497 | |
| 498 | free(buf); |
| 499 | } |
Jeff King | 44b877e | 2015-06-22 06:45:41 -0400 | [diff] [blame] | 500 | } |
| 501 | |
Jeff King | 79ed0a5 | 2018-08-14 14:20:22 -0400 | [diff] [blame] | 502 | strbuf_reset(scratch); |
John Cai | eb54a33 | 2022-03-15 02:40:36 +0000 | [diff] [blame] | 503 | |
| 504 | if (!opt->format) { |
Patrick Steinhardt | f79e188 | 2023-06-06 07:19:45 +0200 | [diff] [blame] | 505 | print_default_format(scratch, data, opt); |
John Cai | eb54a33 | 2022-03-15 02:40:36 +0000 | [diff] [blame] | 506 | } else { |
René Scharfe | 6f1e2d5 | 2023-06-17 22:43:17 +0200 | [diff] [blame] | 507 | expand_format(scratch, opt->format, data); |
Patrick Steinhardt | f79e188 | 2023-06-06 07:19:45 +0200 | [diff] [blame] | 508 | strbuf_addch(scratch, opt->output_delim); |
John Cai | eb54a33 | 2022-03-15 02:40:36 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Jeff King | 79ed0a5 | 2018-08-14 14:20:22 -0400 | [diff] [blame] | 511 | batch_write(opt, scratch->buf, scratch->len); |
Jeff King | 44b877e | 2015-06-22 06:45:41 -0400 | [diff] [blame] | 512 | |
John Cai | ac4e58c | 2022-02-18 18:23:12 +0000 | [diff] [blame] | 513 | if (opt->batch_mode == BATCH_MODE_CONTENTS) { |
Jeff King | 44b877e | 2015-06-22 06:45:41 -0400 | [diff] [blame] | 514 | print_object_or_die(opt, data); |
Patrick Steinhardt | f79e188 | 2023-06-06 07:19:45 +0200 | [diff] [blame] | 515 | batch_write(opt, &opt->output_delim, 1); |
Jeff King | 44b877e | 2015-06-22 06:45:41 -0400 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
Jeff King | 79ed0a5 | 2018-08-14 14:20:22 -0400 | [diff] [blame] | 519 | static void batch_one_object(const char *obj_name, |
| 520 | struct strbuf *scratch, |
| 521 | struct batch_options *opt, |
Jeff King | 8233095 | 2015-06-22 06:45:33 -0400 | [diff] [blame] | 522 | struct expand_data *data) |
Adam Roben | 05d5667 | 2008-04-23 15:17:46 -0400 | [diff] [blame] | 523 | { |
Patrick Steinhardt | f87c55c | 2024-06-11 11:19:59 +0200 | [diff] [blame] | 524 | struct object_context ctx = {0}; |
Eric W. Biederman | d6222a2 | 2023-10-01 21:40:27 -0500 | [diff] [blame] | 525 | int flags = |
| 526 | GET_OID_HASH_ANY | |
| 527 | (opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0); |
David Turner | d1dd94b | 2019-01-17 23:19:43 -0500 | [diff] [blame] | 528 | enum get_oid_result result; |
Adam Roben | 05d5667 | 2008-04-23 15:17:46 -0400 | [diff] [blame] | 529 | |
Nguyễn Thái Ngọc Duy | 3a7a698 | 2019-01-12 09:13:28 +0700 | [diff] [blame] | 530 | result = get_oid_with_context(the_repository, obj_name, |
| 531 | flags, &data->oid, &ctx); |
David Turner | 122d534 | 2015-05-20 13:03:40 -0400 | [diff] [blame] | 532 | if (result != FOUND) { |
| 533 | switch (result) { |
| 534 | case MISSING_OBJECT: |
Patrick Steinhardt | f79e188 | 2023-06-06 07:19:45 +0200 | [diff] [blame] | 535 | printf("%s missing%c", obj_name, opt->output_delim); |
David Turner | 122d534 | 2015-05-20 13:03:40 -0400 | [diff] [blame] | 536 | break; |
David Turner | d1dd94b | 2019-01-17 23:19:43 -0500 | [diff] [blame] | 537 | case SHORT_NAME_AMBIGUOUS: |
Patrick Steinhardt | f79e188 | 2023-06-06 07:19:45 +0200 | [diff] [blame] | 538 | printf("%s ambiguous%c", obj_name, opt->output_delim); |
David Turner | d1dd94b | 2019-01-17 23:19:43 -0500 | [diff] [blame] | 539 | break; |
David Turner | 122d534 | 2015-05-20 13:03:40 -0400 | [diff] [blame] | 540 | case DANGLING_SYMLINK: |
Patrick Steinhardt | f79e188 | 2023-06-06 07:19:45 +0200 | [diff] [blame] | 541 | printf("dangling %"PRIuMAX"%c%s%c", |
| 542 | (uintmax_t)strlen(obj_name), |
| 543 | opt->output_delim, obj_name, opt->output_delim); |
David Turner | 122d534 | 2015-05-20 13:03:40 -0400 | [diff] [blame] | 544 | break; |
| 545 | case SYMLINK_LOOP: |
Patrick Steinhardt | f79e188 | 2023-06-06 07:19:45 +0200 | [diff] [blame] | 546 | printf("loop %"PRIuMAX"%c%s%c", |
| 547 | (uintmax_t)strlen(obj_name), |
| 548 | opt->output_delim, obj_name, opt->output_delim); |
David Turner | 122d534 | 2015-05-20 13:03:40 -0400 | [diff] [blame] | 549 | break; |
| 550 | case NOT_DIR: |
Patrick Steinhardt | f79e188 | 2023-06-06 07:19:45 +0200 | [diff] [blame] | 551 | printf("notdir %"PRIuMAX"%c%s%c", |
| 552 | (uintmax_t)strlen(obj_name), |
| 553 | opt->output_delim, obj_name, opt->output_delim); |
David Turner | 122d534 | 2015-05-20 13:03:40 -0400 | [diff] [blame] | 554 | break; |
| 555 | default: |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 556 | BUG("unknown get_sha1_with_context result %d\n", |
David Turner | 122d534 | 2015-05-20 13:03:40 -0400 | [diff] [blame] | 557 | result); |
| 558 | break; |
| 559 | } |
| 560 | fflush(stdout); |
Patrick Steinhardt | f87c55c | 2024-06-11 11:19:59 +0200 | [diff] [blame] | 561 | |
| 562 | goto out; |
David Turner | 122d534 | 2015-05-20 13:03:40 -0400 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | if (ctx.mode == 0) { |
Patrick Steinhardt | f79e188 | 2023-06-06 07:19:45 +0200 | [diff] [blame] | 566 | printf("symlink %"PRIuMAX"%c%s%c", |
David Turner | 122d534 | 2015-05-20 13:03:40 -0400 | [diff] [blame] | 567 | (uintmax_t)ctx.symlink_path.len, |
Patrick Steinhardt | f79e188 | 2023-06-06 07:19:45 +0200 | [diff] [blame] | 568 | opt->output_delim, ctx.symlink_path.buf, opt->output_delim); |
Lea Wiemann | 422b206 | 2008-06-03 20:34:17 +0200 | [diff] [blame] | 569 | fflush(stdout); |
Patrick Steinhardt | f87c55c | 2024-06-11 11:19:59 +0200 | [diff] [blame] | 570 | goto out; |
Adam Roben | 05d5667 | 2008-04-23 15:17:46 -0400 | [diff] [blame] | 571 | } |
| 572 | |
Jeff King | bf97289 | 2021-10-05 16:38:04 -0400 | [diff] [blame] | 573 | batch_object_write(obj_name, scratch, opt, data, NULL, 0); |
Patrick Steinhardt | f87c55c | 2024-06-11 11:19:59 +0200 | [diff] [blame] | 574 | |
| 575 | out: |
| 576 | object_context_release(&ctx); |
Adam Roben | 05d5667 | 2008-04-23 15:17:46 -0400 | [diff] [blame] | 577 | } |
| 578 | |
Jeff King | 6a95193 | 2015-06-22 06:45:59 -0400 | [diff] [blame] | 579 | struct object_cb_data { |
| 580 | struct batch_options *opt; |
| 581 | struct expand_data *expand; |
Jeff King | 0750bb5 | 2018-08-10 19:24:57 -0400 | [diff] [blame] | 582 | struct oidset *seen; |
Jeff King | 79ed0a5 | 2018-08-14 14:20:22 -0400 | [diff] [blame] | 583 | struct strbuf *scratch; |
Jeff King | 6a95193 | 2015-06-22 06:45:59 -0400 | [diff] [blame] | 584 | }; |
| 585 | |
brian m. carlson | 1b7ba79 | 2017-03-31 01:39:59 +0000 | [diff] [blame] | 586 | static int batch_object_cb(const struct object_id *oid, void *vdata) |
Jeff King | 6a95193 | 2015-06-22 06:45:59 -0400 | [diff] [blame] | 587 | { |
Jeff King | 3115ee4 | 2015-06-22 07:06:32 -0400 | [diff] [blame] | 588 | struct object_cb_data *data = vdata; |
brian m. carlson | 1b7ba79 | 2017-03-31 01:39:59 +0000 | [diff] [blame] | 589 | oidcpy(&data->expand->oid, oid); |
Jeff King | bf97289 | 2021-10-05 16:38:04 -0400 | [diff] [blame] | 590 | batch_object_write(NULL, data->scratch, data->opt, data->expand, |
| 591 | NULL, 0); |
Jeff King | 16ddcd4 | 2016-09-26 08:00:29 -0400 | [diff] [blame] | 592 | return 0; |
Jeff King | 6a95193 | 2015-06-22 06:45:59 -0400 | [diff] [blame] | 593 | } |
| 594 | |
Jeff King | b1adb38 | 2018-08-10 19:17:14 -0400 | [diff] [blame] | 595 | static int collect_loose_object(const struct object_id *oid, |
Jeff King | be252d3 | 2023-02-24 01:39:24 -0500 | [diff] [blame] | 596 | const char *path UNUSED, |
Jeff King | b1adb38 | 2018-08-10 19:17:14 -0400 | [diff] [blame] | 597 | void *data) |
Jeff King | 6a95193 | 2015-06-22 06:45:59 -0400 | [diff] [blame] | 598 | { |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 599 | oid_array_append(data, oid); |
Jeff King | 3115ee4 | 2015-06-22 07:06:32 -0400 | [diff] [blame] | 600 | return 0; |
Jeff King | 6a95193 | 2015-06-22 06:45:59 -0400 | [diff] [blame] | 601 | } |
| 602 | |
Jeff King | b1adb38 | 2018-08-10 19:17:14 -0400 | [diff] [blame] | 603 | static int collect_packed_object(const struct object_id *oid, |
Jeff King | be252d3 | 2023-02-24 01:39:24 -0500 | [diff] [blame] | 604 | struct packed_git *pack UNUSED, |
| 605 | uint32_t pos UNUSED, |
Jeff King | b1adb38 | 2018-08-10 19:17:14 -0400 | [diff] [blame] | 606 | void *data) |
Jeff King | 6a95193 | 2015-06-22 06:45:59 -0400 | [diff] [blame] | 607 | { |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 608 | oid_array_append(data, oid); |
Jeff King | 3115ee4 | 2015-06-22 07:06:32 -0400 | [diff] [blame] | 609 | return 0; |
Jeff King | 6a95193 | 2015-06-22 06:45:59 -0400 | [diff] [blame] | 610 | } |
| 611 | |
Jeff King | bf97289 | 2021-10-05 16:38:04 -0400 | [diff] [blame] | 612 | static int batch_unordered_object(const struct object_id *oid, |
| 613 | struct packed_git *pack, off_t offset, |
| 614 | void *vdata) |
Jeff King | 0750bb5 | 2018-08-10 19:24:57 -0400 | [diff] [blame] | 615 | { |
| 616 | struct object_cb_data *data = vdata; |
| 617 | |
Jeff King | ced9fff | 2018-08-14 14:14:27 -0400 | [diff] [blame] | 618 | if (oidset_insert(data->seen, oid)) |
Jeff King | 0750bb5 | 2018-08-10 19:24:57 -0400 | [diff] [blame] | 619 | return 0; |
Jeff King | 0750bb5 | 2018-08-10 19:24:57 -0400 | [diff] [blame] | 620 | |
Jeff King | 818e393 | 2021-10-05 16:36:17 -0400 | [diff] [blame] | 621 | oidcpy(&data->expand->oid, oid); |
Jeff King | bf97289 | 2021-10-05 16:38:04 -0400 | [diff] [blame] | 622 | batch_object_write(NULL, data->scratch, data->opt, data->expand, |
| 623 | pack, offset); |
Jeff King | 818e393 | 2021-10-05 16:36:17 -0400 | [diff] [blame] | 624 | return 0; |
Jeff King | 0750bb5 | 2018-08-10 19:24:57 -0400 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | static int batch_unordered_loose(const struct object_id *oid, |
Jeff King | be252d3 | 2023-02-24 01:39:24 -0500 | [diff] [blame] | 628 | const char *path UNUSED, |
Jeff King | 0750bb5 | 2018-08-10 19:24:57 -0400 | [diff] [blame] | 629 | void *data) |
| 630 | { |
Jeff King | bf97289 | 2021-10-05 16:38:04 -0400 | [diff] [blame] | 631 | return batch_unordered_object(oid, NULL, 0, data); |
Jeff King | 0750bb5 | 2018-08-10 19:24:57 -0400 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | static int batch_unordered_packed(const struct object_id *oid, |
| 635 | struct packed_git *pack, |
| 636 | uint32_t pos, |
| 637 | void *data) |
| 638 | { |
Jeff King | bf97289 | 2021-10-05 16:38:04 -0400 | [diff] [blame] | 639 | return batch_unordered_object(oid, pack, |
| 640 | nth_packed_object_offset(pack, pos), |
| 641 | data); |
Jeff King | 0750bb5 | 2018-08-10 19:24:57 -0400 | [diff] [blame] | 642 | } |
| 643 | |
John Cai | 440c705 | 2022-02-18 18:23:14 +0000 | [diff] [blame] | 644 | typedef void (*parse_cmd_fn_t)(struct batch_options *, const char *, |
| 645 | struct strbuf *, struct expand_data *); |
| 646 | |
| 647 | struct queued_cmd { |
| 648 | parse_cmd_fn_t fn; |
| 649 | char *line; |
| 650 | }; |
| 651 | |
| 652 | static 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 | |
| 661 | static 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 | |
| 670 | static 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 | |
| 687 | static 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 | |
| 698 | static 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 | |
| 708 | static 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 Steinhardt | 3217f52 | 2023-06-06 07:19:41 +0200 | [diff] [blame] | 716 | while (strbuf_getdelim_strip_crlf(&input, stdin, opt->input_delim) != EOF) { |
| 717 | int i; |
John Cai | 440c705 | 2022-02-18 18:23:14 +0000 | [diff] [blame] | 718 | 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ð Bjarmason | d90dafb | 2022-07-01 12:42:54 +0200 | [diff] [blame] | 769 | free_cmds(queued_cmd, &nr); |
John Cai | 440c705 | 2022-02-18 18:23:14 +0000 | [diff] [blame] | 770 | free(queued_cmd); |
| 771 | strbuf_release(&input); |
| 772 | } |
| 773 | |
John Cai | eb54a33 | 2022-03-15 02:40:36 +0000 | [diff] [blame] | 774 | #define DEFAULT_FORMAT "%(objectname) %(objecttype) %(objectsize)" |
| 775 | |
Jeff King | b71bd48 | 2013-07-10 07:38:58 -0400 | [diff] [blame] | 776 | static int batch_objects(struct batch_options *opt) |
Adam Roben | 05d5667 | 2008-04-23 15:17:46 -0400 | [diff] [blame] | 777 | { |
Jeff King | 54d2f0d | 2018-08-14 14:18:06 -0400 | [diff] [blame] | 778 | struct strbuf input = STRBUF_INIT; |
| 779 | struct strbuf output = STRBUF_INIT; |
Jeff King | 93d2a60 | 2013-07-10 07:45:47 -0400 | [diff] [blame] | 780 | struct expand_data data; |
Jeff King | a42fcd1 | 2014-03-12 16:05:43 -0400 | [diff] [blame] | 781 | int save_warning; |
Jeff King | 07e2383 | 2014-01-07 17:10:15 -0500 | [diff] [blame] | 782 | int retval = 0; |
Jeff King | 93d2a60 | 2013-07-10 07:45:47 -0400 | [diff] [blame] | 783 | |
Jeff King | 93d2a60 | 2013-07-10 07:45:47 -0400 | [diff] [blame] | 784 | /* |
| 785 | * Expand once with our special mark_query flag, which will prime the |
Jeff King | c93206b | 2019-01-07 03:34:12 -0500 | [diff] [blame] | 786 | * object_info to be handed to oid_object_info_extended for each |
Jeff King | 93d2a60 | 2013-07-10 07:45:47 -0400 | [diff] [blame] | 787 | * object. |
| 788 | */ |
| 789 | memset(&data, 0, sizeof(data)); |
| 790 | data.mark_query = 1; |
René Scharfe | 6f1e2d5 | 2023-06-17 22:43:17 +0200 | [diff] [blame] | 791 | expand_format(&output, |
John Cai | eb54a33 | 2022-03-15 02:40:36 +0000 | [diff] [blame] | 792 | opt->format ? opt->format : DEFAULT_FORMAT, |
John Cai | eb54a33 | 2022-03-15 02:40:36 +0000 | [diff] [blame] | 793 | &data); |
Jeff King | 93d2a60 | 2013-07-10 07:45:47 -0400 | [diff] [blame] | 794 | data.mark_query = 0; |
Jeff King | 54d2f0d | 2018-08-14 14:18:06 -0400 | [diff] [blame] | 795 | strbuf_release(&output); |
John Cai | a2c7552 | 2022-02-18 18:23:11 +0000 | [diff] [blame] | 796 | if (opt->transform_mode) |
Johannes Schindelin | 3214594 | 2016-09-09 12:10:54 +0200 | [diff] [blame] | 797 | data.split_on_whitespace = 1; |
Adam Roben | 05d5667 | 2008-04-23 15:17:46 -0400 | [diff] [blame] | 798 | |
John Cai | eb54a33 | 2022-03-15 02:40:36 +0000 | [diff] [blame] | 799 | if (opt->format && !strcmp(opt->format, DEFAULT_FORMAT)) |
| 800 | opt->format = NULL; |
Jeff King | 25fba78 | 2013-07-12 02:20:05 -0400 | [diff] [blame] | 801 | /* |
Jeff King | 6554dfa | 2013-12-12 07:15:50 +0800 | [diff] [blame] | 802 | * 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 Cai | ac4e58c | 2022-02-18 18:23:12 +0000 | [diff] [blame] | 805 | if (opt->batch_mode == BATCH_MODE_CONTENTS) |
Jeff King | 6554dfa | 2013-12-12 07:15:50 +0800 | [diff] [blame] | 806 | data.info.typep = &data.type; |
| 807 | |
Jeff King | 6a95193 | 2015-06-22 06:45:59 -0400 | [diff] [blame] | 808 | if (opt->all_objects) { |
ZheNing Hu | ee02ac6 | 2021-06-03 16:29:26 +0000 | [diff] [blame] | 809 | struct object_cb_data cb; |
ZheNing Hu | e16acc8 | 2021-06-03 16:29:25 +0000 | [diff] [blame] | 810 | struct object_info empty = OBJECT_INFO_INIT; |
| 811 | |
| 812 | if (!memcmp(&data.info, &empty, sizeof(empty))) |
| 813 | data.skip_object_info = 1; |
Jeff King | 3115ee4 | 2015-06-22 07:06:32 -0400 | [diff] [blame] | 814 | |
Ævar Arnfjörð Bjarmason | a5183d7 | 2023-03-28 15:58:53 +0200 | [diff] [blame] | 815 | if (repo_has_promisor_remote(the_repository)) |
Christian Couder | b14ed5a | 2019-06-25 15:40:31 +0200 | [diff] [blame] | 816 | warning("This repository uses promisor remotes. Some objects may not be loaded."); |
Jeff King | 3115ee4 | 2015-06-22 07:06:32 -0400 | [diff] [blame] | 817 | |
Derrick Stolee | d24eda4 | 2023-06-06 13:24:35 +0000 | [diff] [blame] | 818 | disable_replace_refs(); |
Jeff King | 5c5b29b | 2021-10-05 16:36:07 -0400 | [diff] [blame] | 819 | |
Jeff King | 6a95193 | 2015-06-22 06:45:59 -0400 | [diff] [blame] | 820 | cb.opt = opt; |
| 821 | cb.expand = &data; |
Jeff King | 79ed0a5 | 2018-08-14 14:20:22 -0400 | [diff] [blame] | 822 | cb.scratch = &output; |
Jeff King | 3115ee4 | 2015-06-22 07:06:32 -0400 | [diff] [blame] | 823 | |
Jeff King | 0750bb5 | 2018-08-10 19:24:57 -0400 | [diff] [blame] | 824 | 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 King | 79ed0a5 | 2018-08-14 14:20:22 -0400 | [diff] [blame] | 845 | strbuf_release(&output); |
Jeff King | 6a95193 | 2015-06-22 06:45:59 -0400 | [diff] [blame] | 846 | return 0; |
| 847 | } |
| 848 | |
Jeff King | 6554dfa | 2013-12-12 07:15:50 +0800 | [diff] [blame] | 849 | /* |
Jeff King | 25fba78 | 2013-07-12 02:20:05 -0400 | [diff] [blame] | 850 | * 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 King | a42fcd1 | 2014-03-12 16:05:43 -0400 | [diff] [blame] | 856 | save_warning = warn_on_object_refname_ambiguity; |
Jeff King | 25fba78 | 2013-07-12 02:20:05 -0400 | [diff] [blame] | 857 | warn_on_object_refname_ambiguity = 0; |
| 858 | |
John Cai | 440c705 | 2022-02-18 18:23:14 +0000 | [diff] [blame] | 859 | if (opt->batch_mode == BATCH_MODE_QUEUE_AND_DISPATCH) { |
| 860 | batch_objects_command(opt, &output, &data); |
| 861 | goto cleanup; |
| 862 | } |
| 863 | |
Patrick Steinhardt | 3217f52 | 2023-06-06 07:19:41 +0200 | [diff] [blame] | 864 | while (strbuf_getdelim_strip_crlf(&input, stdin, opt->input_delim) != EOF) { |
Jeff King | 97be040 | 2013-08-02 04:59:07 -0700 | [diff] [blame] | 865 | 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 King | 54d2f0d | 2018-08-14 14:18:06 -0400 | [diff] [blame] | 871 | char *p = strpbrk(input.buf, " \t"); |
Jeff King | 97be040 | 2013-08-02 04:59:07 -0700 | [diff] [blame] | 872 | if (p) { |
| 873 | while (*p && strchr(" \t", *p)) |
| 874 | *p++ = '\0'; |
| 875 | } |
| 876 | data.rest = p; |
| 877 | } |
| 878 | |
Jeff King | 79ed0a5 | 2018-08-14 14:20:22 -0400 | [diff] [blame] | 879 | batch_one_object(input.buf, &output, opt, &data); |
Adam Roben | 05d5667 | 2008-04-23 15:17:46 -0400 | [diff] [blame] | 880 | } |
| 881 | |
John Cai | 440c705 | 2022-02-18 18:23:14 +0000 | [diff] [blame] | 882 | cleanup: |
Jeff King | 54d2f0d | 2018-08-14 14:18:06 -0400 | [diff] [blame] | 883 | strbuf_release(&input); |
Jeff King | 79ed0a5 | 2018-08-14 14:20:22 -0400 | [diff] [blame] | 884 | strbuf_release(&output); |
Jeff King | a42fcd1 | 2014-03-12 16:05:43 -0400 | [diff] [blame] | 885 | warn_on_object_refname_ambiguity = save_warning; |
Jeff King | 07e2383 | 2014-01-07 17:10:15 -0500 | [diff] [blame] | 886 | return retval; |
Adam Roben | 05d5667 | 2008-04-23 15:17:46 -0400 | [diff] [blame] | 887 | } |
| 888 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 889 | static int git_cat_file_config(const char *var, const char *value, |
| 890 | const struct config_context *ctx, void *cb) |
Clément Poulain | e5fba60 | 2010-06-15 17:50:28 +0200 | [diff] [blame] | 891 | { |
Jeff King | 6680a08 | 2012-02-07 13:23:02 -0500 | [diff] [blame] | 892 | if (userdiff_config(var, value) < 0) |
Clément Poulain | e5fba60 | 2010-06-15 17:50:28 +0200 | [diff] [blame] | 893 | return -1; |
Clément Poulain | e5fba60 | 2010-06-15 17:50:28 +0200 | [diff] [blame] | 894 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 895 | return git_default_config(var, value, ctx, cb); |
Clément Poulain | e5fba60 | 2010-06-15 17:50:28 +0200 | [diff] [blame] | 896 | } |
| 897 | |
Jeff King | b71bd48 | 2013-07-10 07:38:58 -0400 | [diff] [blame] | 898 | static 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 King | 517fe80 | 2018-11-05 01:45:42 -0500 | [diff] [blame] | 904 | BUG_ON_OPT_NEG(unset); |
| 905 | |
David Turner | 122d534 | 2015-05-20 13:03:40 -0400 | [diff] [blame] | 906 | if (bo->enabled) { |
Jeff King | 0eb8d37 | 2018-11-05 01:43:44 -0500 | [diff] [blame] | 907 | return error(_("only one batch option may be specified")); |
Jeff King | b71bd48 | 2013-07-10 07:38:58 -0400 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | bo->enabled = 1; |
John Cai | ac4e58c | 2022-02-18 18:23:12 +0000 | [diff] [blame] | 911 | |
| 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 Cai | 440c705 | 2022-02-18 18:23:14 +0000 | [diff] [blame] | 916 | else if (!strcmp(opt->long_name, "batch-command")) |
| 917 | bo->batch_mode = BATCH_MODE_QUEUE_AND_DISPATCH; |
John Cai | ac4e58c | 2022-02-18 18:23:12 +0000 | [diff] [blame] | 918 | else |
| 919 | BUG("%s given to batch-option-callback", opt->long_name); |
| 920 | |
Jeff King | 93d2a60 | 2013-07-10 07:45:47 -0400 | [diff] [blame] | 921 | bo->format = arg; |
Jeff King | b71bd48 | 2013-07-10 07:38:58 -0400 | [diff] [blame] | 922 | |
| 923 | return 0; |
| 924 | } |
| 925 | |
John Cai | 9b1cb50 | 2024-09-13 21:16:14 +0000 | [diff] [blame] | 926 | int cmd_cat_file(int argc, |
| 927 | const char **argv, |
| 928 | const char *prefix, |
| 929 | struct repository *repo UNUSED) |
Adam Roben | 9cf71b1 | 2008-04-23 15:17:44 -0400 | [diff] [blame] | 930 | { |
Jeff King | b71bd48 | 2013-07-10 07:38:58 -0400 | [diff] [blame] | 931 | int opt = 0; |
Ævar Arnfjörð Bjarmason | b3fe468 | 2021-12-28 14:28:47 +0100 | [diff] [blame] | 932 | int opt_cw = 0; |
| 933 | int opt_epts = 0; |
Adam Roben | 4814dbe | 2008-04-23 15:17:45 -0400 | [diff] [blame] | 934 | const char *exp_type = NULL, *obj_name = NULL; |
Jeff King | b71bd48 | 2013-07-10 07:38:58 -0400 | [diff] [blame] | 935 | struct batch_options batch = {0}; |
Karthik Nayak | 39e4ae3 | 2015-05-03 20:00:01 +0530 | [diff] [blame] | 936 | int unknown_type = 0; |
Patrick Steinhardt | 3217f52 | 2023-06-06 07:19:41 +0200 | [diff] [blame] | 937 | int input_nul_terminated = 0; |
Patrick Steinhardt | f79e188 | 2023-06-06 07:19:45 +0200 | [diff] [blame] | 938 | int nul_terminated = 0; |
Adam Roben | 9cf71b1 | 2008-04-23 15:17:44 -0400 | [diff] [blame] | 939 | |
Ævar Arnfjörð Bjarmason | 5a40417 | 2021-12-28 14:28:45 +0100 | [diff] [blame] | 940 | const char * const usage[] = { |
| 941 | N_("git cat-file <type> <object>"), |
| 942 | N_("git cat-file (-e | -p) <object>"), |
Ævar Arnfjörð Bjarmason | 83dc443 | 2022-01-10 23:08:45 +0100 | [diff] [blame] | 943 | N_("git cat-file (-t | -s) [--allow-unknown-type] <object>"), |
Štěpán Němec | cebfaaa | 2023-10-09 19:56:04 +0200 | [diff] [blame] | 944 | N_("git cat-file (--textconv | --filters)\n" |
| 945 | " [<rev>:<path|tree-ish> | --path=<path|tree-ish> <rev>]"), |
John Cai | 440c705 | 2022-02-18 18:23:14 +0000 | [diff] [blame] | 946 | N_("git cat-file (--batch | --batch-check | --batch-command) [--batch-all-objects]\n" |
Ævar Arnfjörð Bjarmason | 5a40417 | 2021-12-28 14:28:45 +0100 | [diff] [blame] | 947 | " [--buffer] [--follow-symlinks] [--unordered]\n" |
Patrick Steinhardt | f79e188 | 2023-06-06 07:19:45 +0200 | [diff] [blame] | 948 | " [--textconv | --filters] [-Z]"), |
Ævar Arnfjörð Bjarmason | 5a40417 | 2021-12-28 14:28:45 +0100 | [diff] [blame] | 949 | NULL |
| 950 | }; |
Michele Ballabio | 15d8e56 | 2008-05-23 16:19:42 +0200 | [diff] [blame] | 951 | const struct option options[] = { |
Ævar Arnfjörð Bjarmason | 57d6a1c | 2021-12-28 14:28:48 +0100 | [diff] [blame] | 952 | /* Simple queries */ |
| 953 | OPT_GROUP(N_("Check object existence or emit object contents")), |
Karthik Nayak | b48158a | 2015-05-03 20:00:00 +0530 | [diff] [blame] | 954 | OPT_CMDMODE('e', NULL, &opt, |
Ævar Arnfjörð Bjarmason | 57d6a1c | 2021-12-28 14:28:48 +0100 | [diff] [blame] | 955 | 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 King | ad42f28 | 2015-06-22 06:40:56 -0400 | [diff] [blame] | 961 | OPT_BOOL(0, "allow-unknown-type", &unknown_type, |
Karthik Nayak | 39e4ae3 | 2015-05-03 20:00:01 +0530 | [diff] [blame] | 962 | N_("allow -s and -t to work with broken/corrupt objects")), |
Siddharth Asthana | ec031da | 2022-07-19 01:21:02 +0530 | [diff] [blame] | 963 | OPT_BOOL(0, "use-mailmap", &use_mailmap, N_("use mail map file")), |
| 964 | OPT_ALIAS(0, "mailmap", "use-mailmap"), |
Ævar Arnfjörð Bjarmason | 57d6a1c | 2021-12-28 14:28:48 +0100 | [diff] [blame] | 965 | /* 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 Steinhardt | f79e188 | 2023-06-06 07:19:45 +0200 | [diff] [blame] | 975 | 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 Cai | 440c705 | 2022-02-18 18:23:14 +0000 | [diff] [blame] | 978 | 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ð Bjarmason | 57d6a1c | 2021-12-28 14:28:48 +0100 | [diff] [blame] | 982 | 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 King | fc4937c | 2015-06-22 06:45:17 -0400 | [diff] [blame] | 986 | OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")), |
David Turner | 122d534 | 2015-05-20 13:03:40 -0400 | [diff] [blame] | 987 | OPT_BOOL(0, "follow-symlinks", &batch.follow_symlinks, |
Ævar Arnfjörð Bjarmason | 57d6a1c | 2021-12-28 14:28:48 +0100 | [diff] [blame] | 988 | N_("follow in-tree symlinks")), |
Jeff King | 0750bb5 | 2018-08-10 19:24:57 -0400 | [diff] [blame] | 989 | OPT_BOOL(0, "unordered", &batch.unordered, |
Ævar Arnfjörð Bjarmason | 57d6a1c | 2021-12-28 14:28:48 +0100 | [diff] [blame] | 990 | 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ð Bjarmason | 83dc443 | 2022-01-10 23:08:45 +0100 | [diff] [blame] | 998 | N_("use a <path> for (--textconv | --filters); Not with 'batch'")), |
Michele Ballabio | 15d8e56 | 2008-05-23 16:19:42 +0200 | [diff] [blame] | 999 | OPT_END() |
| 1000 | }; |
| 1001 | |
Clément Poulain | e5fba60 | 2010-06-15 17:50:28 +0200 | [diff] [blame] | 1002 | git_config(git_cat_file_config, NULL); |
Adam Roben | 9cf71b1 | 2008-04-23 15:17:44 -0400 | [diff] [blame] | 1003 | |
Jeff King | 6a36e1e | 2016-05-18 12:56:14 -0400 | [diff] [blame] | 1004 | batch.buffer_output = -1; |
Adam Roben | a8128ed | 2008-04-23 15:17:47 -0400 | [diff] [blame] | 1005 | |
Ævar Arnfjörð Bjarmason | 485fd2c | 2021-12-28 14:28:46 +0100 | [diff] [blame] | 1006 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
Ævar Arnfjörð Bjarmason | b3fe468 | 2021-12-28 14:28:47 +0100 | [diff] [blame] | 1007 | opt_cw = (opt == 'c' || opt == 'w'); |
| 1008 | opt_epts = (opt == 'e' || opt == 'p' || opt == 't' || opt == 's'); |
| 1009 | |
Siddharth Asthana | ec031da | 2022-07-19 01:21:02 +0530 | [diff] [blame] | 1010 | if (use_mailmap) |
| 1011 | read_mailmap(&mailmap); |
| 1012 | |
Ævar Arnfjörð Bjarmason | b3fe468 | 2021-12-28 14:28:47 +0100 | [diff] [blame] | 1013 | /* --batch-all-objects? */ |
| 1014 | if (opt == 'b') |
Ævar Arnfjörð Bjarmason | 485fd2c | 2021-12-28 14:28:46 +0100 | [diff] [blame] | 1015 | batch.all_objects = 1; |
Adam Roben | 9cf71b1 | 2008-04-23 15:17:44 -0400 | [diff] [blame] | 1016 | |
Ævar Arnfjörð Bjarmason | b3fe468 | 2021-12-28 14:28:47 +0100 | [diff] [blame] | 1017 | /* 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 Turner | 122d534 | 2015-05-20 13:03:40 -0400 | [diff] [blame] | 1023 | |
Ævar Arnfjörð Bjarmason | b3fe468 | 2021-12-28 14:28:47 +0100 | [diff] [blame] | 1024 | /* 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ð Bjarmason | 5fb2490 | 2022-01-10 23:08:46 +0100 | [diff] [blame] | 1029 | "--follow-symlinks"); |
Ævar Arnfjörð Bjarmason | b3fe468 | 2021-12-28 14:28:47 +0100 | [diff] [blame] | 1030 | 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 Steinhardt | 3217f52 | 2023-06-06 07:19:41 +0200 | [diff] [blame] | 1036 | else if (input_nul_terminated) |
Taylor Blau | db9d67f | 2022-07-22 19:29:05 -0400 | [diff] [blame] | 1037 | usage_msg_optf(_("'%s' requires a batch mode"), usage, options, |
| 1038 | "-z"); |
Patrick Steinhardt | f79e188 | 2023-06-06 07:19:45 +0200 | [diff] [blame] | 1039 | else if (nul_terminated) |
| 1040 | usage_msg_optf(_("'%s' requires a batch mode"), usage, options, |
| 1041 | "-Z"); |
Johannes Schindelin | 7bcf341 | 2016-09-09 12:10:50 +0200 | [diff] [blame] | 1042 | |
Patrick Steinhardt | f79e188 | 2023-06-06 07:19:45 +0200 | [diff] [blame] | 1043 | 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 Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 1048 | |
Ævar Arnfjörð Bjarmason | b3fe468 | 2021-12-28 14:28:47 +0100 | [diff] [blame] | 1049 | /* Batch defaults */ |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 1050 | if (batch.buffer_output < 0) |
| 1051 | batch.buffer_output = batch.all_objects; |
| 1052 | |
Kevin Lyles | e65b0c7 | 2024-09-03 22:06:47 +0000 | [diff] [blame] | 1053 | prepare_repo_settings(the_repository); |
| 1054 | the_repository->settings.command_requires_full_index = 0; |
| 1055 | |
Ævar Arnfjörð Bjarmason | b3fe468 | 2021-12-28 14:28:47 +0100 | [diff] [blame] | 1056 | /* Return early if we're in batch mode? */ |
| 1057 | if (batch.enabled) { |
| 1058 | if (opt_cw) |
John Cai | a2c7552 | 2022-02-18 18:23:11 +0000 | [diff] [blame] | 1059 | batch.transform_mode = opt; |
Ævar Arnfjörð Bjarmason | b3fe468 | 2021-12-28 14:28:47 +0100 | [diff] [blame] | 1060 | 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 Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 1067 | return batch_objects(&batch); |
Ævar Arnfjörð Bjarmason | b3fe468 | 2021-12-28 14:28:47 +0100 | [diff] [blame] | 1068 | } |
| 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 Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 1093 | |
| 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 | } |