rev-list.con commit git-rev-parse: parse ".." before simple SHA1's (800644c)
   1#include "cache.h"
   2#include "commit.h"
   3#include "epoch.h"
   4
   5#define SEEN            (1u << 0)
   6#define INTERESTING     (1u << 1)
   7#define COUNTED         (1u << 2)
   8#define SHOWN           (LAST_EPOCH_FLAG << 2)
   9
  10static const char rev_list_usage[] =
  11        "usage: git-rev-list [OPTION] commit-id <commit-id>\n"
  12                      "  --max-count=nr\n"
  13                      "  --max-age=epoch\n"
  14                      "  --min-age=epoch\n"
  15                      "  --header\n"
  16                      "  --pretty\n"
  17                      "  --merge-order [ --show-breaks ]";
  18
  19static int bisect_list = 0;
  20static int verbose_header = 0;
  21static int show_parents = 0;
  22static int hdr_termination = 0;
  23static const char *prefix = "";
  24static unsigned long max_age = -1;
  25static unsigned long min_age = -1;
  26static int max_count = -1;
  27static enum cmit_fmt commit_format = CMIT_FMT_RAW;
  28static int merge_order = 0;
  29static int show_breaks = 0;
  30static int stop_traversal = 0;
  31
  32static void show_commit(struct commit *commit)
  33{
  34        commit->object.flags |= SHOWN;
  35        if (show_breaks) {
  36                prefix = "| ";
  37                if (commit->object.flags & DISCONTINUITY) {
  38                        prefix = "^ ";     
  39                } else if (commit->object.flags & BOUNDARY) {
  40                        prefix = "= ";
  41                } 
  42        }                       
  43        printf("%s%s", prefix, sha1_to_hex(commit->object.sha1));
  44        if (show_parents) {
  45                struct commit_list *parents = commit->parents;
  46                while (parents) {
  47                        printf(" %s", sha1_to_hex(parents->item->object.sha1));
  48                        parents = parents->next;
  49                }
  50        }
  51        putchar('\n');
  52        if (verbose_header) {
  53                static char pretty_header[16384];
  54                pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
  55                printf("%s%c", pretty_header, hdr_termination);
  56        }       
  57}
  58
  59static int filter_commit(struct commit * commit)
  60{
  61        if (merge_order && stop_traversal && commit->object.flags & BOUNDARY)
  62                return STOP;
  63        if (commit->object.flags & (UNINTERESTING|SHOWN))
  64                return CONTINUE;
  65        if (min_age != -1 && (commit->date > min_age))
  66                return CONTINUE;
  67        if (max_age != -1 && (commit->date < max_age)) {
  68                if (!merge_order)
  69                        return STOP;
  70                else {
  71                        stop_traversal = 1;
  72                        return CONTINUE;
  73                }
  74        }
  75        if (max_count != -1 && !max_count--)
  76                return STOP;
  77        return DO;
  78}
  79
  80static int process_commit(struct commit * commit)
  81{
  82        int action=filter_commit(commit);
  83
  84        if (action == STOP) {
  85                return STOP;
  86        }
  87
  88        if (action == CONTINUE) {
  89                return CONTINUE;
  90        }
  91
  92        show_commit(commit);
  93
  94        return CONTINUE;
  95}
  96
  97static void show_commit_list(struct commit_list *list)
  98{
  99        while (list) {
 100                struct commit *commit = pop_most_recent_commit(&list, SEEN);
 101
 102                if (process_commit(commit) == STOP)
 103                        break;
 104        }
 105}
 106
 107static void mark_parents_uninteresting(struct commit *commit)
 108{
 109        struct commit_list *parents = commit->parents;
 110
 111        while (parents) {
 112                struct commit *commit = parents->item;
 113                commit->object.flags |= UNINTERESTING;
 114                parents = parents->next;
 115        }
 116}
 117
 118static int everybody_uninteresting(struct commit_list *list)
 119{
 120        while (list) {
 121                struct commit *commit = list->item;
 122                list = list->next;
 123                if (commit->object.flags & UNINTERESTING)
 124                        continue;
 125                return 0;
 126        }
 127        return 1;
 128}
 129
 130/*
 131 * This is a truly stupid algorithm, but it's only
 132 * used for bisection, and we just don't care enough.
 133 *
 134 * We care just barely enough to avoid recursing for
 135 * non-merge entries.
 136 */
 137static int count_distance(struct commit_list *entry)
 138{
 139        int nr = 0;
 140
 141        while (entry) {
 142                struct commit *commit = entry->item;
 143                struct commit_list *p;
 144
 145                if (commit->object.flags & (UNINTERESTING | COUNTED))
 146                        break;
 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        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                nr++;
 180                p = p->next;
 181        }
 182        closest = 0;
 183        best = list;
 184
 185        p = list;
 186        while (p) {
 187                int distance = count_distance(p);
 188                clear_distance(list);
 189                if (nr - distance < distance)
 190                        distance = nr - distance;
 191                if (distance > closest) {
 192                        best = p;
 193                        closest = distance;
 194                }
 195                p = p->next;
 196        }
 197        if (best)
 198                best->next = NULL;
 199        return best;
 200}
 201
 202struct commit_list *limit_list(struct commit_list *list)
 203{
 204        struct commit_list *newlist = NULL;
 205        struct commit_list **p = &newlist;
 206        do {
 207                struct commit *commit = pop_most_recent_commit(&list, SEEN);
 208                struct object *obj = &commit->object;
 209
 210                if (obj->flags & UNINTERESTING) {
 211                        mark_parents_uninteresting(commit);
 212                        if (everybody_uninteresting(list))
 213                                break;
 214                        continue;
 215                }
 216                p = &commit_list_insert(commit, p)->next;
 217        } while (list);
 218        if (bisect_list)
 219                newlist = find_bisection(newlist);
 220        return newlist;
 221}
 222
 223static enum cmit_fmt get_commit_format(const char *arg)
 224{
 225        if (!*arg)
 226                return CMIT_FMT_DEFAULT;
 227        if (!strcmp(arg, "=raw"))
 228                return CMIT_FMT_RAW;
 229        if (!strcmp(arg, "=medium"))
 230                return CMIT_FMT_MEDIUM;
 231        if (!strcmp(arg, "=short"))
 232                return CMIT_FMT_SHORT;
 233        usage(rev_list_usage);  
 234}                       
 235
 236
 237int main(int argc, char **argv)
 238{
 239        struct commit_list *list = NULL;
 240        int i, limited = 0;
 241
 242        for (i = 1 ; i < argc; i++) {
 243                int flags;
 244                char *arg = argv[i];
 245                unsigned char sha1[20];
 246                struct commit *commit;
 247
 248                if (!strncmp(arg, "--max-count=", 12)) {
 249                        max_count = atoi(arg + 12);
 250                        continue;
 251                }
 252                if (!strncmp(arg, "--max-age=", 10)) {
 253                        max_age = atoi(arg + 10);
 254                        continue;
 255                }
 256                if (!strncmp(arg, "--min-age=", 10)) {
 257                        min_age = atoi(arg + 10);
 258                        continue;
 259                }
 260                if (!strcmp(arg, "--header")) {
 261                        verbose_header = 1;
 262                        continue;
 263                }
 264                if (!strncmp(arg, "--pretty", 8)) {
 265                        commit_format = get_commit_format(arg+8);
 266                        verbose_header = 1;
 267                        hdr_termination = '\n';
 268                        prefix = "commit ";
 269                        continue;
 270                }
 271                if (!strcmp(arg, "--parents")) {
 272                        show_parents = 1;
 273                        continue;
 274                }
 275                if (!strcmp(arg, "--bisect")) {
 276                        bisect_list = 1;
 277                        continue;
 278                }
 279                if (!strncmp(arg, "--merge-order", 13)) {
 280                        merge_order = 1;
 281                        continue;
 282                }
 283                if (!strncmp(arg, "--show-breaks", 13)) {
 284                        show_breaks = 1;
 285                        continue;
 286                }
 287
 288                flags = 0;
 289                if (*arg == '^') {
 290                        flags = UNINTERESTING;
 291                        arg++;
 292                        limited = 1;
 293                }
 294                if (get_sha1(arg, sha1) || (show_breaks && !merge_order))
 295                        usage(rev_list_usage);
 296                commit = lookup_commit_reference(sha1);
 297                if (!commit || parse_commit(commit) < 0)
 298                        die("bad commit object %s", arg);
 299                commit->object.flags |= flags;
 300                commit_list_insert(commit, &list);
 301        }
 302
 303        if (!list)
 304                usage(rev_list_usage);
 305
 306        if (!merge_order) {             
 307                if (limited)
 308                        list = limit_list(list);
 309                show_commit_list(list);
 310        } else {
 311                if (sort_list_in_merge_order(list, &process_commit)) {
 312                          die("merge order sort failed\n");
 313                }
 314        }
 315
 316        return 0;
 317}