09e3a60bf6492a280baf4e09eaa29ede67c7c245
   1#include "cache.h"
   2#include "refs.h"
   3#include "tag.h"
   4#include "commit.h"
   5#include "tree.h"
   6#include "blob.h"
   7#include "tree-walk.h"
   8#include "diff.h"
   9#include "revision.h"
  10#include "list-objects.h"
  11#include "builtin.h"
  12
  13/* bits #0-15 in revision.h */
  14
  15#define COUNTED         (1u<<16)
  16
  17static const char rev_list_usage[] =
  18"git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
  19"  limiting output:\n"
  20"    --max-count=nr\n"
  21"    --max-age=epoch\n"
  22"    --min-age=epoch\n"
  23"    --sparse\n"
  24"    --no-merges\n"
  25"    --remove-empty\n"
  26"    --all\n"
  27"    --stdin\n"
  28"  ordering output:\n"
  29"    --topo-order\n"
  30"    --date-order\n"
  31"  formatting output:\n"
  32"    --parents\n"
  33"    --objects | --objects-edge\n"
  34"    --unpacked\n"
  35"    --header | --pretty\n"
  36"    --abbrev=nr | --no-abbrev\n"
  37"    --abbrev-commit\n"
  38"  special purpose:\n"
  39"    --bisect\n"
  40"    --bisect-vars"
  41;
  42
  43static struct rev_info revs;
  44
  45static int bisect_list;
  46static int show_timestamp;
  47static int hdr_termination;
  48static const char *header_prefix;
  49
  50static void show_commit(struct commit *commit)
  51{
  52        if (show_timestamp)
  53                printf("%lu ", commit->date);
  54        if (header_prefix)
  55                fputs(header_prefix, stdout);
  56        if (commit->object.flags & BOUNDARY)
  57                putchar('-');
  58        else if (revs.left_right) {
  59                if (commit->object.flags & SYMMETRIC_LEFT)
  60                        putchar('<');
  61                else
  62                        putchar('>');
  63        }
  64        if (revs.abbrev_commit && revs.abbrev)
  65                fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
  66                      stdout);
  67        else
  68                fputs(sha1_to_hex(commit->object.sha1), stdout);
  69        if (revs.parents) {
  70                struct commit_list *parents = commit->parents;
  71                while (parents) {
  72                        struct object *o = &(parents->item->object);
  73                        parents = parents->next;
  74                        if (o->flags & TMP_MARK)
  75                                continue;
  76                        printf(" %s", sha1_to_hex(o->sha1));
  77                        o->flags |= TMP_MARK;
  78                }
  79                /* TMP_MARK is a general purpose flag that can
  80                 * be used locally, but the user should clean
  81                 * things up after it is done with them.
  82                 */
  83                for (parents = commit->parents;
  84                     parents;
  85                     parents = parents->next)
  86                        parents->item->object.flags &= ~TMP_MARK;
  87        }
  88        if (revs.commit_format == CMIT_FMT_ONELINE)
  89                putchar(' ');
  90        else
  91                putchar('\n');
  92
  93        if (revs.verbose_header) {
  94                static char pretty_header[16384];
  95                pretty_print_commit(revs.commit_format, commit, ~0,
  96                                    pretty_header, sizeof(pretty_header),
  97                                    revs.abbrev, NULL, NULL, revs.relative_date);
  98                printf("%s%c", pretty_header, hdr_termination);
  99        }
 100        fflush(stdout);
 101        if (commit->parents) {
 102                free_commit_list(commit->parents);
 103                commit->parents = NULL;
 104        }
 105        free(commit->buffer);
 106        commit->buffer = NULL;
 107}
 108
 109static void show_object(struct object_array_entry *p)
 110{
 111        /* An object with name "foo\n0000000..." can be used to
 112         * confuse downstream git-pack-objects very badly.
 113         */
 114        const char *ep = strchr(p->name, '\n');
 115        if (ep) {
 116                printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
 117                       (int) (ep - p->name),
 118                       p->name);
 119        }
 120        else
 121                printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
 122}
 123
 124static void show_edge(struct commit *commit)
 125{
 126        printf("-%s\n", sha1_to_hex(commit->object.sha1));
 127}
 128
 129/*
 130 * This is a truly stupid algorithm, but it's only
 131 * used for bisection, and we just don't care enough.
 132 *
 133 * We care just barely enough to avoid recursing for
 134 * non-merge entries.
 135 */
 136static int count_distance(struct commit_list *entry)
 137{
 138        int nr = 0;
 139
 140        while (entry) {
 141                struct commit *commit = entry->item;
 142                struct commit_list *p;
 143
 144                if (commit->object.flags & (UNINTERESTING | COUNTED))
 145                        break;
 146                if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
 147                        nr++;
 148                commit->object.flags |= COUNTED;
 149                p = commit->parents;
 150                entry = p;
 151                if (p) {
 152                        p = p->next;
 153                        while (p) {
 154                                nr += count_distance(p);
 155                                p = p->next;
 156                        }
 157                }
 158        }
 159
 160        return nr;
 161}
 162
 163static void clear_distance(struct commit_list *list)
 164{
 165        while (list) {
 166                struct commit *commit = list->item;
 167                commit->object.flags &= ~COUNTED;
 168                list = list->next;
 169        }
 170}
 171
 172static struct commit_list *find_bisection(struct commit_list *list,
 173                                          int *reaches, int *all)
 174{
 175        int nr, closest;
 176        struct commit_list *p, *best;
 177
 178        nr = 0;
 179        p = list;
 180        while (p) {
 181                if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
 182                        nr++;
 183                p = p->next;
 184        }
 185        closest = -1;
 186        best = list;
 187        *all = nr;
 188
 189        for (p = list; p; p = p->next) {
 190                int distance, reach;
 191
 192                if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
 193                        continue;
 194
 195                distance = reach = count_distance(p);
 196                clear_distance(list);
 197                if (nr - distance < distance)
 198                        distance = nr - distance;
 199                if (distance > closest) {
 200                        best = p;
 201                        *reaches = reach;
 202                        closest = distance;
 203                }
 204        }
 205        if (best)
 206                best->next = NULL;
 207        return best;
 208}
 209
 210static inline int commit_interesting(struct commit_list *elem)
 211{
 212        unsigned flags = elem->item->object.flags;
 213        if (flags & UNINTERESTING)
 214                return 0;
 215        return (!revs.prune_fn || (flags & TREECHANGE));
 216}
 217
 218static inline int weight(struct commit_list *elem)
 219{
 220        return *((int*)(elem->item->util));
 221}
 222
 223static inline void weight_set(struct commit_list *elem, int weight)
 224{
 225        *((int*)(elem->item->util)) = weight;
 226}
 227
 228static int count_interesting_parents(struct commit_list *elem)
 229{
 230        int cnt = 0;
 231        if (!elem->item->parents)
 232                return cnt;
 233        for (elem = elem->item->parents; elem; elem = elem->next) {
 234                if (commit_interesting(elem))
 235                        cnt++;
 236        }
 237        return cnt;
 238}
 239
 240static struct commit_list *find_bisection_2(struct commit_list *list,
 241                                            int *reaches, int *all)
 242{
 243        int n, nr, counted, distance;
 244        struct commit_list *p, *best;
 245        int *weights;
 246
 247        for (nr = 0, p = list; p; p = p->next) {
 248                if (commit_interesting(p))
 249                        nr++;
 250        }
 251        *all = nr;
 252        weights = xcalloc(nr, sizeof(int*));
 253        counted = 0;
 254
 255        for (n = 0, p = list; p; p = p->next) {
 256                if (!commit_interesting(p))
 257                        continue;
 258                if (commit_interesting(p)) {
 259                        /*
 260                         * positive weight is the number of interesting
 261                         * commits it can reach, including itself.
 262                         * weight = 0 means it has one parent and
 263                         * its distance is unknown.
 264                         * weight < 0 means it has more than one
 265                         * parent and its distance is unknown.
 266                         */
 267                        p->item->util = &weights[n++];
 268                        switch (count_interesting_parents(p)) {
 269                        case 0:
 270                                weight_set(p, 1);
 271                                counted++;
 272                                break;
 273                        case 1:
 274                                weight_set(p, 0);
 275                                break;
 276                        default:
 277                                weight_set(p, -1);
 278                                break;
 279                        }
 280                }
 281        }
 282
 283        /*
 284         * If you have only one parent in the resulting set
 285         * then you can reach one commit more than that parent
 286         * can reach.  So we do not have to run the expensive
 287         * count_distance() for single strand of pearls.
 288         *
 289         * However, if you have more than one parents, you cannot
 290         * just add their distance and one for yourself, since
 291         * they usually reach the same ancestor and you would
 292         * end up counting them twice that way.
 293         *
 294         * So we will first count distance of merges the usual
 295         * way, and then fill the blanks using cheaper algorithm.
 296         */
 297        for (p = list; p; p = p->next) {
 298                if (!commit_interesting(p))
 299                        continue;
 300                n = weight(p);
 301                if (0 <= n)
 302                        continue;
 303                distance = count_distance(p);
 304                clear_distance(p);
 305                weight_set(p, distance);
 306
 307                /* Does it happen to be at exactly half-way? */
 308                distance *= 2;
 309                if (nr == distance || (nr+1) == distance) {
 310                        p->next = NULL;
 311                        *reaches = weight(p);
 312                        free(weights);
 313                        return p;
 314                }
 315                counted++;
 316        }
 317
 318        while (counted < nr) {
 319                for (p = list; p; p = p->next) {
 320                        struct commit_list *q;
 321
 322                        if (!commit_interesting(p) || 0 < weight(p))
 323                                continue;
 324                        for (q = p->item->parents; q; q = q->next)
 325                                if (commit_interesting(q) && 0 < weight(q))
 326                                        break;
 327                        if (!q)
 328                                continue;
 329                        weight_set(p, weight(q)+1);
 330                        counted++;
 331
 332                        /* Does it happen to be at exactly half-way? */
 333                        distance = weight(p) * 2;
 334                        if (nr == distance || (nr+1) == distance) {
 335                                p->next = NULL;
 336                                *reaches = weight(p);
 337                                free(weights);
 338                                return p;
 339                        }
 340                }
 341        }
 342
 343        /* Then find the best one */
 344        counted = 0;
 345        best = list;
 346        for (p = list; p; p = p->next) {
 347                if (!commit_interesting(p))
 348                        continue;
 349                distance = weight(p);
 350                if (nr - distance < distance)
 351                        distance = nr - distance;
 352                if (distance > counted) {
 353                        best = p;
 354                        counted = distance;
 355                        *reaches = weight(p);
 356                }
 357        }
 358        if (best)
 359                best->next = NULL;
 360        free(weights);
 361        return best;
 362}
 363
 364static void read_revisions_from_stdin(struct rev_info *revs)
 365{
 366        char line[1000];
 367
 368        while (fgets(line, sizeof(line), stdin) != NULL) {
 369                int len = strlen(line);
 370                if (line[len - 1] == '\n')
 371                        line[--len] = 0;
 372                if (!len)
 373                        break;
 374                if (line[0] == '-')
 375                        die("options not supported in --stdin mode");
 376                if (handle_revision_arg(line, revs, 0, 1))
 377                        die("bad revision '%s'", line);
 378        }
 379}
 380
 381int cmd_rev_list(int argc, const char **argv, const char *prefix)
 382{
 383        struct commit_list *list;
 384        int i;
 385        int read_from_stdin = 0;
 386        int bisect_show_vars = 0;
 387
 388        git_config(git_default_config);
 389        init_revisions(&revs, prefix);
 390        revs.abbrev = 0;
 391        revs.commit_format = CMIT_FMT_UNSPECIFIED;
 392        argc = setup_revisions(argc, argv, &revs, NULL);
 393
 394        for (i = 1 ; i < argc; i++) {
 395                const char *arg = argv[i];
 396
 397                if (!strcmp(arg, "--header")) {
 398                        revs.verbose_header = 1;
 399                        continue;
 400                }
 401                if (!strcmp(arg, "--timestamp")) {
 402                        show_timestamp = 1;
 403                        continue;
 404                }
 405                if (!strcmp(arg, "--bisect")) {
 406                        bisect_list = 1;
 407                        continue;
 408                }
 409                if (!strcmp(arg, "--bisect-vars")) {
 410                        bisect_list = 1;
 411                        bisect_show_vars = 1;
 412                        continue;
 413                }
 414                if (!strcmp(arg, "--stdin")) {
 415                        if (read_from_stdin++)
 416                                die("--stdin given twice?");
 417                        read_revisions_from_stdin(&revs);
 418                        continue;
 419                }
 420                usage(rev_list_usage);
 421
 422        }
 423        if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
 424                /* The command line has a --pretty  */
 425                hdr_termination = '\n';
 426                if (revs.commit_format == CMIT_FMT_ONELINE)
 427                        header_prefix = "";
 428                else
 429                        header_prefix = "commit ";
 430        }
 431        else if (revs.verbose_header)
 432                /* Only --header was specified */
 433                revs.commit_format = CMIT_FMT_RAW;
 434
 435        list = revs.commits;
 436
 437        if ((!list &&
 438             (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
 439              !revs.pending.nr)) ||
 440            revs.diff)
 441                usage(rev_list_usage);
 442
 443        save_commit_buffer = revs.verbose_header || revs.grep_filter;
 444        track_object_refs = 0;
 445        if (bisect_list)
 446                revs.limited = 1;
 447
 448        prepare_revision_walk(&revs);
 449        if (revs.tree_objects)
 450                mark_edges_uninteresting(revs.commits, &revs, show_edge);
 451
 452        if (bisect_list) {
 453                int reaches = reaches, all = all;
 454
 455                if (!revs.prune_fn)
 456                        revs.commits = find_bisection_2(revs.commits,
 457                                                        &reaches, &all);
 458                else
 459                        revs.commits = find_bisection(revs.commits,
 460                                                      &reaches, &all);
 461                if (bisect_show_vars) {
 462                        int cnt;
 463                        if (!revs.commits)
 464                                return 1;
 465                        /*
 466                         * revs.commits can reach "reaches" commits among
 467                         * "all" commits.  If it is good, then there are
 468                         * (all-reaches) commits left to be bisected.
 469                         * On the other hand, if it is bad, then the set
 470                         * to bisect is "reaches".
 471                         * A bisect set of size N has (N-1) commits further
 472                         * to test, as we already know one bad one.
 473                         */
 474                        cnt = all-reaches;
 475                        if (cnt < reaches)
 476                                cnt = reaches;
 477                        printf("bisect_rev=%s\n"
 478                               "bisect_nr=%d\n"
 479                               "bisect_good=%d\n"
 480                               "bisect_bad=%d\n"
 481                               "bisect_all=%d\n",
 482                               sha1_to_hex(revs.commits->item->object.sha1),
 483                               cnt - 1,
 484                               all - reaches - 1,
 485                               reaches - 1,
 486                               all);
 487                        return 0;
 488                }
 489        }
 490
 491        traverse_commit_list(&revs, show_commit, show_object);
 492
 493        return 0;
 494}