show-branch.con commit [PATCH] Possible cleanups for local-pull.c (2ab141a)
   1#include <stdlib.h>
   2#include "cache.h"
   3#include "commit.h"
   4#include "refs.h"
   5
   6static const char show_branch_usage[] =
   7"git-show-branch [--all] [--heads] [--tags] [--more=count] [<refs>...]";
   8
   9#define UNINTERESTING   01
  10
  11#define REV_SHIFT        2
  12#define MAX_REVS        29 /* should not exceed bits_per_int - REV_SHIFT */
  13
  14static struct commit *interesting(struct commit_list *list)
  15{
  16        while (list) {
  17                struct commit *commit = list->item;
  18                list = list->next;
  19                if (commit->object.flags & UNINTERESTING)
  20                        continue;
  21                return commit;
  22        }
  23        return NULL;
  24}
  25
  26static struct commit *pop_one_commit(struct commit_list **list_p)
  27{
  28        struct commit *commit;
  29        struct commit_list *list;
  30        list = *list_p;
  31        commit = list->item;
  32        *list_p = list->next;
  33        free(list);
  34        return commit;
  35}
  36
  37struct commit_name {
  38        const char *head_name; /* which head's ancestor? */
  39        int generation; /* how many parents away from head_name */
  40};
  41
  42/* Name the commit as nth generation ancestor of head_name;
  43 * we count only the first-parent relationship for naming purposes.
  44 */
  45static void name_commit(struct commit *commit, const char *head_name, int nth)
  46{
  47        struct commit_name *name;
  48        if (!commit->object.util)
  49                commit->object.util = xmalloc(sizeof(struct commit_name));
  50        name = commit->object.util;
  51        name->head_name = head_name;
  52        name->generation = nth;
  53}
  54
  55/* Parent is the first parent of the commit.  We may name it
  56 * as (n+1)th generation ancestor of the same head_name as
  57 * commit is nth generation ancestore of, if that generation
  58 * number is better than the name it already has.
  59 */
  60static void name_parent(struct commit *commit, struct commit *parent)
  61{
  62        struct commit_name *commit_name = commit->object.util;
  63        struct commit_name *parent_name = parent->object.util;
  64        if (!commit_name)
  65                return;
  66        if (!parent_name ||
  67            commit_name->generation + 1 < parent_name->generation)
  68                name_commit(parent, commit_name->head_name,
  69                            commit_name->generation + 1);
  70}
  71
  72static int name_first_parent_chain(struct commit *c)
  73{
  74        int i = 0;
  75        while (c) {
  76                struct commit *p;
  77                if (!c->object.util)
  78                        break;
  79                if (!c->parents)
  80                        break;
  81                p = c->parents->item;
  82                if (!p->object.util) {
  83                        name_parent(c, p);
  84                        i++;
  85                }
  86                c = p;
  87        }
  88        return i;
  89}
  90
  91static void name_commits(struct commit_list *list,
  92                         struct commit **rev,
  93                         char **ref_name,
  94                         int num_rev)
  95{
  96        struct commit_list *cl;
  97        struct commit *c;
  98        int i;
  99
 100        /* First give names to the given heads */
 101        for (cl = list; cl; cl = cl->next) {
 102                c = cl->item;
 103                if (c->object.util)
 104                        continue;
 105                for (i = 0; i < num_rev; i++) {
 106                        if (rev[i] == c) {
 107                                name_commit(c, ref_name[i], 0);
 108                                break;
 109                        }
 110                }
 111        }
 112
 113        /* Then commits on the first parent ancestry chain */
 114        do {
 115                i = 0;
 116                for (cl = list; cl; cl = cl->next) {
 117                        i += name_first_parent_chain(cl->item);
 118                }
 119        } while (i);
 120
 121        /* Finally, any unnamed commits */
 122        do {
 123                i = 0;
 124                for (cl = list; cl; cl = cl->next) {
 125                        struct commit_list *parents;
 126                        struct commit_name *n;
 127                        int nth;
 128                        c = cl->item;
 129                        if (!c->object.util)
 130                                continue;
 131                        n = c->object.util;
 132                        parents = c->parents;
 133                        nth = 0;
 134                        while (parents) {
 135                                struct commit *p = parents->item;
 136                                char newname[1000];
 137                                parents = parents->next;
 138                                nth++;
 139                                if (p->object.util)
 140                                        continue;
 141                                sprintf(newname, "%s^%d", n->head_name, nth);
 142                                name_commit(p, strdup(newname), 0);
 143                                i++;
 144                                name_first_parent_chain(p);
 145                        }
 146                }
 147        } while (i);
 148}
 149
 150static int mark_seen(struct commit *commit, struct commit_list **seen_p)
 151{
 152        if (!commit->object.flags) {
 153                insert_by_date(commit, seen_p);
 154                return 1;
 155        }
 156        return 0;
 157}
 158
 159static void join_revs(struct commit_list **list_p,
 160                      struct commit_list **seen_p,
 161                      int num_rev, int extra)
 162{
 163        int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
 164        int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
 165
 166        while (*list_p) {
 167                struct commit_list *parents;
 168                struct commit *commit = pop_one_commit(list_p);
 169                int flags = commit->object.flags & all_mask;
 170                int still_interesting = !!interesting(*list_p);
 171
 172                if (!still_interesting && extra < 0)
 173                        break;
 174
 175                mark_seen(commit, seen_p);
 176                if ((flags & all_revs) == all_revs)
 177                        flags |= UNINTERESTING;
 178                parents = commit->parents;
 179
 180                while (parents) {
 181                        struct commit *p = parents->item;
 182                        int this_flag = p->object.flags;
 183                        parents = parents->next;
 184                        if ((this_flag & flags) == flags)
 185                                continue;
 186                        parse_commit(p);
 187                        if (mark_seen(p, seen_p) && !still_interesting)
 188                                extra--;
 189                        p->object.flags |= flags;
 190                        insert_by_date(p, list_p);
 191                }
 192        }
 193}
 194
 195static void show_one_commit(struct commit *commit)
 196{
 197        char pretty[128], *cp;
 198        struct commit_name *name = commit->object.util;
 199        pretty_print_commit(CMIT_FMT_ONELINE, commit->buffer, ~0,
 200                            pretty, sizeof(pretty));
 201        if (!strncmp(pretty, "[PATCH] ", 8))
 202                cp = pretty + 8;
 203        else
 204                cp = pretty;
 205        if (name && name->head_name) {
 206                printf("[%s", name->head_name);
 207                if (name->generation)
 208                        printf("~%d", name->generation);
 209                printf("] ");
 210        }
 211        puts(cp);
 212}
 213
 214static char *ref_name[MAX_REVS + 1];
 215static int ref_name_cnt;
 216
 217static int compare_ref_name(const void *a_, const void *b_)
 218{
 219        const char * const*a = a_, * const*b = b_;
 220        return strcmp(*a, *b);
 221}
 222
 223static void sort_ref_range(int bottom, int top)
 224{
 225        qsort(ref_name + bottom, top - bottom, sizeof(ref_name[0]),
 226              compare_ref_name);
 227}
 228
 229static int append_ref(const char *refname, const unsigned char *sha1)
 230{
 231        struct commit *commit = lookup_commit_reference_gently(sha1, 1);
 232        if (!commit)
 233                return 0;
 234        if (MAX_REVS < ref_name_cnt) {
 235                fprintf(stderr, "warning: ignoring %s; "
 236                        "cannot handle more than %d refs",
 237                        refname, MAX_REVS);
 238                return 0;
 239        }
 240        ref_name[ref_name_cnt++] = strdup(refname);
 241        ref_name[ref_name_cnt] = NULL;
 242        return 0;
 243}
 244
 245static int append_head_ref(const char *refname, const unsigned char *sha1)
 246{
 247        if (strncmp(refname, "refs/heads/", 11))
 248                return 0;
 249        return append_ref(refname + 11, sha1);
 250}
 251
 252static int append_tag_ref(const char *refname, const unsigned char *sha1)
 253{
 254        if (strncmp(refname, "refs/tags/", 10))
 255                return 0;
 256        return append_ref(refname + 5, sha1);
 257}
 258
 259static void snarf_refs(int head, int tag)
 260{
 261        if (head) {
 262                int orig_cnt = ref_name_cnt;
 263                for_each_ref(append_head_ref);
 264                sort_ref_range(orig_cnt, ref_name_cnt);
 265        }
 266        if (tag) {
 267                int orig_cnt = ref_name_cnt;
 268                for_each_ref(append_tag_ref);
 269                sort_ref_range(orig_cnt, ref_name_cnt);
 270        }
 271}
 272
 273static int rev_is_head(char *head_path, int headlen,
 274                       char *name,
 275                       unsigned char *head_sha1, unsigned char *sha1)
 276{
 277        int namelen;
 278        if ((!head_path[0]) || memcmp(head_sha1, sha1, 20))
 279                return 0;
 280        namelen = strlen(name);
 281        if ((headlen < namelen) ||
 282            memcmp(head_path + headlen - namelen, name, namelen))
 283                return 0;
 284        if (headlen == namelen ||
 285            head_path[headlen - namelen - 1] == '/')
 286                return 1;
 287        return 0;
 288}
 289
 290static int show_merge_base(struct commit_list *seen, int num_rev)
 291{
 292        int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
 293        int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
 294
 295        while (seen) {
 296                struct commit *commit = pop_one_commit(&seen);
 297                int flags = commit->object.flags & all_mask;
 298                if (!(flags & UNINTERESTING) &&
 299                    ((flags & all_revs) == all_revs)) {
 300                        puts(sha1_to_hex(commit->object.sha1));
 301                        return 0;
 302                }
 303        }
 304        return 1;
 305}
 306
 307int main(int ac, char **av)
 308{
 309        struct commit *rev[MAX_REVS], *commit;
 310        struct commit_list *list = NULL, *seen = NULL;
 311        int num_rev, i, extra = 0;
 312        int all_heads = 0, all_tags = 0;
 313        int all_mask, all_revs, shown_merge_point;
 314        char head_path[128];
 315        int head_path_len;
 316        unsigned char head_sha1[20];
 317        int merge_base = 0;
 318        char **label;
 319
 320        setup_git_directory();
 321
 322        while (1 < ac && av[1][0] == '-') {
 323                char *arg = av[1];
 324                if (!strcmp(arg, "--all"))
 325                        all_heads = all_tags = 1;
 326                else if (!strcmp(arg, "--heads"))
 327                        all_heads = 1;
 328                else if (!strcmp(arg, "--tags"))
 329                        all_tags = 1;
 330                else if (!strcmp(arg, "--more"))
 331                        extra = 1;
 332                else if (!strncmp(arg, "--more=", 7)) {
 333                        extra = atoi(arg + 7);
 334                        if (extra < 0)
 335                                usage(show_branch_usage);
 336                }
 337                else if (!strcmp(arg, "--merge-base"))
 338                        merge_base = 1;
 339                else
 340                        usage(show_branch_usage);
 341                ac--; av++;
 342        }
 343        ac--; av++;
 344
 345        if (all_heads + all_tags)
 346                snarf_refs(all_heads, all_tags);
 347
 348        while (0 < ac) {
 349                unsigned char revkey[20];
 350                if (get_sha1(*av, revkey))
 351                        die("bad sha1 reference %s", *av);
 352                append_ref(*av, revkey);
 353                ac--; av++;
 354        }
 355
 356        /* If still no revs, then add heads */
 357        if (!ref_name_cnt)
 358                snarf_refs(1, 0);
 359
 360        for (num_rev = 0; ref_name[num_rev]; num_rev++) {
 361                unsigned char revkey[20];
 362
 363                if (MAX_REVS <= num_rev)
 364                        die("cannot handle more than %d revs.", MAX_REVS);
 365                if (get_sha1(ref_name[num_rev], revkey))
 366                        usage(show_branch_usage);
 367                commit = lookup_commit_reference(revkey);
 368                if (!commit)
 369                        die("cannot find commit %s (%s)",
 370                            ref_name[num_rev], revkey);
 371                parse_commit(commit);
 372                mark_seen(commit, &seen);
 373
 374                /* rev#0 uses bit REV_SHIFT, rev#1 uses bit REV_SHIFT+1,
 375                 * and so on.  REV_SHIFT bits from bit 0 are used for
 376                 * internal bookkeeping.
 377                 */
 378                commit->object.flags |= 1u << (num_rev + REV_SHIFT);
 379                insert_by_date(commit, &list);
 380                rev[num_rev] = commit;
 381        }
 382        join_revs(&list, &seen, num_rev, extra);
 383
 384        head_path_len = readlink(".git/HEAD", head_path, sizeof(head_path)-1);
 385        if ((head_path_len < 0) || get_sha1("HEAD", head_sha1))
 386                head_path[0] = 0;
 387        else
 388                head_path[head_path_len] = 0;
 389
 390        if (merge_base)
 391                return show_merge_base(seen, num_rev);
 392
 393        /* Show list */
 394        if (1 < num_rev) {
 395                for (i = 0; i < num_rev; i++) {
 396                        int j;
 397                        int is_head = rev_is_head(head_path,
 398                                                  head_path_len,
 399                                                  ref_name[i],
 400                                                  head_sha1,
 401                                                  rev[i]->object.sha1);
 402                        for (j = 0; j < i; j++)
 403                                putchar(' ');
 404                        printf("%c [%s] ", is_head ? '*' : '!', ref_name[i]);
 405                        show_one_commit(rev[i]);
 406                }
 407                for (i = 0; i < num_rev; i++)
 408                        putchar('-');
 409                putchar('\n');
 410        }
 411
 412        /* Sort topologically */
 413        sort_in_topological_order(&seen);
 414
 415        /* Give names to commits */
 416        name_commits(seen, rev, ref_name, num_rev);
 417
 418        all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
 419        all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
 420        shown_merge_point = 0;
 421
 422        while (seen) {
 423                struct commit *commit = pop_one_commit(&seen);
 424                int this_flag = commit->object.flags;
 425                int is_merge_point = (this_flag & all_revs) == all_revs;
 426                static char *obvious[] = { "" };
 427
 428                if (is_merge_point)
 429                        shown_merge_point = 1;
 430
 431                if (1 < num_rev) {
 432                        for (i = 0; i < num_rev; i++)
 433                                putchar((this_flag & (1u << (i + REV_SHIFT)))
 434                                        ? '+' : ' ');
 435                        putchar(' ');
 436                }
 437                show_one_commit(commit);
 438                if (num_rev == 1)
 439                        label = obvious;
 440                if (shown_merge_point && is_merge_point)
 441                        if (--extra < 0)
 442                                break;
 443        }
 444        return 0;
 445}