builtin / cat-file.con commit diff.h: remove extern from function declaration (f758a7f)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include "cache.h"
   7#include "config.h"
   8#include "builtin.h"
   9#include "diff.h"
  10#include "parse-options.h"
  11#include "userdiff.h"
  12#include "streaming.h"
  13#include "tree-walk.h"
  14#include "sha1-array.h"
  15#include "packfile.h"
  16#include "object-store.h"
  17
  18struct batch_options {
  19        int enabled;
  20        int follow_symlinks;
  21        int print_contents;
  22        int buffer_output;
  23        int all_objects;
  24        int cmdmode; /* may be 'w' or 'c' for --filters or --textconv */
  25        const char *format;
  26};
  27
  28static const char *force_path;
  29
  30static int filter_object(const char *path, unsigned mode,
  31                         const struct object_id *oid,
  32                         char **buf, unsigned long *size)
  33{
  34        enum object_type type;
  35
  36        *buf = read_object_file(oid, &type, size);
  37        if (!*buf)
  38                return error(_("cannot read object %s '%s'"),
  39                             oid_to_hex(oid), path);
  40        if ((type == OBJ_BLOB) && S_ISREG(mode)) {
  41                struct strbuf strbuf = STRBUF_INIT;
  42                if (convert_to_working_tree(path, *buf, *size, &strbuf)) {
  43                        free(*buf);
  44                        *size = strbuf.len;
  45                        *buf = strbuf_detach(&strbuf, NULL);
  46                }
  47        }
  48
  49        return 0;
  50}
  51
  52static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
  53                        int unknown_type)
  54{
  55        struct object_id oid;
  56        enum object_type type;
  57        char *buf;
  58        unsigned long size;
  59        struct object_context obj_context;
  60        struct object_info oi = OBJECT_INFO_INIT;
  61        struct strbuf sb = STRBUF_INIT;
  62        unsigned flags = OBJECT_INFO_LOOKUP_REPLACE;
  63        const char *path = force_path;
  64
  65        if (unknown_type)
  66                flags |= OBJECT_INFO_ALLOW_UNKNOWN_TYPE;
  67
  68        if (get_oid_with_context(obj_name, GET_OID_RECORD_PATH,
  69                                 &oid, &obj_context))
  70                die("Not a valid object name %s", obj_name);
  71
  72        if (!path)
  73                path = obj_context.path;
  74        if (obj_context.mode == S_IFINVALID)
  75                obj_context.mode = 0100644;
  76
  77        buf = NULL;
  78        switch (opt) {
  79        case 't':
  80                oi.type_name = &sb;
  81                if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
  82                        die("git cat-file: could not get object info");
  83                if (sb.len) {
  84                        printf("%s\n", sb.buf);
  85                        strbuf_release(&sb);
  86                        return 0;
  87                }
  88                break;
  89
  90        case 's':
  91                oi.sizep = &size;
  92                if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
  93                        die("git cat-file: could not get object info");
  94                printf("%lu\n", size);
  95                return 0;
  96
  97        case 'e':
  98                return !has_object_file(&oid);
  99
 100        case 'w':
 101                if (!path)
 102                        die("git cat-file --filters %s: <object> must be "
 103                            "<sha1:path>", obj_name);
 104
 105                if (filter_object(path, obj_context.mode,
 106                                  &oid, &buf, &size))
 107                        return -1;
 108                break;
 109
 110        case 'c':
 111                if (!path)
 112                        die("git cat-file --textconv %s: <object> must be <sha1:path>",
 113                            obj_name);
 114
 115                if (textconv_object(path, obj_context.mode, &oid, 1, &buf, &size))
 116                        break;
 117                /* else fallthrough */
 118
 119        case 'p':
 120                type = oid_object_info(the_repository, &oid, NULL);
 121                if (type < 0)
 122                        die("Not a valid object name %s", obj_name);
 123
 124                /* custom pretty-print here */
 125                if (type == OBJ_TREE) {
 126                        const char *ls_args[3] = { NULL };
 127                        ls_args[0] =  "ls-tree";
 128                        ls_args[1] =  obj_name;
 129                        return cmd_ls_tree(2, ls_args, NULL);
 130                }
 131
 132                if (type == OBJ_BLOB)
 133                        return stream_blob_to_fd(1, &oid, NULL, 0);
 134                buf = read_object_file(&oid, &type, &size);
 135                if (!buf)
 136                        die("Cannot read object %s", obj_name);
 137
 138                /* otherwise just spit out the data */
 139                break;
 140
 141        case 0:
 142                if (type_from_string(exp_type) == OBJ_BLOB) {
 143                        struct object_id blob_oid;
 144                        if (oid_object_info(the_repository, &oid, NULL) == OBJ_TAG) {
 145                                char *buffer = read_object_file(&oid, &type,
 146                                                                &size);
 147                                const char *target;
 148                                if (!skip_prefix(buffer, "object ", &target) ||
 149                                    get_oid_hex(target, &blob_oid))
 150                                        die("%s not a valid tag", oid_to_hex(&oid));
 151                                free(buffer);
 152                        } else
 153                                oidcpy(&blob_oid, &oid);
 154
 155                        if (oid_object_info(the_repository, &blob_oid, NULL) == OBJ_BLOB)
 156                                return stream_blob_to_fd(1, &blob_oid, NULL, 0);
 157                        /*
 158                         * we attempted to dereference a tag to a blob
 159                         * and failed; there may be new dereference
 160                         * mechanisms this code is not aware of.
 161                         * fall-back to the usual case.
 162                         */
 163                }
 164                buf = read_object_with_reference(&oid, exp_type, &size, NULL);
 165                break;
 166
 167        default:
 168                die("git cat-file: unknown option: %s", exp_type);
 169        }
 170
 171        if (!buf)
 172                die("git cat-file %s: bad file", obj_name);
 173
 174        write_or_die(1, buf, size);
 175        free(buf);
 176        free(obj_context.path);
 177        return 0;
 178}
 179
 180struct expand_data {
 181        struct object_id oid;
 182        enum object_type type;
 183        unsigned long size;
 184        off_t disk_size;
 185        const char *rest;
 186        struct object_id delta_base_oid;
 187
 188        /*
 189         * If mark_query is true, we do not expand anything, but rather
 190         * just mark the object_info with items we wish to query.
 191         */
 192        int mark_query;
 193
 194        /*
 195         * Whether to split the input on whitespace before feeding it to
 196         * get_sha1; this is decided during the mark_query phase based on
 197         * whether we have a %(rest) token in our format.
 198         */
 199        int split_on_whitespace;
 200
 201        /*
 202         * After a mark_query run, this object_info is set up to be
 203         * passed to sha1_object_info_extended. It will point to the data
 204         * elements above, so you can retrieve the response from there.
 205         */
 206        struct object_info info;
 207
 208        /*
 209         * This flag will be true if the requested batch format and options
 210         * don't require us to call sha1_object_info, which can then be
 211         * optimized out.
 212         */
 213        unsigned skip_object_info : 1;
 214};
 215
 216static int is_atom(const char *atom, const char *s, int slen)
 217{
 218        int alen = strlen(atom);
 219        return alen == slen && !memcmp(atom, s, alen);
 220}
 221
 222static void expand_atom(struct strbuf *sb, const char *atom, int len,
 223                        void *vdata)
 224{
 225        struct expand_data *data = vdata;
 226
 227        if (is_atom("objectname", atom, len)) {
 228                if (!data->mark_query)
 229                        strbuf_addstr(sb, oid_to_hex(&data->oid));
 230        } else if (is_atom("objecttype", atom, len)) {
 231                if (data->mark_query)
 232                        data->info.typep = &data->type;
 233                else
 234                        strbuf_addstr(sb, type_name(data->type));
 235        } else if (is_atom("objectsize", atom, len)) {
 236                if (data->mark_query)
 237                        data->info.sizep = &data->size;
 238                else
 239                        strbuf_addf(sb, "%lu", data->size);
 240        } else if (is_atom("objectsize:disk", atom, len)) {
 241                if (data->mark_query)
 242                        data->info.disk_sizep = &data->disk_size;
 243                else
 244                        strbuf_addf(sb, "%"PRIuMAX, (uintmax_t)data->disk_size);
 245        } else if (is_atom("rest", atom, len)) {
 246                if (data->mark_query)
 247                        data->split_on_whitespace = 1;
 248                else if (data->rest)
 249                        strbuf_addstr(sb, data->rest);
 250        } else if (is_atom("deltabase", atom, len)) {
 251                if (data->mark_query)
 252                        data->info.delta_base_sha1 = data->delta_base_oid.hash;
 253                else
 254                        strbuf_addstr(sb,
 255                                      oid_to_hex(&data->delta_base_oid));
 256        } else
 257                die("unknown format element: %.*s", len, atom);
 258}
 259
 260static size_t expand_format(struct strbuf *sb, const char *start, void *data)
 261{
 262        const char *end;
 263
 264        if (*start != '(')
 265                return 0;
 266        end = strchr(start + 1, ')');
 267        if (!end)
 268                die("format element '%s' does not end in ')'", start);
 269
 270        expand_atom(sb, start + 1, end - start - 1, data);
 271
 272        return end - start + 1;
 273}
 274
 275static void batch_write(struct batch_options *opt, const void *data, int len)
 276{
 277        if (opt->buffer_output) {
 278                if (fwrite(data, 1, len, stdout) != len)
 279                        die_errno("unable to write to stdout");
 280        } else
 281                write_or_die(1, data, len);
 282}
 283
 284static void print_object_or_die(struct batch_options *opt, struct expand_data *data)
 285{
 286        const struct object_id *oid = &data->oid;
 287
 288        assert(data->info.typep);
 289
 290        if (data->type == OBJ_BLOB) {
 291                if (opt->buffer_output)
 292                        fflush(stdout);
 293                if (opt->cmdmode) {
 294                        char *contents;
 295                        unsigned long size;
 296
 297                        if (!data->rest)
 298                                die("missing path for '%s'", oid_to_hex(oid));
 299
 300                        if (opt->cmdmode == 'w') {
 301                                if (filter_object(data->rest, 0100644, oid,
 302                                                  &contents, &size))
 303                                        die("could not convert '%s' %s",
 304                                            oid_to_hex(oid), data->rest);
 305                        } else if (opt->cmdmode == 'c') {
 306                                enum object_type type;
 307                                if (!textconv_object(data->rest, 0100644, oid,
 308                                                     1, &contents, &size))
 309                                        contents = read_object_file(oid,
 310                                                                    &type,
 311                                                                    &size);
 312                                if (!contents)
 313                                        die("could not convert '%s' %s",
 314                                            oid_to_hex(oid), data->rest);
 315                        } else
 316                                BUG("invalid cmdmode: %c", opt->cmdmode);
 317                        batch_write(opt, contents, size);
 318                        free(contents);
 319                } else if (stream_blob_to_fd(1, oid, NULL, 0) < 0)
 320                        die("unable to stream %s to stdout", oid_to_hex(oid));
 321        }
 322        else {
 323                enum object_type type;
 324                unsigned long size;
 325                void *contents;
 326
 327                contents = read_object_file(oid, &type, &size);
 328                if (!contents)
 329                        die("object %s disappeared", oid_to_hex(oid));
 330                if (type != data->type)
 331                        die("object %s changed type!?", oid_to_hex(oid));
 332                if (data->info.sizep && size != data->size)
 333                        die("object %s changed size!?", oid_to_hex(oid));
 334
 335                batch_write(opt, contents, size);
 336                free(contents);
 337        }
 338}
 339
 340static void batch_object_write(const char *obj_name, struct batch_options *opt,
 341                               struct expand_data *data)
 342{
 343        struct strbuf buf = STRBUF_INIT;
 344
 345        if (!data->skip_object_info &&
 346            oid_object_info_extended(the_repository, &data->oid, &data->info,
 347                                     OBJECT_INFO_LOOKUP_REPLACE) < 0) {
 348                printf("%s missing\n",
 349                       obj_name ? obj_name : oid_to_hex(&data->oid));
 350                fflush(stdout);
 351                return;
 352        }
 353
 354        strbuf_expand(&buf, opt->format, expand_format, data);
 355        strbuf_addch(&buf, '\n');
 356        batch_write(opt, buf.buf, buf.len);
 357        strbuf_release(&buf);
 358
 359        if (opt->print_contents) {
 360                print_object_or_die(opt, data);
 361                batch_write(opt, "\n", 1);
 362        }
 363}
 364
 365static void batch_one_object(const char *obj_name, struct batch_options *opt,
 366                             struct expand_data *data)
 367{
 368        struct object_context ctx;
 369        int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0;
 370        enum follow_symlinks_result result;
 371
 372        result = get_oid_with_context(obj_name, flags, &data->oid, &ctx);
 373        if (result != FOUND) {
 374                switch (result) {
 375                case MISSING_OBJECT:
 376                        printf("%s missing\n", obj_name);
 377                        break;
 378                case DANGLING_SYMLINK:
 379                        printf("dangling %"PRIuMAX"\n%s\n",
 380                               (uintmax_t)strlen(obj_name), obj_name);
 381                        break;
 382                case SYMLINK_LOOP:
 383                        printf("loop %"PRIuMAX"\n%s\n",
 384                               (uintmax_t)strlen(obj_name), obj_name);
 385                        break;
 386                case NOT_DIR:
 387                        printf("notdir %"PRIuMAX"\n%s\n",
 388                               (uintmax_t)strlen(obj_name), obj_name);
 389                        break;
 390                default:
 391                        BUG("unknown get_sha1_with_context result %d\n",
 392                               result);
 393                        break;
 394                }
 395                fflush(stdout);
 396                return;
 397        }
 398
 399        if (ctx.mode == 0) {
 400                printf("symlink %"PRIuMAX"\n%s\n",
 401                       (uintmax_t)ctx.symlink_path.len,
 402                       ctx.symlink_path.buf);
 403                fflush(stdout);
 404                return;
 405        }
 406
 407        batch_object_write(obj_name, opt, data);
 408}
 409
 410struct object_cb_data {
 411        struct batch_options *opt;
 412        struct expand_data *expand;
 413};
 414
 415static int batch_object_cb(const struct object_id *oid, void *vdata)
 416{
 417        struct object_cb_data *data = vdata;
 418        oidcpy(&data->expand->oid, oid);
 419        batch_object_write(NULL, data->opt, data->expand);
 420        return 0;
 421}
 422
 423static int batch_loose_object(const struct object_id *oid,
 424                              const char *path,
 425                              void *data)
 426{
 427        oid_array_append(data, oid);
 428        return 0;
 429}
 430
 431static int batch_packed_object(const struct object_id *oid,
 432                               struct packed_git *pack,
 433                               uint32_t pos,
 434                               void *data)
 435{
 436        oid_array_append(data, oid);
 437        return 0;
 438}
 439
 440static int batch_objects(struct batch_options *opt)
 441{
 442        struct strbuf buf = STRBUF_INIT;
 443        struct expand_data data;
 444        int save_warning;
 445        int retval = 0;
 446
 447        if (!opt->format)
 448                opt->format = "%(objectname) %(objecttype) %(objectsize)";
 449
 450        /*
 451         * Expand once with our special mark_query flag, which will prime the
 452         * object_info to be handed to sha1_object_info_extended for each
 453         * object.
 454         */
 455        memset(&data, 0, sizeof(data));
 456        data.mark_query = 1;
 457        strbuf_expand(&buf, opt->format, expand_format, &data);
 458        data.mark_query = 0;
 459        if (opt->cmdmode)
 460                data.split_on_whitespace = 1;
 461
 462        if (opt->all_objects) {
 463                struct object_info empty = OBJECT_INFO_INIT;
 464                if (!memcmp(&data.info, &empty, sizeof(empty)))
 465                        data.skip_object_info = 1;
 466        }
 467
 468        /*
 469         * If we are printing out the object, then always fill in the type,
 470         * since we will want to decide whether or not to stream.
 471         */
 472        if (opt->print_contents)
 473                data.info.typep = &data.type;
 474
 475        if (opt->all_objects) {
 476                struct oid_array sa = OID_ARRAY_INIT;
 477                struct object_cb_data cb;
 478
 479                for_each_loose_object(batch_loose_object, &sa, 0);
 480                for_each_packed_object(batch_packed_object, &sa, 0);
 481                if (repository_format_partial_clone)
 482                        warning("This repository has extensions.partialClone set. Some objects may not be loaded.");
 483
 484                cb.opt = opt;
 485                cb.expand = &data;
 486                oid_array_for_each_unique(&sa, batch_object_cb, &cb);
 487
 488                oid_array_clear(&sa);
 489                return 0;
 490        }
 491
 492        /*
 493         * We are going to call get_sha1 on a potentially very large number of
 494         * objects. In most large cases, these will be actual object sha1s. The
 495         * cost to double-check that each one is not also a ref (just so we can
 496         * warn) ends up dwarfing the actual cost of the object lookups
 497         * themselves. We can work around it by just turning off the warning.
 498         */
 499        save_warning = warn_on_object_refname_ambiguity;
 500        warn_on_object_refname_ambiguity = 0;
 501
 502        while (strbuf_getline(&buf, stdin) != EOF) {
 503                if (data.split_on_whitespace) {
 504                        /*
 505                         * Split at first whitespace, tying off the beginning
 506                         * of the string and saving the remainder (or NULL) in
 507                         * data.rest.
 508                         */
 509                        char *p = strpbrk(buf.buf, " \t");
 510                        if (p) {
 511                                while (*p && strchr(" \t", *p))
 512                                        *p++ = '\0';
 513                        }
 514                        data.rest = p;
 515                }
 516
 517                batch_one_object(buf.buf, opt, &data);
 518        }
 519
 520        strbuf_release(&buf);
 521        warn_on_object_refname_ambiguity = save_warning;
 522        return retval;
 523}
 524
 525static const char * const cat_file_usage[] = {
 526        N_("git cat-file (-t [--allow-unknown-type] | -s [--allow-unknown-type] | -e | -p | <type> | --textconv | --filters) [--path=<path>] <object>"),
 527        N_("git cat-file (--batch | --batch-check) [--follow-symlinks] [--textconv | --filters]"),
 528        NULL
 529};
 530
 531static int git_cat_file_config(const char *var, const char *value, void *cb)
 532{
 533        if (userdiff_config(var, value) < 0)
 534                return -1;
 535
 536        return git_default_config(var, value, cb);
 537}
 538
 539static int batch_option_callback(const struct option *opt,
 540                                 const char *arg,
 541                                 int unset)
 542{
 543        struct batch_options *bo = opt->value;
 544
 545        if (bo->enabled) {
 546                return 1;
 547        }
 548
 549        bo->enabled = 1;
 550        bo->print_contents = !strcmp(opt->long_name, "batch");
 551        bo->format = arg;
 552
 553        return 0;
 554}
 555
 556int cmd_cat_file(int argc, const char **argv, const char *prefix)
 557{
 558        int opt = 0;
 559        const char *exp_type = NULL, *obj_name = NULL;
 560        struct batch_options batch = {0};
 561        int unknown_type = 0;
 562
 563        const struct option options[] = {
 564                OPT_GROUP(N_("<type> can be one of: blob, tree, commit, tag")),
 565                OPT_CMDMODE('t', NULL, &opt, N_("show object type"), 't'),
 566                OPT_CMDMODE('s', NULL, &opt, N_("show object size"), 's'),
 567                OPT_CMDMODE('e', NULL, &opt,
 568                            N_("exit with zero when there's no error"), 'e'),
 569                OPT_CMDMODE('p', NULL, &opt, N_("pretty-print object's content"), 'p'),
 570                OPT_CMDMODE(0, "textconv", &opt,
 571                            N_("for blob objects, run textconv on object's content"), 'c'),
 572                OPT_CMDMODE(0, "filters", &opt,
 573                            N_("for blob objects, run filters on object's content"), 'w'),
 574                OPT_STRING(0, "path", &force_path, N_("blob"),
 575                           N_("use a specific path for --textconv/--filters")),
 576                OPT_BOOL(0, "allow-unknown-type", &unknown_type,
 577                          N_("allow -s and -t to work with broken/corrupt objects")),
 578                OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")),
 579                { OPTION_CALLBACK, 0, "batch", &batch, "format",
 580                        N_("show info and content of objects fed from the standard input"),
 581                        PARSE_OPT_OPTARG, batch_option_callback },
 582                { OPTION_CALLBACK, 0, "batch-check", &batch, "format",
 583                        N_("show info about objects fed from the standard input"),
 584                        PARSE_OPT_OPTARG, batch_option_callback },
 585                OPT_BOOL(0, "follow-symlinks", &batch.follow_symlinks,
 586                         N_("follow in-tree symlinks (used with --batch or --batch-check)")),
 587                OPT_BOOL(0, "batch-all-objects", &batch.all_objects,
 588                         N_("show all objects with --batch or --batch-check")),
 589                OPT_END()
 590        };
 591
 592        git_config(git_cat_file_config, NULL);
 593
 594        batch.buffer_output = -1;
 595        argc = parse_options(argc, argv, prefix, options, cat_file_usage, 0);
 596
 597        if (opt) {
 598                if (batch.enabled && (opt == 'c' || opt == 'w'))
 599                        batch.cmdmode = opt;
 600                else if (argc == 1)
 601                        obj_name = argv[0];
 602                else
 603                        usage_with_options(cat_file_usage, options);
 604        }
 605        if (!opt && !batch.enabled) {
 606                if (argc == 2) {
 607                        exp_type = argv[0];
 608                        obj_name = argv[1];
 609                } else
 610                        usage_with_options(cat_file_usage, options);
 611        }
 612        if (batch.enabled) {
 613                if (batch.cmdmode != opt || argc)
 614                        usage_with_options(cat_file_usage, options);
 615                if (batch.cmdmode && batch.all_objects)
 616                        die("--batch-all-objects cannot be combined with "
 617                            "--textconv nor with --filters");
 618        }
 619
 620        if ((batch.follow_symlinks || batch.all_objects) && !batch.enabled) {
 621                usage_with_options(cat_file_usage, options);
 622        }
 623
 624        if (force_path && opt != 'c' && opt != 'w') {
 625                error("--path=<path> needs --textconv or --filters");
 626                usage_with_options(cat_file_usage, options);
 627        }
 628
 629        if (force_path && batch.enabled) {
 630                error("--path=<path> incompatible with --batch");
 631                usage_with_options(cat_file_usage, options);
 632        }
 633
 634        if (batch.buffer_output < 0)
 635                batch.buffer_output = batch.all_objects;
 636
 637        if (batch.enabled)
 638                return batch_objects(&batch);
 639
 640        if (unknown_type && opt != 't' && opt != 's')
 641                die("git cat-file --allow-unknown-type: use with -s or -t");
 642        return cat_one_file(opt, exp_type, obj_name, unknown_type);
 643}