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