builtin-rev-list.con commit Revert "Allow branch.*.merge to talk about remote tracking branches." (756373d)
   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"
  40;
  41
  42static struct rev_info revs;
  43
  44static int bisect_list;
  45static int show_timestamp;
  46static int hdr_termination;
  47static const char *header_prefix;
  48
  49static void show_commit(struct commit *commit)
  50{
  51        if (show_timestamp)
  52                printf("%lu ", commit->date);
  53        if (header_prefix)
  54                fputs(header_prefix, stdout);
  55        if (commit->object.flags & BOUNDARY)
  56                putchar('-');
  57        else if (revs.left_right) {
  58                if (commit->object.flags & SYMMETRIC_LEFT)
  59                        putchar('<');
  60                else
  61                        putchar('>');
  62        }
  63        if (revs.abbrev_commit && revs.abbrev)
  64                fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
  65                      stdout);
  66        else
  67                fputs(sha1_to_hex(commit->object.sha1), stdout);
  68        if (revs.parents) {
  69                struct commit_list *parents = commit->parents;
  70                while (parents) {
  71                        struct object *o = &(parents->item->object);
  72                        parents = parents->next;
  73                        if (o->flags & TMP_MARK)
  74                                continue;
  75                        printf(" %s", sha1_to_hex(o->sha1));
  76                        o->flags |= TMP_MARK;
  77                }
  78                /* TMP_MARK is a general purpose flag that can
  79                 * be used locally, but the user should clean
  80                 * things up after it is done with them.
  81                 */
  82                for (parents = commit->parents;
  83                     parents;
  84                     parents = parents->next)
  85                        parents->item->object.flags &= ~TMP_MARK;
  86        }
  87        if (revs.commit_format == CMIT_FMT_ONELINE)
  88                putchar(' ');
  89        else
  90                putchar('\n');
  91
  92        if (revs.verbose_header) {
  93                static char pretty_header[16384];
  94                pretty_print_commit(revs.commit_format, commit, ~0,
  95                                    pretty_header, sizeof(pretty_header),
  96                                    revs.abbrev, NULL, NULL, revs.relative_date);
  97                printf("%s%c", pretty_header, hdr_termination);
  98        }
  99        fflush(stdout);
 100        if (commit->parents) {
 101                free_commit_list(commit->parents);
 102                commit->parents = NULL;
 103        }
 104        free(commit->buffer);
 105        commit->buffer = NULL;
 106}
 107
 108static void show_object(struct object_array_entry *p)
 109{
 110        /* An object with name "foo\n0000000..." can be used to
 111         * confuse downstream git-pack-objects very badly.
 112         */
 113        const char *ep = strchr(p->name, '\n');
 114        if (ep) {
 115                printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
 116                       (int) (ep - p->name),
 117                       p->name);
 118        }
 119        else
 120                printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
 121}
 122
 123static void show_edge(struct commit *commit)
 124{
 125        printf("-%s\n", sha1_to_hex(commit->object.sha1));
 126}
 127
 128/*
 129 * This is a truly stupid algorithm, but it's only
 130 * used for bisection, and we just don't care enough.
 131 *
 132 * We care just barely enough to avoid recursing for
 133 * non-merge entries.
 134 */
 135static int count_distance(struct commit_list *entry)
 136{
 137        int nr = 0;
 138
 139        while (entry) {
 140                struct commit *commit = entry->item;
 141                struct commit_list *p;
 142
 143                if (commit->object.flags & (UNINTERESTING | COUNTED))
 144                        break;
 145                if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
 146                        nr++;
 147                commit->object.flags |= COUNTED;
 148                p = commit->parents;
 149                entry = p;
 150                if (p) {
 151                        p = p->next;
 152                        while (p) {
 153                                nr += count_distance(p);
 154                                p = p->next;
 155                        }
 156                }
 157        }
 158
 159        return nr;
 160}
 161
 162static void clear_distance(struct commit_list *list)
 163{
 164        while (list) {
 165                struct commit *commit = list->item;
 166                commit->object.flags &= ~COUNTED;
 167                list = list->next;
 168        }
 169}
 170
 171static struct commit_list *find_bisection(struct commit_list *list)
 172{
 173        int nr, closest;
 174        struct commit_list *p, *best;
 175
 176        nr = 0;
 177        p = list;
 178        while (p) {
 179                if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
 180                        nr++;
 181                p = p->next;
 182        }
 183        closest = 0;
 184        best = list;
 185
 186        for (p = list; p; p = p->next) {
 187                int distance;
 188
 189                if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
 190                        continue;
 191
 192                distance = count_distance(p);
 193                clear_distance(list);
 194                if (nr - distance < distance)
 195                        distance = nr - distance;
 196                if (distance > closest) {
 197                        best = p;
 198                        closest = distance;
 199                }
 200        }
 201        if (best)
 202                best->next = NULL;
 203        return best;
 204}
 205
 206static void read_revisions_from_stdin(struct rev_info *revs)
 207{
 208        char line[1000];
 209
 210        while (fgets(line, sizeof(line), stdin) != NULL) {
 211                int len = strlen(line);
 212                if (line[len - 1] == '\n')
 213                        line[--len] = 0;
 214                if (!len)
 215                        break;
 216                if (line[0] == '-')
 217                        die("options not supported in --stdin mode");
 218                if (handle_revision_arg(line, revs, 0, 1))
 219                        die("bad revision '%s'", line);
 220        }
 221}
 222
 223int cmd_rev_list(int argc, const char **argv, const char *prefix)
 224{
 225        struct commit_list *list;
 226        int i;
 227        int read_from_stdin = 0;
 228
 229        init_revisions(&revs, prefix);
 230        revs.abbrev = 0;
 231        revs.commit_format = CMIT_FMT_UNSPECIFIED;
 232        argc = setup_revisions(argc, argv, &revs, NULL);
 233
 234        for (i = 1 ; i < argc; i++) {
 235                const char *arg = argv[i];
 236
 237                if (!strcmp(arg, "--header")) {
 238                        revs.verbose_header = 1;
 239                        continue;
 240                }
 241                if (!strcmp(arg, "--timestamp")) {
 242                        show_timestamp = 1;
 243                        continue;
 244                }
 245                if (!strcmp(arg, "--bisect")) {
 246                        bisect_list = 1;
 247                        continue;
 248                }
 249                if (!strcmp(arg, "--stdin")) {
 250                        if (read_from_stdin++)
 251                                die("--stdin given twice?");
 252                        read_revisions_from_stdin(&revs);
 253                        continue;
 254                }
 255                usage(rev_list_usage);
 256
 257        }
 258        if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
 259                /* The command line has a --pretty  */
 260                hdr_termination = '\n';
 261                if (revs.commit_format == CMIT_FMT_ONELINE)
 262                        header_prefix = "";
 263                else
 264                        header_prefix = "commit ";
 265        }
 266        else if (revs.verbose_header)
 267                /* Only --header was specified */
 268                revs.commit_format = CMIT_FMT_RAW;
 269
 270        list = revs.commits;
 271
 272        if ((!list &&
 273             (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
 274              !revs.pending.nr)) ||
 275            revs.diff)
 276                usage(rev_list_usage);
 277
 278        save_commit_buffer = revs.verbose_header || revs.grep_filter;
 279        track_object_refs = 0;
 280        if (bisect_list)
 281                revs.limited = 1;
 282
 283        prepare_revision_walk(&revs);
 284        if (revs.tree_objects)
 285                mark_edges_uninteresting(revs.commits, &revs, show_edge);
 286
 287        if (bisect_list)
 288                revs.commits = find_bisection(revs.commits);
 289
 290        traverse_commit_list(&revs, show_commit, show_object);
 291
 292        return 0;
 293}