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