notes-merge: convert struct notes_merge_pair to struct object_id
[gitweb.git] / builtin / cat-file.c
index 54db1184a003897294b6aaa46bc6bf29faa919bc..49b8fa8e22cc0607528cce0461682dee081389c4 100644 (file)
@@ -23,7 +23,7 @@ struct batch_options {
 static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
                        int unknown_type)
 {
-       unsigned char sha1[20];
+       struct object_id oid;
        enum object_type type;
        char *buf;
        unsigned long size;
@@ -35,14 +35,14 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
        if (unknown_type)
                flags |= LOOKUP_UNKNOWN_OBJECT;
 
-       if (get_sha1_with_context(obj_name, 0, sha1, &obj_context))
+       if (get_sha1_with_context(obj_name, 0, oid.hash, &obj_context))
                die("Not a valid object name %s", obj_name);
 
        buf = NULL;
        switch (opt) {
        case 't':
                oi.typename = &sb;
-               if (sha1_object_info_extended(sha1, &oi, flags) < 0)
+               if (sha1_object_info_extended(oid.hash, &oi, flags) < 0)
                        die("git cat-file: could not get object info");
                if (sb.len) {
                        printf("%s\n", sb.buf);
@@ -53,24 +53,24 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
 
        case 's':
                oi.sizep = &size;
-               if (sha1_object_info_extended(sha1, &oi, flags) < 0)
+               if (sha1_object_info_extended(oid.hash, &oi, flags) < 0)
                        die("git cat-file: could not get object info");
                printf("%lu\n", size);
                return 0;
 
        case 'e':
-               return !has_sha1_file(sha1);
+               return !has_object_file(&oid);
 
        case 'c':
                if (!obj_context.path[0])
                        die("git cat-file --textconv %s: <object> must be <sha1:path>",
                            obj_name);
 
-               if (textconv_object(obj_context.path, obj_context.mode, sha1, 1, &buf, &size))
+               if (textconv_object(obj_context.path, obj_context.mode, &oid, 1, &buf, &size))
                        break;
 
        case 'p':
-               type = sha1_object_info(sha1, NULL);
+               type = sha1_object_info(oid.hash, NULL);
                if (type < 0)
                        die("Not a valid object name %s", obj_name);
 
@@ -83,8 +83,8 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
                }
 
                if (type == OBJ_BLOB)
-                       return stream_blob_to_fd(1, sha1, NULL, 0);
-               buf = read_sha1_file(sha1, &type, &size);
+                       return stream_blob_to_fd(1, &oid, NULL, 0);
+               buf = read_sha1_file(oid.hash, &type, &size);
                if (!buf)
                        die("Cannot read object %s", obj_name);
 
@@ -93,19 +93,19 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
 
        case 0:
                if (type_from_string(exp_type) == OBJ_BLOB) {
-                       unsigned char blob_sha1[20];
-                       if (sha1_object_info(sha1, NULL) == OBJ_TAG) {
-                               char *buffer = read_sha1_file(sha1, &type, &size);
+                       struct object_id blob_oid;
+                       if (sha1_object_info(oid.hash, NULL) == OBJ_TAG) {
+                               char *buffer = read_sha1_file(oid.hash, &type, &size);
                                const char *target;
                                if (!skip_prefix(buffer, "object ", &target) ||
-                                   get_sha1_hex(target, blob_sha1))
-                                       die("%s not a valid tag", sha1_to_hex(sha1));
+                                   get_oid_hex(target, &blob_oid))
+                                       die("%s not a valid tag", oid_to_hex(&oid));
                                free(buffer);
                        } else
-                               hashcpy(blob_sha1, sha1);
+                               oidcpy(&blob_oid, &oid);
 
-                       if (sha1_object_info(blob_sha1, NULL) == OBJ_BLOB)
-                               return stream_blob_to_fd(1, blob_sha1, NULL, 0);
+                       if (sha1_object_info(blob_oid.hash, NULL) == OBJ_BLOB)
+                               return stream_blob_to_fd(1, &blob_oid, NULL, 0);
                        /*
                         * we attempted to dereference a tag to a blob
                         * and failed; there may be new dereference
@@ -113,7 +113,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
                         * fall-back to the usual case.
                         */
                }
-               buf = read_object_with_reference(sha1, exp_type, &size, NULL);
+               buf = read_object_with_reference(oid.hash, exp_type, &size, NULL);
                break;
 
        default:
@@ -128,12 +128,12 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
 }
 
 struct expand_data {
-       unsigned char sha1[20];
+       struct object_id oid;
        enum object_type type;
        unsigned long size;
-       unsigned long disk_size;
+       off_t disk_size;
        const char *rest;
-       unsigned char delta_base_sha1[20];
+       struct object_id delta_base_oid;
 
        /*
         * If mark_query is true, we do not expand anything, but rather
@@ -154,6 +154,13 @@ struct expand_data {
         * elements above, so you can retrieve the response from there.
         */
        struct object_info info;
+
+       /*
+        * This flag will be true if the requested batch format and options
+        * don't require us to call sha1_object_info, which can then be
+        * optimized out.
+        */
+       unsigned skip_object_info : 1;
 };
 
 static int is_atom(const char *atom, const char *s, int slen)
@@ -169,7 +176,7 @@ static void expand_atom(struct strbuf *sb, const char *atom, int len,
 
        if (is_atom("objectname", atom, len)) {
                if (!data->mark_query)
-                       strbuf_addstr(sb, sha1_to_hex(data->sha1));
+                       strbuf_addstr(sb, oid_to_hex(&data->oid));
        } else if (is_atom("objecttype", atom, len)) {
                if (data->mark_query)
                        data->info.typep = &data->type;
@@ -184,7 +191,7 @@ static void expand_atom(struct strbuf *sb, const char *atom, int len,
                if (data->mark_query)
                        data->info.disk_sizep = &data->disk_size;
                else
-                       strbuf_addf(sb, "%lu", data->disk_size);
+                       strbuf_addf(sb, "%"PRIuMAX, (uintmax_t)data->disk_size);
        } else if (is_atom("rest", atom, len)) {
                if (data->mark_query)
                        data->split_on_whitespace = 1;
@@ -192,9 +199,10 @@ static void expand_atom(struct strbuf *sb, const char *atom, int len,
                        strbuf_addstr(sb, data->rest);
        } else if (is_atom("deltabase", atom, len)) {
                if (data->mark_query)
-                       data->info.delta_base_sha1 = data->delta_base_sha1;
+                       data->info.delta_base_sha1 = data->delta_base_oid.hash;
                else
-                       strbuf_addstr(sb, sha1_to_hex(data->delta_base_sha1));
+                       strbuf_addstr(sb,
+                                     oid_to_hex(&data->delta_base_oid));
        } else
                die("unknown format element: %.*s", len, atom);
 }
@@ -225,28 +233,28 @@ static void batch_write(struct batch_options *opt, const void *data, int len)
 
 static void print_object_or_die(struct batch_options *opt, struct expand_data *data)
 {
-       const unsigned char *sha1 = data->sha1;
+       const struct object_id *oid = &data->oid;
 
        assert(data->info.typep);
 
        if (data->type == OBJ_BLOB) {
                if (opt->buffer_output)
                        fflush(stdout);
-               if (stream_blob_to_fd(1, sha1, NULL, 0) < 0)
-                       die("unable to stream %s to stdout", sha1_to_hex(sha1));
+               if (stream_blob_to_fd(1, oid, NULL, 0) < 0)
+                       die("unable to stream %s to stdout", oid_to_hex(oid));
        }
        else {
                enum object_type type;
                unsigned long size;
                void *contents;
 
-               contents = read_sha1_file(sha1, &type, &size);
+               contents = read_sha1_file(oid->hash, &type, &size);
                if (!contents)
-                       die("object %s disappeared", sha1_to_hex(sha1));
+                       die("object %s disappeared", oid_to_hex(oid));
                if (type != data->type)
-                       die("object %s changed type!?", sha1_to_hex(sha1));
+                       die("object %s changed type!?", oid_to_hex(oid));
                if (data->info.sizep && size != data->size)
-                       die("object %s changed size!?", sha1_to_hex(sha1));
+                       die("object %s changed size!?", oid_to_hex(oid));
 
                batch_write(opt, contents, size);
                free(contents);
@@ -258,8 +266,10 @@ 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 ? obj_name : sha1_to_hex(data->sha1));
+       if (!data->skip_object_info &&
+           sha1_object_info_extended(data->oid.hash, &data->info, LOOKUP_REPLACE_OBJECT) < 0) {
+               printf("%s missing\n",
+                      obj_name ? obj_name : oid_to_hex(&data->oid));
                fflush(stdout);
                return;
        }
@@ -282,7 +292,7 @@ static void batch_one_object(const char *obj_name, struct batch_options *opt,
        int flags = opt->follow_symlinks ? GET_SHA1_FOLLOW_SYMLINKS : 0;
        enum follow_symlinks_result result;
 
-       result = get_sha1_with_context(obj_name, flags, data->sha1, &ctx);
+       result = get_sha1_with_context(obj_name, flags, data->oid.hash, &ctx);
        if (result != FOUND) {
                switch (result) {
                case MISSING_OBJECT:
@@ -328,7 +338,7 @@ struct object_cb_data {
 static void batch_object_cb(const unsigned char sha1[20], void *vdata)
 {
        struct object_cb_data *data = vdata;
-       hashcpy(data->expand->sha1, sha1);
+       hashcpy(data->expand->oid.hash, sha1);
        batch_object_write(NULL, data->opt, data->expand);
 }
 
@@ -369,6 +379,13 @@ static int batch_objects(struct batch_options *opt)
        strbuf_expand(&buf, opt->format, expand_format, &data);
        data.mark_query = 0;
 
+       if (opt->all_objects) {
+               struct object_info empty;
+               memset(&empty, 0, sizeof(empty));
+               if (!memcmp(&data.info, &empty, sizeof(empty)))
+                       data.skip_object_info = 1;
+       }
+
        /*
         * If we are printing out the object, then always fill in the type,
         * since we will want to decide whether or not to stream.
@@ -489,6 +506,7 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
 
        git_config(git_cat_file_config, NULL);
 
+       batch.buffer_output = -1;
        argc = parse_options(argc, argv, prefix, options, cat_file_usage, 0);
 
        if (opt) {
@@ -512,6 +530,9 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
                usage_with_options(cat_file_usage, options);
        }
 
+       if (batch.buffer_output < 0)
+               batch.buffer_output = batch.all_objects;
+
        if (batch.enabled)
                return batch_objects(&batch);