builtin / rev-list.con commit Merge branch 'sb/submodule-update-reset-fix' into maint (14890e9)
   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 struct object_id *oid,
 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", oid_to_hex(oid));
 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 (revs.diffopt.flags.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, ("--no-" CL_ARG__FILTER))) {
 407                        list_objects_filter_release(&filter_options);
 408                        continue;
 409                }
 410                if (!strcmp(arg, "--filter-print-omitted")) {
 411                        arg_print_omitted = 1;
 412                        continue;
 413                }
 414
 415                if (skip_prefix(arg, "--missing=", &arg) &&
 416                    parse_missing_action_value(arg))
 417                        continue;
 418
 419                usage(rev_list_usage);
 420
 421        }
 422        if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
 423                /* The command line has a --pretty  */
 424                info.hdr_termination = '\n';
 425                if (revs.commit_format == CMIT_FMT_ONELINE)
 426                        info.header_prefix = "";
 427                else
 428                        info.header_prefix = "commit ";
 429        }
 430        else if (revs.verbose_header)
 431                /* Only --header was specified */
 432                revs.commit_format = CMIT_FMT_RAW;
 433
 434        if ((!revs.commits && reflog_walk_empty(revs.reflog_info) &&
 435             (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
 436              !revs.pending.nr) &&
 437             !revs.rev_input_given) ||
 438            revs.diff)
 439                usage(rev_list_usage);
 440
 441        if (revs.show_notes)
 442                die(_("rev-list does not support display of notes"));
 443
 444        if (filter_options.choice && use_bitmap_index)
 445                die(_("cannot combine --use-bitmap-index with object filtering"));
 446
 447        save_commit_buffer = (revs.verbose_header ||
 448                              revs.grep_filter.pattern_list ||
 449                              revs.grep_filter.header_list);
 450        if (bisect_list)
 451                revs.limited = 1;
 452
 453        if (show_progress)
 454                progress = start_delayed_progress(show_progress, 0);
 455
 456        if (use_bitmap_index && !revs.prune) {
 457                if (revs.count && !revs.left_right && !revs.cherry_mark) {
 458                        uint32_t commit_count;
 459                        int max_count = revs.max_count;
 460                        if (!prepare_bitmap_walk(&revs)) {
 461                                count_bitmap_commit_list(&commit_count, NULL, NULL, NULL);
 462                                if (max_count >= 0 && max_count < commit_count)
 463                                        commit_count = max_count;
 464                                printf("%d\n", commit_count);
 465                                return 0;
 466                        }
 467                } else if (revs.max_count < 0 &&
 468                           revs.tag_objects && revs.tree_objects && revs.blob_objects) {
 469                        if (!prepare_bitmap_walk(&revs)) {
 470                                traverse_bitmap_commit_list(&show_object_fast);
 471                                return 0;
 472                        }
 473                }
 474        }
 475
 476        if (prepare_revision_walk(&revs))
 477                die("revision walk setup failed");
 478        if (revs.tree_objects)
 479                mark_edges_uninteresting(&revs, show_edge);
 480
 481        if (bisect_list) {
 482                int reaches = reaches, all = all;
 483
 484                find_bisection(&revs.commits, &reaches, &all, bisect_find_all);
 485
 486                if (bisect_show_vars)
 487                        return show_bisect_vars(&info, reaches, all);
 488        }
 489
 490        if (arg_print_omitted)
 491                oidset_init(&omitted_objects, DEFAULT_OIDSET_SIZE);
 492        if (arg_missing_action == MA_PRINT)
 493                oidset_init(&missing_objects, DEFAULT_OIDSET_SIZE);
 494
 495        traverse_commit_list_filtered(
 496                &filter_options, &revs, show_commit, show_object, &info,
 497                (arg_print_omitted ? &omitted_objects : NULL));
 498
 499        if (arg_print_omitted) {
 500                struct oidset_iter iter;
 501                struct object_id *oid;
 502                oidset_iter_init(&omitted_objects, &iter);
 503                while ((oid = oidset_iter_next(&iter)))
 504                        printf("~%s\n", oid_to_hex(oid));
 505                oidset_clear(&omitted_objects);
 506        }
 507        if (arg_missing_action == MA_PRINT) {
 508                struct oidset_iter iter;
 509                struct object_id *oid;
 510                oidset_iter_init(&missing_objects, &iter);
 511                while ((oid = oidset_iter_next(&iter)))
 512                        printf("?%s\n", oid_to_hex(oid));
 513                oidset_clear(&missing_objects);
 514        }
 515
 516        stop_progress(&progress);
 517
 518        if (revs.count) {
 519                if (revs.left_right && revs.cherry_mark)
 520                        printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same);
 521                else if (revs.left_right)
 522                        printf("%d\t%d\n", revs.count_left, revs.count_right);
 523                else if (revs.cherry_mark)
 524                        printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same);
 525                else
 526                        printf("%d\n", revs.count_left + revs.count_right);
 527        }
 528
 529        return 0;
 530}