builtin-rev-list.con commit rev-list --left-right (577ed5c)
   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;
  48static int show_left_right;
  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 (show_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{
 174        int nr, closest;
 175        struct commit_list *p, *best;
 176
 177        nr = 0;
 178        p = list;
 179        while (p) {
 180                if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
 181                        nr++;
 182                p = p->next;
 183        }
 184        closest = 0;
 185        best = list;
 186
 187        for (p = list; p; p = p->next) {
 188                int distance;
 189
 190                if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
 191                        continue;
 192
 193                distance = count_distance(p);
 194                clear_distance(list);
 195                if (nr - distance < distance)
 196                        distance = nr - distance;
 197                if (distance > closest) {
 198                        best = p;
 199                        closest = distance;
 200                }
 201        }
 202        if (best)
 203                best->next = NULL;
 204        return best;
 205}
 206
 207static void read_revisions_from_stdin(struct rev_info *revs)
 208{
 209        char line[1000];
 210
 211        while (fgets(line, sizeof(line), stdin) != NULL) {
 212                int len = strlen(line);
 213                if (line[len - 1] == '\n')
 214                        line[--len] = 0;
 215                if (!len)
 216                        break;
 217                if (line[0] == '-')
 218                        die("options not supported in --stdin mode");
 219                if (handle_revision_arg(line, revs, 0, 1))
 220                        die("bad revision '%s'", line);
 221        }
 222}
 223
 224int cmd_rev_list(int argc, const char **argv, const char *prefix)
 225{
 226        struct commit_list *list;
 227        int i;
 228        int read_from_stdin = 0;
 229
 230        init_revisions(&revs, prefix);
 231        revs.abbrev = 0;
 232        revs.commit_format = CMIT_FMT_UNSPECIFIED;
 233        argc = setup_revisions(argc, argv, &revs, NULL);
 234
 235        for (i = 1 ; i < argc; i++) {
 236                const char *arg = argv[i];
 237
 238                if (!strcmp(arg, "--header")) {
 239                        revs.verbose_header = 1;
 240                        continue;
 241                }
 242                if (!strcmp(arg, "--timestamp")) {
 243                        show_timestamp = 1;
 244                        continue;
 245                }
 246                if (!strcmp(arg, "--bisect")) {
 247                        bisect_list = 1;
 248                        continue;
 249                }
 250                if (!strcmp(arg, "--left-right")) {
 251                        show_left_right = 1;
 252                        continue;
 253                }
 254                if (!strcmp(arg, "--stdin")) {
 255                        if (read_from_stdin++)
 256                                die("--stdin given twice?");
 257                        read_revisions_from_stdin(&revs);
 258                        continue;
 259                }
 260                usage(rev_list_usage);
 261
 262        }
 263        if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
 264                /* The command line has a --pretty  */
 265                hdr_termination = '\n';
 266                if (revs.commit_format == CMIT_FMT_ONELINE)
 267                        header_prefix = "";
 268                else
 269                        header_prefix = "commit ";
 270        }
 271        else if (revs.verbose_header)
 272                /* Only --header was specified */
 273                revs.commit_format = CMIT_FMT_RAW;
 274
 275        list = revs.commits;
 276
 277        if ((!list &&
 278             (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
 279              !revs.pending.nr)) ||
 280            revs.diff)
 281                usage(rev_list_usage);
 282
 283        save_commit_buffer = revs.verbose_header || revs.grep_filter;
 284        track_object_refs = 0;
 285        if (bisect_list)
 286                revs.limited = 1;
 287
 288        prepare_revision_walk(&revs);
 289        if (revs.tree_objects)
 290                mark_edges_uninteresting(revs.commits, &revs, show_edge);
 291
 292        if (bisect_list)
 293                revs.commits = find_bisection(revs.commits);
 294
 295        traverse_commit_list(&revs, show_commit, show_object);
 296
 297        return 0;
 298}