path: root/builtin/cat-file.c
diff options
authorJeff King <>2015-06-22 10:45:59 (GMT)
committerJunio C Hamano <>2015-06-22 21:55:52 (GMT)
commit6a951937ae1abb5fe438bfb41ebb28c5abe0419d (patch)
tree0661a045de2c3d3753209985cd132f57c30627af /builtin/cat-file.c
parent44b877e9bc944258db096a0ec57151be7c8cbf66 (diff)
cat-file: add --batch-all-objects option
It can sometimes be useful to examine all objects in the repository. Normally this is done with "git rev-list --all --objects", but: 1. That shows only reachable objects. You may want to look at all available objects. 2. It's slow. We actually open each object to walk the graph. If your operation is OK with seeing unreachable objects, it's an order of magnitude faster to just enumerate the loose directories and pack indices. You can do this yourself using "ls" and "git show-index", but it's non-obvious. This patch adds an option to "cat-file --batch-check" to operate on all available objects (rather than reading names from stdin). This is based on a proposal by Charles Bailey to provide a separate "git list-all-objects" command. That is more orthogonal, as it splits enumerating the objects from getting information about them. However, in practice you will either: a. Feed the list of objects directly into cat-file anyway, so you can find out information about them. Keeping it in a single process is more efficient. b. Ask the listing process to start telling you more information about the objects, in which case you will reinvent cat-file's batch-check formatter. Adding a cat-file option is simple and efficient. And if you really do want just the object names, you can always do: git cat-file --batch-check='%(objectname)' --batch-all-objects Signed-off-by: Jeff King <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'builtin/cat-file.c')
1 files changed, 42 insertions, 2 deletions
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index 499ccda..95604c4 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -15,6 +15,7 @@ struct batch_options {
int follow_symlinks;
int print_contents;
int buffer_output;
+ int all_objects;
const char *format;
@@ -257,7 +258,7 @@ static void batch_object_write(const char *obj_name, struct batch_options *opt,
struct strbuf buf = STRBUF_INIT;
if (sha1_object_info_extended(data->sha1, &data->info, LOOKUP_REPLACE_OBJECT) < 0) {
- printf("%s missing\n", obj_name);
+ printf("%s missing\n", obj_name ? obj_name : sha1_to_hex(data->sha1));
@@ -318,6 +319,34 @@ static void batch_one_object(const char *obj_name, struct batch_options *opt,
batch_object_write(obj_name, opt, data);
+struct object_cb_data {
+ struct batch_options *opt;
+ struct expand_data *expand;
+static int batch_object_cb(const unsigned char *sha1,
+ struct object_cb_data *data)
+ hashcpy(data->expand->sha1, sha1);
+ batch_object_write(NULL, data->opt, data->expand);
+ return 0;
+static int batch_loose_object(const unsigned char *sha1,
+ const char *path,
+ void *data)
+ return batch_object_cb(sha1, data);
+static int batch_packed_object(const unsigned char *sha1,
+ struct packed_git *pack,
+ uint32_t pos,
+ void *data)
+ return batch_object_cb(sha1, data);
static int batch_objects(struct batch_options *opt)
struct strbuf buf = STRBUF_INIT;
@@ -345,6 +374,15 @@ static int batch_objects(struct batch_options *opt)
if (opt->print_contents) = &data.type;
+ if (opt->all_objects) {
+ struct object_cb_data cb;
+ cb.opt = opt;
+ cb.expand = &data;
+ for_each_loose_object(batch_loose_object, &cb, 0);
+ for_each_packed_object(batch_packed_object, &cb, 0);
+ return 0;
+ }
* We are going to call get_sha1 on a potentially very large number of
* objects. In most large cases, these will be actual object sha1s. The
@@ -436,6 +474,8 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
PARSE_OPT_OPTARG, batch_option_callback },
OPT_BOOL(0, "follow-symlinks", &batch.follow_symlinks,
N_("follow in-tree symlinks (used with --batch or --batch-check)")),
+ OPT_BOOL(0, "batch-all-objects", &batch.all_objects,
+ N_("show all objects with --batch or --batch-check")),
@@ -460,7 +500,7 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
usage_with_options(cat_file_usage, options);
- if (batch.follow_symlinks && !batch.enabled) {
+ if ((batch.follow_symlinks || batch.all_objects) && !batch.enabled) {
usage_with_options(cat_file_usage, options);