builtin / rev-list.con commit revision.c: put promisor option in specialized struct (bbcde41)
   1#include "cache.h"
   2#include "config.h"
   3#include "commit.h"
   4#include "diff.h"
   5#include "revision.h"
   6#include "list-objects.h"
   7#include "list-objects-filter.h"
   8#include "list-objects-filter-options.h"
   9#include "pack.h"
  10#include "pack-bitmap.h"
  11#include "builtin.h"
  12#include "log-tree.h"
  13#include "graph.h"
  14#include "bisect.h"
  15#include "progress.h"
  16#include "reflog-walk.h"
  17#include "oidset.h"
  18#include "packfile.h"
  19
  20static const char rev_list_usage[] =
  21"git rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
  22"  limiting output:\n"
  23"    --max-count=<n>\n"
  24"    --max-age=<epoch>\n"
  25"    --min-age=<epoch>\n"
  26"    --sparse\n"
  27"    --no-merges\n"
  28"    --min-parents=<n>\n"
  29"    --no-min-parents\n"
  30"    --max-parents=<n>\n"
  31"    --no-max-parents\n"
  32"    --remove-empty\n"
  33"    --all\n"
  34"    --branches\n"
  35"    --tags\n"
  36"    --remotes\n"
  37"    --stdin\n"
  38"    --quiet\n"
  39"  ordering output:\n"
  40"    --topo-order\n"
  41"    --date-order\n"
  42"    --reverse\n"
  43"  formatting output:\n"
  44"    --parents\n"
  45"    --children\n"
  46"    --objects | --objects-edge\n"
  47"    --unpacked\n"
  48"    --header | --pretty\n"
  49"    --abbrev=<n> | --no-abbrev\n"
  50"    --abbrev-commit\n"
  51"    --left-right\n"
  52"    --count\n"
  53"  special purpose:\n"
  54"    --bisect\n"
  55"    --bisect-vars\n"
  56"    --bisect-all"
  57;
  58
  59static struct progress *progress;
  60static unsigned progress_counter;
  61
  62static struct list_objects_filter_options filter_options;
  63static struct oidset omitted_objects;
  64static int arg_print_omitted; /* print objects omitted by filter */
  65
  66static struct oidset missing_objects;
  67enum missing_action {
  68        MA_ERROR = 0,    /* fail if any missing objects are encountered */
  69        MA_ALLOW_ANY,    /* silently allow ALL missing objects */
  70        MA_PRINT,        /* print ALL missing objects in special section */
  71        MA_ALLOW_PROMISOR, /* silently allow all missing PROMISOR objects */
  72};
  73static enum missing_action arg_missing_action;
  74
  75#define DEFAULT_OIDSET_SIZE     (16*1024)
  76
  77static void finish_commit(struct commit *commit, void *data);
  78static void show_commit(struct commit *commit, void *data)
  79{
  80        struct rev_list_info *info = data;
  81        struct rev_info *revs = info->revs;
  82
  83        display_progress(progress, ++progress_counter);
  84
  85        if (info->flags & REV_LIST_QUIET) {
  86                finish_commit(commit, data);
  87                return;
  88        }
  89
  90        graph_show_commit(revs->graph);
  91
  92        if (revs->count) {
  93                if (commit->object.flags & PATCHSAME)
  94                        revs->count_same++;
  95                else if (commit->object.flags & SYMMETRIC_LEFT)
  96                        revs->count_left++;
  97                else
  98                        revs->count_right++;
  99                finish_commit(commit, data);
 100                return;
 101        }
 102
 103        if (info->show_timestamp)
 104                printf("%"PRItime" ", commit->date);
 105        if (info->header_prefix)
 106                fputs(info->header_prefix, stdout);
 107
 108        if (!revs->graph)
 109                fputs(get_revision_mark(revs, commit), stdout);
 110        if (revs->abbrev_commit && revs->abbrev)
 111                fputs(find_unique_abbrev(commit->object.oid.hash, revs->abbrev),
 112                      stdout);
 113        else
 114                fputs(oid_to_hex(&commit->object.oid), stdout);
 115        if (revs->print_parents) {
 116                struct commit_list *parents = commit->parents;
 117                while (parents) {
 118                        printf(" %s", oid_to_hex(&parents->item->object.oid));
 119                        parents = parents->next;
 120                }
 121        }
 122        if (revs->children.name) {
 123                struct commit_list *children;
 124
 125                children = lookup_decoration(&revs->children, &commit->object);
 126                while (children) {
 127                        printf(" %s", oid_to_hex(&children->item->object.oid));
 128                        children = children->next;
 129                }
 130        }
 131        show_decorations(revs, commit);
 132        if (revs->commit_format == CMIT_FMT_ONELINE)
 133                putchar(' ');
 134        else
 135                putchar('\n');
 136
 137        if (revs->verbose_header) {
 138                struct strbuf buf = STRBUF_INIT;
 139                struct pretty_print_context ctx = {0};
 140                ctx.abbrev = revs->abbrev;
 141                ctx.date_mode = revs->date_mode;
 142                ctx.date_mode_explicit = revs->date_mode_explicit;
 143                ctx.fmt = revs->commit_format;
 144                ctx.output_encoding = get_log_output_encoding();
 145                ctx.color = revs->diffopt.use_color;
 146                pretty_print_commit(&ctx, commit, &buf);
 147                if (buf.len) {
 148                        if (revs->commit_format != CMIT_FMT_ONELINE)
 149                                graph_show_oneline(revs->graph);
 150
 151                        graph_show_commit_msg(revs->graph, stdout, &buf);
 152
 153                        /*
 154                         * Add a newline after the commit message.
 155                         *
 156                         * Usually, this newline produces a blank
 157                         * padding line between entries, in which case
 158                         * we need to add graph padding on this line.
 159                         *
 160                         * However, the commit message may not end in a
 161                         * newline.  In this case the newline simply
 162                         * ends the last line of the commit message,
 163                         * and we don't need any graph output.  (This
 164                         * always happens with CMIT_FMT_ONELINE, and it
 165                         * happens with CMIT_FMT_USERFORMAT when the
 166                         * format doesn't explicitly end in a newline.)
 167                         */
 168                        if (buf.len && buf.buf[buf.len - 1] == '\n')
 169                                graph_show_padding(revs->graph);
 170                        putchar(info->hdr_termination);
 171                } else {
 172                        /*
 173                         * If the message buffer is empty, just show
 174                         * the rest of the graph output for this
 175                         * commit.
 176                         */
 177                        if (graph_show_remainder(revs->graph))
 178                                putchar('\n');
 179                        if (revs->commit_format == CMIT_FMT_ONELINE)
 180                                putchar('\n');
 181                }
 182                strbuf_release(&buf);
 183        } else {
 184                if (graph_show_remainder(revs->graph))
 185                        putchar('\n');
 186        }
 187        maybe_flush_or_die(stdout, "stdout");
 188        finish_commit(commit, data);
 189}
 190
 191static void finish_commit(struct commit *commit, void *data)
 192{
 193        if (commit->parents) {
 194                free_commit_list(commit->parents);
 195                commit->parents = NULL;
 196        }
 197        free_commit_buffer(commit);
 198}
 199
 200static inline void finish_object__ma(struct object *obj)
 201{
 202        /*
 203         * Whether or not we try to dynamically fetch missing objects
 204         * from the server, we currently DO NOT have the object.  We
 205         * can either print, allow (ignore), or conditionally allow
 206         * (ignore) them.
 207         */
 208        switch (arg_missing_action) {
 209        case MA_ERROR:
 210                die("missing blob object '%s'", oid_to_hex(&obj->oid));
 211                return;
 212
 213        case MA_ALLOW_ANY:
 214                return;
 215
 216        case MA_PRINT:
 217                oidset_insert(&missing_objects, &obj->oid);
 218                return;
 219
 220        case MA_ALLOW_PROMISOR:
 221                if (is_promisor_object(&obj->oid))
 222                        return;
 223                die("unexpected missing blob object '%s'",
 224                    oid_to_hex(&obj->oid));
 225                return;
 226
 227        default:
 228                BUG("unhandled missing_action");
 229                return;
 230        }
 231}
 232
 233static int finish_object(struct object *obj, const char *name, void *cb_data)
 234{
 235        struct rev_list_info *info = cb_data;
 236        if (obj->type == OBJ_BLOB && !has_object_file(&obj->oid)) {
 237                finish_object__ma(obj);
 238                return 1;
 239        }
 240        if (info->revs->verify_objects && !obj->parsed && obj->type != OBJ_COMMIT)
 241                parse_object(&obj->oid);
 242        return 0;
 243}
 244
 245static void show_object(struct object *obj, const char *name, void *cb_data)
 246{
 247        struct rev_list_info *info = cb_data;
 248        if (finish_object(obj, name, cb_data))
 249                return;
 250        display_progress(progress, ++progress_counter);
 251        if (info->flags & REV_LIST_QUIET)
 252                return;
 253        show_object_with_name(stdout, obj, name);
 254}
 255
 256static void show_edge(struct commit *commit)
 257{
 258        printf("-%s\n", oid_to_hex(&commit->object.oid));
 259}
 260
 261static void print_var_str(const char *var, const char *val)
 262{
 263        printf("%s='%s'\n", var, val);
 264}
 265
 266static void print_var_int(const char *var, int val)
 267{
 268        printf("%s=%d\n", var, val);
 269}
 270
 271static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
 272{
 273        int cnt, flags = info->flags;
 274        char hex[GIT_MAX_HEXSZ + 1] = "";
 275        struct commit_list *tried;
 276        struct rev_info *revs = info->revs;
 277
 278        if (!revs->commits)
 279                return 1;
 280
 281        revs->commits = filter_skipped(revs->commits, &tried,
 282                                       flags & BISECT_SHOW_ALL,
 283                                       NULL, NULL);
 284
 285        /*
 286         * revs->commits can reach "reaches" commits among
 287         * "all" commits.  If it is good, then there are
 288         * (all-reaches) commits left to be bisected.
 289         * On the other hand, if it is bad, then the set
 290         * to bisect is "reaches".
 291         * A bisect set of size N has (N-1) commits further
 292         * to test, as we already know one bad one.
 293         */
 294        cnt = all - reaches;
 295        if (cnt < reaches)
 296                cnt = reaches;
 297
 298        if (revs->commits)
 299                oid_to_hex_r(hex, &revs->commits->item->object.oid);
 300
 301        if (flags & BISECT_SHOW_ALL) {
 302                traverse_commit_list(revs, show_commit, show_object, info);
 303                printf("------\n");
 304        }
 305
 306        print_var_str("bisect_rev", hex);
 307        print_var_int("bisect_nr", cnt - 1);
 308        print_var_int("bisect_good", all - reaches - 1);
 309        print_var_int("bisect_bad", reaches - 1);
 310        print_var_int("bisect_all", all);
 311        print_var_int("bisect_steps", estimate_bisect_steps(all));
 312
 313        return 0;
 314}
 315
 316static int show_object_fast(
 317        const struct object_id *oid,
 318        enum object_type type,
 319        int exclude,
 320        uint32_t name_hash,
 321        struct packed_git *found_pack,
 322        off_t found_offset)
 323{
 324        fprintf(stdout, "%s\n", oid_to_hex(oid));
 325        return 1;
 326}
 327
 328static inline int parse_missing_action_value(const char *value)
 329{
 330        if (!strcmp(value, "error")) {
 331                arg_missing_action = MA_ERROR;
 332                return 1;
 333        }
 334
 335        if (!strcmp(value, "allow-any")) {
 336                arg_missing_action = MA_ALLOW_ANY;
 337                fetch_if_missing = 0;
 338                return 1;
 339        }
 340
 341        if (!strcmp(value, "print")) {
 342                arg_missing_action = MA_PRINT;
 343                fetch_if_missing = 0;
 344                return 1;
 345        }
 346
 347        if (!strcmp(value, "allow-promisor")) {
 348                arg_missing_action = MA_ALLOW_PROMISOR;
 349                fetch_if_missing = 0;
 350                return 1;
 351        }
 352
 353        return 0;
 354}
 355
 356int cmd_rev_list(int argc, const char **argv, const char *prefix)
 357{
 358        struct rev_info revs;
 359        struct rev_list_info info;
 360        struct setup_revision_opt s_r_opt = {
 361                .allow_exclude_promisor_objects = 1,
 362        };
 363        int i;
 364        int bisect_list = 0;
 365        int bisect_show_vars = 0;
 366        int bisect_find_all = 0;
 367        int use_bitmap_index = 0;
 368        const char *show_progress = NULL;
 369
 370        if (argc == 2 && !strcmp(argv[1], "-h"))
 371                usage(rev_list_usage);
 372
 373        git_config(git_default_config, NULL);
 374        init_revisions(&revs, prefix);
 375        revs.abbrev = DEFAULT_ABBREV;
 376        revs.commit_format = CMIT_FMT_UNSPECIFIED;
 377
 378        /*
 379         * Scan the argument list before invoking setup_revisions(), so that we
 380         * know if fetch_if_missing needs to be set to 0.
 381         *
 382         * "--exclude-promisor-objects" acts as a pre-filter on missing objects
 383         * by not crossing the boundary from realized objects to promisor
 384         * objects.
 385         *
 386         * Let "--missing" to conditionally set fetch_if_missing.
 387         */
 388        for (i = 1; i < argc; i++) {
 389                const char *arg = argv[i];
 390                if (!strcmp(arg, "--exclude-promisor-objects")) {
 391                        fetch_if_missing = 0;
 392                        revs.exclude_promisor_objects = 1;
 393                        break;
 394                }
 395        }
 396        for (i = 1; i < argc; i++) {
 397                const char *arg = argv[i];
 398                if (skip_prefix(arg, "--missing=", &arg)) {
 399                        if (revs.exclude_promisor_objects)
 400                                die(_("cannot combine --exclude-promisor-objects and --missing"));
 401                        if (parse_missing_action_value(arg))
 402                                break;
 403                }
 404        }
 405
 406        argc = setup_revisions(argc, argv, &revs, &s_r_opt);
 407
 408        memset(&info, 0, sizeof(info));
 409        info.revs = &revs;
 410        if (revs.bisect)
 411                bisect_list = 1;
 412
 413        if (revs.diffopt.flags.quick)
 414                info.flags |= REV_LIST_QUIET;
 415        for (i = 1 ; i < argc; i++) {
 416                const char *arg = argv[i];
 417
 418                if (!strcmp(arg, "--header")) {
 419                        revs.verbose_header = 1;
 420                        continue;
 421                }
 422                if (!strcmp(arg, "--timestamp")) {
 423                        info.show_timestamp = 1;
 424                        continue;
 425                }
 426                if (!strcmp(arg, "--bisect")) {
 427                        bisect_list = 1;
 428                        continue;
 429                }
 430                if (!strcmp(arg, "--bisect-all")) {
 431                        bisect_list = 1;
 432                        bisect_find_all = 1;
 433                        info.flags |= BISECT_SHOW_ALL;
 434                        revs.show_decorations = 1;
 435                        continue;
 436                }
 437                if (!strcmp(arg, "--bisect-vars")) {
 438                        bisect_list = 1;
 439                        bisect_show_vars = 1;
 440                        continue;
 441                }
 442                if (!strcmp(arg, "--use-bitmap-index")) {
 443                        use_bitmap_index = 1;
 444                        continue;
 445                }
 446                if (!strcmp(arg, "--test-bitmap")) {
 447                        test_bitmap_walk(&revs);
 448                        return 0;
 449                }
 450                if (skip_prefix(arg, "--progress=", &arg)) {
 451                        show_progress = arg;
 452                        continue;
 453                }
 454
 455                if (skip_prefix(arg, ("--" CL_ARG__FILTER "="), &arg)) {
 456                        parse_list_objects_filter(&filter_options, arg);
 457                        if (filter_options.choice && !revs.blob_objects)
 458                                die(_("object filtering requires --objects"));
 459                        if (filter_options.choice == LOFC_SPARSE_OID &&
 460                            !filter_options.sparse_oid_value)
 461                                die(_("invalid sparse value '%s'"),
 462                                    filter_options.filter_spec);
 463                        continue;
 464                }
 465                if (!strcmp(arg, ("--no-" CL_ARG__FILTER))) {
 466                        list_objects_filter_set_no_filter(&filter_options);
 467                        continue;
 468                }
 469                if (!strcmp(arg, "--filter-print-omitted")) {
 470                        arg_print_omitted = 1;
 471                        continue;
 472                }
 473
 474                if (!strcmp(arg, "--exclude-promisor-objects"))
 475                        continue; /* already handled above */
 476                if (skip_prefix(arg, "--missing=", &arg))
 477                        continue; /* already handled above */
 478
 479                usage(rev_list_usage);
 480
 481        }
 482        if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
 483                /* The command line has a --pretty  */
 484                info.hdr_termination = '\n';
 485                if (revs.commit_format == CMIT_FMT_ONELINE)
 486                        info.header_prefix = "";
 487                else
 488                        info.header_prefix = "commit ";
 489        }
 490        else if (revs.verbose_header)
 491                /* Only --header was specified */
 492                revs.commit_format = CMIT_FMT_RAW;
 493
 494        if ((!revs.commits && reflog_walk_empty(revs.reflog_info) &&
 495             (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
 496              !revs.pending.nr) &&
 497             !revs.rev_input_given) ||
 498            revs.diff)
 499                usage(rev_list_usage);
 500
 501        if (revs.show_notes)
 502                die(_("rev-list does not support display of notes"));
 503
 504        if (filter_options.choice && use_bitmap_index)
 505                die(_("cannot combine --use-bitmap-index with object filtering"));
 506
 507        save_commit_buffer = (revs.verbose_header ||
 508                              revs.grep_filter.pattern_list ||
 509                              revs.grep_filter.header_list);
 510        if (bisect_list)
 511                revs.limited = 1;
 512
 513        if (show_progress)
 514                progress = start_delayed_progress(show_progress, 0);
 515
 516        if (use_bitmap_index && !revs.prune) {
 517                if (revs.count && !revs.left_right && !revs.cherry_mark) {
 518                        uint32_t commit_count;
 519                        int max_count = revs.max_count;
 520                        if (!prepare_bitmap_walk(&revs)) {
 521                                count_bitmap_commit_list(&commit_count, NULL, NULL, NULL);
 522                                if (max_count >= 0 && max_count < commit_count)
 523                                        commit_count = max_count;
 524                                printf("%d\n", commit_count);
 525                                return 0;
 526                        }
 527                } else if (revs.max_count < 0 &&
 528                           revs.tag_objects && revs.tree_objects && revs.blob_objects) {
 529                        if (!prepare_bitmap_walk(&revs)) {
 530                                traverse_bitmap_commit_list(&show_object_fast);
 531                                return 0;
 532                        }
 533                }
 534        }
 535
 536        if (prepare_revision_walk(&revs))
 537                die("revision walk setup failed");
 538        if (revs.tree_objects)
 539                mark_edges_uninteresting(&revs, show_edge);
 540
 541        if (bisect_list) {
 542                int reaches, all;
 543
 544                find_bisection(&revs.commits, &reaches, &all, bisect_find_all);
 545
 546                if (bisect_show_vars)
 547                        return show_bisect_vars(&info, reaches, all);
 548        }
 549
 550        if (arg_print_omitted)
 551                oidset_init(&omitted_objects, DEFAULT_OIDSET_SIZE);
 552        if (arg_missing_action == MA_PRINT)
 553                oidset_init(&missing_objects, DEFAULT_OIDSET_SIZE);
 554
 555        traverse_commit_list_filtered(
 556                &filter_options, &revs, show_commit, show_object, &info,
 557                (arg_print_omitted ? &omitted_objects : NULL));
 558
 559        if (arg_print_omitted) {
 560                struct oidset_iter iter;
 561                struct object_id *oid;
 562                oidset_iter_init(&omitted_objects, &iter);
 563                while ((oid = oidset_iter_next(&iter)))
 564                        printf("~%s\n", oid_to_hex(oid));
 565                oidset_clear(&omitted_objects);
 566        }
 567        if (arg_missing_action == MA_PRINT) {
 568                struct oidset_iter iter;
 569                struct object_id *oid;
 570                oidset_iter_init(&missing_objects, &iter);
 571                while ((oid = oidset_iter_next(&iter)))
 572                        printf("?%s\n", oid_to_hex(oid));
 573                oidset_clear(&missing_objects);
 574        }
 575
 576        stop_progress(&progress);
 577
 578        if (revs.count) {
 579                if (revs.left_right && revs.cherry_mark)
 580                        printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same);
 581                else if (revs.left_right)
 582                        printf("%d\t%d\n", revs.count_left, revs.count_right);
 583                else if (revs.cherry_mark)
 584                        printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same);
 585                else
 586                        printf("%d\n", revs.count_left + revs.count_right);
 587        }
 588
 589        return 0;
 590}