Merge branch 'gr/rebase-i-drop-warn'
[gitweb.git] / builtin / cat-file.c
index 7d99c157a16f18c0d0d34197804c439575e940ec..07baad1e59c1977e5fce04d4c7288f8263947f4e 100644 (file)
@@ -9,12 +9,14 @@
 #include "userdiff.h"
 #include "streaming.h"
 #include "tree-walk.h"
+#include "sha1-array.h"
 
 struct batch_options {
        int enabled;
        int follow_symlinks;
        int print_contents;
        int buffer_output;
+       int all_objects;
        const char *format;
 };
 
@@ -251,10 +253,31 @@ static void print_object_or_die(struct batch_options *opt, struct expand_data *d
        }
 }
 
+static void batch_object_write(const char *obj_name, struct batch_options *opt,
+                              struct expand_data *data)
+{
+       struct strbuf buf = STRBUF_INIT;
+
+       if (sha1_object_info_extended(data->sha1, &data->info, LOOKUP_REPLACE_OBJECT) < 0) {
+               printf("%s missing\n", obj_name ? obj_name : sha1_to_hex(data->sha1));
+               fflush(stdout);
+               return;
+       }
+
+       strbuf_expand(&buf, opt->format, expand_format, data);
+       strbuf_addch(&buf, '\n');
+       batch_write(opt, buf.buf, buf.len);
+       strbuf_release(&buf);
+
+       if (opt->print_contents) {
+               print_object_or_die(opt, data);
+               batch_write(opt, "\n", 1);
+       }
+}
+
 static void batch_one_object(const char *obj_name, struct batch_options *opt,
                             struct expand_data *data)
 {
-       struct strbuf buf = STRBUF_INIT;
        struct object_context ctx;
        int flags = opt->follow_symlinks ? GET_SHA1_FOLLOW_SYMLINKS : 0;
        enum follow_symlinks_result result;
@@ -294,21 +317,36 @@ static void batch_one_object(const char *obj_name, struct batch_options *opt,
                return;
        }
 
-       if (sha1_object_info_extended(data->sha1, &data->info, LOOKUP_REPLACE_OBJECT) < 0) {
-               printf("%s missing\n", obj_name);
-               fflush(stdout);
-               return;
-       }
+       batch_object_write(obj_name, opt, data);
+}
 
-       strbuf_expand(&buf, opt->format, expand_format, data);
-       strbuf_addch(&buf, '\n');
-       batch_write(opt, buf.buf, buf.len);
-       strbuf_release(&buf);
+struct object_cb_data {
+       struct batch_options *opt;
+       struct expand_data *expand;
+};
 
-       if (opt->print_contents) {
-               print_object_or_die(opt, data);
-               batch_write(opt, "\n", 1);
-       }
+static void batch_object_cb(const unsigned char sha1[20], void *vdata)
+{
+       struct object_cb_data *data = vdata;
+       hashcpy(data->expand->sha1, sha1);
+       batch_object_write(NULL, data->opt, data->expand);
+}
+
+static int batch_loose_object(const unsigned char *sha1,
+                             const char *path,
+                             void *data)
+{
+       sha1_array_append(data, sha1);
+       return 0;
+}
+
+static int batch_packed_object(const unsigned char *sha1,
+                              struct packed_git *pack,
+                              uint32_t pos,
+                              void *data)
+{
+       sha1_array_append(data, sha1);
+       return 0;
 }
 
 static int batch_objects(struct batch_options *opt)
@@ -338,6 +376,21 @@ static int batch_objects(struct batch_options *opt)
        if (opt->print_contents)
                data.info.typep = &data.type;
 
+       if (opt->all_objects) {
+               struct sha1_array sa = SHA1_ARRAY_INIT;
+               struct object_cb_data cb;
+
+               for_each_loose_object(batch_loose_object, &sa, 0);
+               for_each_packed_object(batch_packed_object, &sa, 0);
+
+               cb.opt = opt;
+               cb.expand = &data;
+               sha1_array_for_each_unique(&sa, batch_object_cb, &cb);
+
+               sha1_array_clear(&sa);
+               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
@@ -429,6 +482,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")),
                OPT_END()
        };
 
@@ -453,7 +508,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);
        }