builtin-rev-list.con commit Merge branch 'eb/mailinfo' into next (33b2926)
   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 "builtin.h"
  11
  12/* bits #0-15 in revision.h */
  13
  14#define COUNTED         (1u<<16)
  15
  16static const char rev_list_usage[] =
  17"git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
  18"  limiting output:\n"
  19"    --max-count=nr\n"
  20"    --max-age=epoch\n"
  21"    --min-age=epoch\n"
  22"    --sparse\n"
  23"    --no-merges\n"
  24"    --remove-empty\n"
  25"    --all\n"
  26"  ordering output:\n"
  27"    --topo-order\n"
  28"    --date-order\n"
  29"  formatting output:\n"
  30"    --parents\n"
  31"    --objects | --objects-edge\n"
  32"    --unpacked\n"
  33"    --header | --pretty\n"
  34"    --abbrev=nr | --no-abbrev\n"
  35"    --abbrev-commit\n"
  36"  special purpose:\n"
  37"    --bisect"
  38;
  39
  40static struct rev_info revs;
  41
  42static int bisect_list = 0;
  43static int show_timestamp = 0;
  44static int hdr_termination = 0;
  45static const char *header_prefix;
  46
  47static void show_commit(struct commit *commit)
  48{
  49        if (show_timestamp)
  50                printf("%lu ", commit->date);
  51        if (header_prefix)
  52                fputs(header_prefix, stdout);
  53        if (commit->object.flags & BOUNDARY)
  54                putchar('-');
  55        if (revs.abbrev_commit && revs.abbrev)
  56                fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
  57                      stdout);
  58        else
  59                fputs(sha1_to_hex(commit->object.sha1), stdout);
  60        if (revs.parents) {
  61                struct commit_list *parents = commit->parents;
  62                while (parents) {
  63                        struct object *o = &(parents->item->object);
  64                        parents = parents->next;
  65                        if (o->flags & TMP_MARK)
  66                                continue;
  67                        printf(" %s", sha1_to_hex(o->sha1));
  68                        o->flags |= TMP_MARK;
  69                }
  70                /* TMP_MARK is a general purpose flag that can
  71                 * be used locally, but the user should clean
  72                 * things up after it is done with them.
  73                 */
  74                for (parents = commit->parents;
  75                     parents;
  76                     parents = parents->next)
  77                        parents->item->object.flags &= ~TMP_MARK;
  78        }
  79        if (revs.commit_format == CMIT_FMT_ONELINE)
  80                putchar(' ');
  81        else
  82                putchar('\n');
  83
  84        if (revs.verbose_header) {
  85                static char pretty_header[16384];
  86                pretty_print_commit(revs.commit_format, commit, ~0,
  87                                    pretty_header, sizeof(pretty_header),
  88                                    revs.abbrev, NULL, NULL);
  89                printf("%s%c", pretty_header, hdr_termination);
  90        }
  91        fflush(stdout);
  92}
  93
  94static struct object_list **process_blob(struct blob *blob,
  95                                         struct object_list **p,
  96                                         struct name_path *path,
  97                                         const char *name)
  98{
  99        struct object *obj = &blob->object;
 100
 101        if (!revs.blob_objects)
 102                return p;
 103        if (obj->flags & (UNINTERESTING | SEEN))
 104                return p;
 105        obj->flags |= SEEN;
 106        return add_object(obj, p, path, name);
 107}
 108
 109static struct object_list **process_tree(struct tree *tree,
 110                                         struct object_list **p,
 111                                         struct name_path *path,
 112                                         const char *name)
 113{
 114        struct object *obj = &tree->object;
 115        struct tree_entry_list *entry;
 116        struct name_path me;
 117
 118        if (!revs.tree_objects)
 119                return p;
 120        if (obj->flags & (UNINTERESTING | SEEN))
 121                return p;
 122        if (parse_tree(tree) < 0)
 123                die("bad tree object %s", sha1_to_hex(obj->sha1));
 124        obj->flags |= SEEN;
 125        p = add_object(obj, p, path, name);
 126        me.up = path;
 127        me.elem = name;
 128        me.elem_len = strlen(name);
 129        entry = tree->entries;
 130        tree->entries = NULL;
 131        while (entry) {
 132                struct tree_entry_list *next = entry->next;
 133                if (entry->directory)
 134                        p = process_tree(entry->item.tree, p, &me, entry->name);
 135                else
 136                        p = process_blob(entry->item.blob, p, &me, entry->name);
 137                free(entry);
 138                entry = next;
 139        }
 140        return p;
 141}
 142
 143static void show_commit_list(struct rev_info *revs)
 144{
 145        struct commit *commit;
 146        struct object_list *objects = NULL, **p = &objects, *pending;
 147
 148        while ((commit = get_revision(revs)) != NULL) {
 149                p = process_tree(commit->tree, p, NULL, "");
 150                show_commit(commit);
 151        }
 152        for (pending = revs->pending_objects; pending; pending = pending->next) {
 153                struct object *obj = pending->item;
 154                const char *name = pending->name;
 155                if (obj->flags & (UNINTERESTING | SEEN))
 156                        continue;
 157                if (obj->type == tag_type) {
 158                        obj->flags |= SEEN;
 159                        p = add_object(obj, p, NULL, name);
 160                        continue;
 161                }
 162                if (obj->type == tree_type) {
 163                        p = process_tree((struct tree *)obj, p, NULL, name);
 164                        continue;
 165                }
 166                if (obj->type == blob_type) {
 167                        p = process_blob((struct blob *)obj, p, NULL, name);
 168                        continue;
 169                }
 170                die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
 171        }
 172        while (objects) {
 173                /* An object with name "foo\n0000000..." can be used to
 174                 * confuse downstream git-pack-objects very badly.
 175                 */
 176                const char *ep = strchr(objects->name, '\n');
 177                if (ep) {
 178                        printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
 179                               (int) (ep - objects->name),
 180                               objects->name);
 181                }
 182                else
 183                        printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
 184                objects = objects->next;
 185        }
 186}
 187
 188/*
 189 * This is a truly stupid algorithm, but it's only
 190 * used for bisection, and we just don't care enough.
 191 *
 192 * We care just barely enough to avoid recursing for
 193 * non-merge entries.
 194 */
 195static int count_distance(struct commit_list *entry)
 196{
 197        int nr = 0;
 198
 199        while (entry) {
 200                struct commit *commit = entry->item;
 201                struct commit_list *p;
 202
 203                if (commit->object.flags & (UNINTERESTING | COUNTED))
 204                        break;
 205                if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
 206                        nr++;
 207                commit->object.flags |= COUNTED;
 208                p = commit->parents;
 209                entry = p;
 210                if (p) {
 211                        p = p->next;
 212                        while (p) {
 213                                nr += count_distance(p);
 214                                p = p->next;
 215                        }
 216                }
 217        }
 218
 219        return nr;
 220}
 221
 222static void clear_distance(struct commit_list *list)
 223{
 224        while (list) {
 225                struct commit *commit = list->item;
 226                commit->object.flags &= ~COUNTED;
 227                list = list->next;
 228        }
 229}
 230
 231static struct commit_list *find_bisection(struct commit_list *list)
 232{
 233        int nr, closest;
 234        struct commit_list *p, *best;
 235
 236        nr = 0;
 237        p = list;
 238        while (p) {
 239                if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
 240                        nr++;
 241                p = p->next;
 242        }
 243        closest = 0;
 244        best = list;
 245
 246        for (p = list; p; p = p->next) {
 247                int distance;
 248
 249                if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
 250                        continue;
 251
 252                distance = count_distance(p);
 253                clear_distance(list);
 254                if (nr - distance < distance)
 255                        distance = nr - distance;
 256                if (distance > closest) {
 257                        best = p;
 258                        closest = distance;
 259                }
 260        }
 261        if (best)
 262                best->next = NULL;
 263        return best;
 264}
 265
 266static void mark_edge_parents_uninteresting(struct commit *commit)
 267{
 268        struct commit_list *parents;
 269
 270        for (parents = commit->parents; parents; parents = parents->next) {
 271                struct commit *parent = parents->item;
 272                if (!(parent->object.flags & UNINTERESTING))
 273                        continue;
 274                mark_tree_uninteresting(parent->tree);
 275                if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
 276                        parent->object.flags |= SHOWN;
 277                        printf("-%s\n", sha1_to_hex(parent->object.sha1));
 278                }
 279        }
 280}
 281
 282static void mark_edges_uninteresting(struct commit_list *list)
 283{
 284        for ( ; list; list = list->next) {
 285                struct commit *commit = list->item;
 286
 287                if (commit->object.flags & UNINTERESTING) {
 288                        mark_tree_uninteresting(commit->tree);
 289                        continue;
 290                }
 291                mark_edge_parents_uninteresting(commit);
 292        }
 293}
 294
 295int cmd_rev_list(int argc, const char **argv, char **envp)
 296{
 297        struct commit_list *list;
 298        int i;
 299
 300        init_revisions(&revs);
 301        revs.abbrev = 0;
 302        revs.commit_format = CMIT_FMT_UNSPECIFIED;
 303        argc = setup_revisions(argc, argv, &revs, NULL);
 304
 305        for (i = 1 ; i < argc; i++) {
 306                const char *arg = argv[i];
 307
 308                if (!strcmp(arg, "--header")) {
 309                        revs.verbose_header = 1;
 310                        continue;
 311                }
 312                if (!strcmp(arg, "--timestamp")) {
 313                        show_timestamp = 1;
 314                        continue;
 315                }
 316                if (!strcmp(arg, "--bisect")) {
 317                        bisect_list = 1;
 318                        continue;
 319                }
 320                usage(rev_list_usage);
 321
 322        }
 323        if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
 324                /* The command line has a --pretty  */
 325                hdr_termination = '\n';
 326                if (revs.commit_format == CMIT_FMT_ONELINE)
 327                        header_prefix = "";
 328                else
 329                        header_prefix = "commit ";
 330        }
 331        else if (revs.verbose_header)
 332                /* Only --header was specified */
 333                revs.commit_format = CMIT_FMT_RAW;
 334
 335        list = revs.commits;
 336
 337        if ((!list &&
 338             (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
 339              !revs.pending_objects)) ||
 340            revs.diff)
 341                usage(rev_list_usage);
 342
 343        save_commit_buffer = revs.verbose_header;
 344        track_object_refs = 0;
 345        if (bisect_list)
 346                revs.limited = 1;
 347
 348        prepare_revision_walk(&revs);
 349        if (revs.tree_objects)
 350                mark_edges_uninteresting(revs.commits);
 351
 352        if (bisect_list)
 353                revs.commits = find_bisection(revs.commits);
 354
 355        show_commit_list(&revs);
 356
 357        return 0;
 358}