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