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