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