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