builtin / rev-list.con commit Merge branch 'jk/shortlog-tolerate-broken-commit' (7f794aa)
   1#include "cache.h"
   2#include "commit.h"
   3#include "diff.h"
   4#include "revision.h"
   5#include "list-objects.h"
   6#include "builtin.h"
   7#include "log-tree.h"
   8#include "graph.h"
   9#include "bisect.h"
  10
  11static const char rev_list_usage[] =
  12"git rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
  13"  limiting output:\n"
  14"    --max-count=<n>\n"
  15"    --max-age=<epoch>\n"
  16"    --min-age=<epoch>\n"
  17"    --sparse\n"
  18"    --no-merges\n"
  19"    --min-parents=<n>\n"
  20"    --no-min-parents\n"
  21"    --max-parents=<n>\n"
  22"    --no-max-parents\n"
  23"    --remove-empty\n"
  24"    --all\n"
  25"    --branches\n"
  26"    --tags\n"
  27"    --remotes\n"
  28"    --stdin\n"
  29"    --quiet\n"
  30"  ordering output:\n"
  31"    --topo-order\n"
  32"    --date-order\n"
  33"    --reverse\n"
  34"  formatting output:\n"
  35"    --parents\n"
  36"    --children\n"
  37"    --objects | --objects-edge\n"
  38"    --unpacked\n"
  39"    --header | --pretty\n"
  40"    --abbrev=<n> | --no-abbrev\n"
  41"    --abbrev-commit\n"
  42"    --left-right\n"
  43"  special purpose:\n"
  44"    --bisect\n"
  45"    --bisect-vars\n"
  46"    --bisect-all"
  47;
  48
  49static void finish_commit(struct commit *commit, void *data);
  50static void show_commit(struct commit *commit, void *data)
  51{
  52        struct rev_list_info *info = data;
  53        struct rev_info *revs = info->revs;
  54
  55        if (info->flags & REV_LIST_QUIET) {
  56                finish_commit(commit, data);
  57                return;
  58        }
  59
  60        graph_show_commit(revs->graph);
  61
  62        if (revs->count) {
  63                if (commit->object.flags & PATCHSAME)
  64                        revs->count_same++;
  65                else if (commit->object.flags & SYMMETRIC_LEFT)
  66                        revs->count_left++;
  67                else
  68                        revs->count_right++;
  69                finish_commit(commit, data);
  70                return;
  71        }
  72
  73        if (info->show_timestamp)
  74                printf("%lu ", commit->date);
  75        if (info->header_prefix)
  76                fputs(info->header_prefix, stdout);
  77
  78        if (!revs->graph)
  79                fputs(get_revision_mark(revs, commit), stdout);
  80        if (revs->abbrev_commit && revs->abbrev)
  81                fputs(find_unique_abbrev(commit->object.sha1, revs->abbrev),
  82                      stdout);
  83        else
  84                fputs(sha1_to_hex(commit->object.sha1), stdout);
  85        if (revs->print_parents) {
  86                struct commit_list *parents = commit->parents;
  87                while (parents) {
  88                        printf(" %s", sha1_to_hex(parents->item->object.sha1));
  89                        parents = parents->next;
  90                }
  91        }
  92        if (revs->children.name) {
  93                struct commit_list *children;
  94
  95                children = lookup_decoration(&revs->children, &commit->object);
  96                while (children) {
  97                        printf(" %s", sha1_to_hex(children->item->object.sha1));
  98                        children = children->next;
  99                }
 100        }
 101        show_decorations(revs, commit);
 102        if (revs->commit_format == CMIT_FMT_ONELINE)
 103                putchar(' ');
 104        else
 105                putchar('\n');
 106
 107        if (revs->verbose_header && commit->buffer) {
 108                struct strbuf buf = STRBUF_INIT;
 109                struct pretty_print_context ctx = {0};
 110                ctx.abbrev = revs->abbrev;
 111                ctx.date_mode = revs->date_mode;
 112                ctx.date_mode_explicit = revs->date_mode_explicit;
 113                ctx.fmt = revs->commit_format;
 114                ctx.output_encoding = get_log_output_encoding();
 115                pretty_print_commit(&ctx, commit, &buf);
 116                if (revs->graph) {
 117                        if (buf.len) {
 118                                if (revs->commit_format != CMIT_FMT_ONELINE)
 119                                        graph_show_oneline(revs->graph);
 120
 121                                graph_show_commit_msg(revs->graph, &buf);
 122
 123                                /*
 124                                 * Add a newline after the commit message.
 125                                 *
 126                                 * Usually, this newline produces a blank
 127                                 * padding line between entries, in which case
 128                                 * we need to add graph padding on this line.
 129                                 *
 130                                 * However, the commit message may not end in a
 131                                 * newline.  In this case the newline simply
 132                                 * ends the last line of the commit message,
 133                                 * and we don't need any graph output.  (This
 134                                 * always happens with CMIT_FMT_ONELINE, and it
 135                                 * happens with CMIT_FMT_USERFORMAT when the
 136                                 * format doesn't explicitly end in a newline.)
 137                                 */
 138                                if (buf.len && buf.buf[buf.len - 1] == '\n')
 139                                        graph_show_padding(revs->graph);
 140                                putchar('\n');
 141                        } else {
 142                                /*
 143                                 * If the message buffer is empty, just show
 144                                 * the rest of the graph output for this
 145                                 * commit.
 146                                 */
 147                                if (graph_show_remainder(revs->graph))
 148                                        putchar('\n');
 149                                if (revs->commit_format == CMIT_FMT_ONELINE)
 150                                        putchar('\n');
 151                        }
 152                } else {
 153                        if (revs->commit_format != CMIT_FMT_USERFORMAT ||
 154                            buf.len) {
 155                                fwrite(buf.buf, 1, buf.len, stdout);
 156                                putchar(info->hdr_termination);
 157                        }
 158                }
 159                strbuf_release(&buf);
 160        } else {
 161                if (graph_show_remainder(revs->graph))
 162                        putchar('\n');
 163        }
 164        maybe_flush_or_die(stdout, "stdout");
 165        finish_commit(commit, data);
 166}
 167
 168static void finish_commit(struct commit *commit, void *data)
 169{
 170        if (commit->parents) {
 171                free_commit_list(commit->parents);
 172                commit->parents = NULL;
 173        }
 174        free(commit->buffer);
 175        commit->buffer = NULL;
 176}
 177
 178static void finish_object(struct object *obj,
 179                          const struct name_path *path, const char *name,
 180                          void *cb_data)
 181{
 182        struct rev_list_info *info = cb_data;
 183        if (obj->type == OBJ_BLOB && !has_sha1_file(obj->sha1))
 184                die("missing blob object '%s'", sha1_to_hex(obj->sha1));
 185        if (info->revs->verify_objects && !obj->parsed && obj->type != OBJ_COMMIT)
 186                parse_object(obj->sha1);
 187}
 188
 189static void show_object(struct object *obj,
 190                        const struct name_path *path, const char *component,
 191                        void *cb_data)
 192{
 193        struct rev_list_info *info = cb_data;
 194        finish_object(obj, path, component, cb_data);
 195        if (info->flags & REV_LIST_QUIET)
 196                return;
 197        show_object_with_name(stdout, obj, path, component);
 198}
 199
 200static void show_edge(struct commit *commit)
 201{
 202        printf("-%s\n", sha1_to_hex(commit->object.sha1));
 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[41] = "";
 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                strcpy(hex, sha1_to_hex(revs->commits->item->object.sha1));
 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
 260int cmd_rev_list(int argc, const char **argv, const char *prefix)
 261{
 262        struct rev_info revs;
 263        struct rev_list_info info;
 264        int i;
 265        int bisect_list = 0;
 266        int bisect_show_vars = 0;
 267        int bisect_find_all = 0;
 268
 269        git_config(git_default_config, NULL);
 270        init_revisions(&revs, prefix);
 271        revs.abbrev = DEFAULT_ABBREV;
 272        revs.commit_format = CMIT_FMT_UNSPECIFIED;
 273        argc = setup_revisions(argc, argv, &revs, NULL);
 274
 275        memset(&info, 0, sizeof(info));
 276        info.revs = &revs;
 277        if (revs.bisect)
 278                bisect_list = 1;
 279
 280        if (DIFF_OPT_TST(&revs.diffopt, QUICK))
 281                info.flags |= REV_LIST_QUIET;
 282        for (i = 1 ; i < argc; i++) {
 283                const char *arg = argv[i];
 284
 285                if (!strcmp(arg, "--header")) {
 286                        revs.verbose_header = 1;
 287                        continue;
 288                }
 289                if (!strcmp(arg, "--timestamp")) {
 290                        info.show_timestamp = 1;
 291                        continue;
 292                }
 293                if (!strcmp(arg, "--bisect")) {
 294                        bisect_list = 1;
 295                        continue;
 296                }
 297                if (!strcmp(arg, "--bisect-all")) {
 298                        bisect_list = 1;
 299                        bisect_find_all = 1;
 300                        info.flags |= BISECT_SHOW_ALL;
 301                        revs.show_decorations = 1;
 302                        continue;
 303                }
 304                if (!strcmp(arg, "--bisect-vars")) {
 305                        bisect_list = 1;
 306                        bisect_show_vars = 1;
 307                        continue;
 308                }
 309                usage(rev_list_usage);
 310
 311        }
 312        if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
 313                /* The command line has a --pretty  */
 314                info.hdr_termination = '\n';
 315                if (revs.commit_format == CMIT_FMT_ONELINE)
 316                        info.header_prefix = "";
 317                else
 318                        info.header_prefix = "commit ";
 319        }
 320        else if (revs.verbose_header)
 321                /* Only --header was specified */
 322                revs.commit_format = CMIT_FMT_RAW;
 323
 324        if ((!revs.commits &&
 325             (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
 326              !revs.pending.nr)) ||
 327            revs.diff)
 328                usage(rev_list_usage);
 329
 330        save_commit_buffer = (revs.verbose_header ||
 331                              revs.grep_filter.pattern_list ||
 332                              revs.grep_filter.header_list);
 333        if (bisect_list)
 334                revs.limited = 1;
 335
 336        if (prepare_revision_walk(&revs))
 337                die("revision walk setup failed");
 338        if (revs.tree_objects)
 339                mark_edges_uninteresting(&revs, show_edge);
 340
 341        if (bisect_list) {
 342                int reaches = reaches, all = all;
 343
 344                revs.commits = find_bisection(revs.commits, &reaches, &all,
 345                                              bisect_find_all);
 346
 347                if (bisect_show_vars)
 348                        return show_bisect_vars(&info, reaches, all);
 349        }
 350
 351        traverse_commit_list(&revs, show_commit, show_object, &info);
 352
 353        if (revs.count) {
 354                if (revs.left_right && revs.cherry_mark)
 355                        printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same);
 356                else if (revs.left_right)
 357                        printf("%d\t%d\n", revs.count_left, revs.count_right);
 358                else if (revs.cherry_mark)
 359                        printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same);
 360                else
 361                        printf("%d\n", revs.count_left + revs.count_right);
 362        }
 363
 364        return 0;
 365}