builtin-describe.con commit If abbrev is set to zero in git-describe, don't add the unique suffix (f127404)
   1#include "cache.h"
   2#include "commit.h"
   3#include "tag.h"
   4#include "refs.h"
   5#include "builtin.h"
   6
   7#define SEEN            (1u<<0)
   8#define MAX_TAGS        (FLAG_BITS - 1)
   9
  10static const char describe_usage[] =
  11"git-describe [--all] [--tags] [--abbrev=<n>] <committish>*";
  12
  13static int debug;       /* Display lots of verbose info */
  14static int all; /* Default to annotated tags only */
  15static int tags;        /* But allow any tags if --tags is specified */
  16static int abbrev = DEFAULT_ABBREV;
  17static int max_candidates = 10;
  18
  19struct commit_name {
  20        int prio; /* annotated tag = 2, tag = 1, head = 0 */
  21        char path[FLEX_ARRAY]; /* more */
  22};
  23static const char *prio_names[] = {
  24        "head", "lightweight", "annotated",
  25};
  26
  27static void add_to_known_names(const char *path,
  28                               struct commit *commit,
  29                               int prio)
  30{
  31        struct commit_name *e = commit->util;
  32        if (!e || e->prio < prio) {
  33                size_t len = strlen(path)+1;
  34                free(e);
  35                e = xmalloc(sizeof(struct commit_name) + len);
  36                e->prio = prio;
  37                memcpy(e->path, path, len);
  38                commit->util = e;
  39        }
  40}
  41
  42static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
  43{
  44        struct commit *commit = lookup_commit_reference_gently(sha1, 1);
  45        struct object *object;
  46        int prio;
  47
  48        if (!commit)
  49                return 0;
  50        object = parse_object(sha1);
  51        /* If --all, then any refs are used.
  52         * If --tags, then any tags are used.
  53         * Otherwise only annotated tags are used.
  54         */
  55        if (!strncmp(path, "refs/tags/", 10)) {
  56                if (object->type == OBJ_TAG)
  57                        prio = 2;
  58                else
  59                        prio = 1;
  60        }
  61        else
  62                prio = 0;
  63
  64        if (!all) {
  65                if (!prio)
  66                        return 0;
  67                if (!tags && prio < 2)
  68                        return 0;
  69        }
  70        add_to_known_names(all ? path + 5 : path + 10, commit, prio);
  71        return 0;
  72}
  73
  74struct possible_tag {
  75        struct commit_name *name;
  76        int depth;
  77        int found_order;
  78        unsigned flag_within;
  79};
  80
  81static int compare_pt(const void *a_, const void *b_)
  82{
  83        struct possible_tag *a = (struct possible_tag *)a_;
  84        struct possible_tag *b = (struct possible_tag *)b_;
  85        if (a->name->prio != b->name->prio)
  86                return b->name->prio - a->name->prio;
  87        if (a->depth != b->depth)
  88                return a->depth - b->depth;
  89        if (a->found_order != b->found_order)
  90                return a->found_order - b->found_order;
  91        return 0;
  92}
  93
  94static void describe(const char *arg, int last_one)
  95{
  96        unsigned char sha1[20];
  97        struct commit *cmit, *gave_up_on = NULL;
  98        struct commit_list *list;
  99        static int initialized = 0;
 100        struct commit_name *n;
 101        struct possible_tag all_matches[MAX_TAGS];
 102        unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
 103        unsigned long seen_commits = 0;
 104
 105        if (get_sha1(arg, sha1))
 106                die("Not a valid object name %s", arg);
 107        cmit = lookup_commit_reference(sha1);
 108        if (!cmit)
 109                die("%s is not a valid '%s' object", arg, commit_type);
 110
 111        if (!initialized) {
 112                initialized = 1;
 113                for_each_ref(get_name, NULL);
 114        }
 115
 116        n = cmit->util;
 117        if (n) {
 118                printf("%s\n", n->path);
 119                return;
 120        }
 121
 122        if (debug)
 123                fprintf(stderr, "searching to describe %s\n", arg);
 124
 125        list = NULL;
 126        cmit->object.flags = SEEN;
 127        commit_list_insert(cmit, &list);
 128        while (list) {
 129                struct commit *c = pop_commit(&list);
 130                struct commit_list *parents = c->parents;
 131                seen_commits++;
 132                n = c->util;
 133                if (n) {
 134                        if (match_cnt < max_candidates) {
 135                                struct possible_tag *t = &all_matches[match_cnt++];
 136                                t->name = n;
 137                                t->depth = seen_commits - 1;
 138                                t->flag_within = 1u << match_cnt;
 139                                t->found_order = match_cnt;
 140                                c->object.flags |= t->flag_within;
 141                                if (n->prio == 2)
 142                                        annotated_cnt++;
 143                        }
 144                        else {
 145                                gave_up_on = c;
 146                                break;
 147                        }
 148                }
 149                for (cur_match = 0; cur_match < match_cnt; cur_match++) {
 150                        struct possible_tag *t = &all_matches[cur_match];
 151                        if (!(c->object.flags & t->flag_within))
 152                                t->depth++;
 153                }
 154                if (annotated_cnt && !list) {
 155                        if (debug)
 156                                fprintf(stderr, "finished search at %s\n",
 157                                        sha1_to_hex(c->object.sha1));
 158                        break;
 159                }
 160                while (parents) {
 161                        struct commit *p = parents->item;
 162                        parse_commit(p);
 163                        if (!(p->object.flags & SEEN))
 164                                insert_by_date(p, &list);
 165                        p->object.flags |= c->object.flags;
 166                        parents = parents->next;
 167                }
 168        }
 169        free_commit_list(list);
 170
 171        if (!match_cnt)
 172                die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
 173
 174        qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
 175        if (debug) {
 176                for (cur_match = 0; cur_match < match_cnt; cur_match++) {
 177                        struct possible_tag *t = &all_matches[cur_match];
 178                        fprintf(stderr, " %-11s %8d %s\n",
 179                                prio_names[t->name->prio],
 180                                t->depth, t->name->path);
 181                }
 182                fprintf(stderr, "traversed %lu commits\n", seen_commits);
 183                if (gave_up_on) {
 184                        fprintf(stderr,
 185                                "more than %i tags found; listed %i most recent\n"
 186                                "gave up search at %s\n",
 187                                max_candidates, max_candidates,
 188                                sha1_to_hex(gave_up_on->object.sha1));
 189                }
 190        }
 191        if (abbrev == 0)
 192                printf("%s\n", all_matches[0].name->path );
 193        else
 194                printf("%s-g%s\n", all_matches[0].name->path,
 195                           find_unique_abbrev(cmit->object.sha1, abbrev));
 196
 197        if (!last_one)
 198                clear_commit_marks(cmit, -1);
 199}
 200
 201int cmd_describe(int argc, const char **argv, const char *prefix)
 202{
 203        int i;
 204
 205        for (i = 1; i < argc; i++) {
 206                const char *arg = argv[i];
 207
 208                if (*arg != '-')
 209                        break;
 210                else if (!strcmp(arg, "--debug"))
 211                        debug = 1;
 212                else if (!strcmp(arg, "--all"))
 213                        all = 1;
 214                else if (!strcmp(arg, "--tags"))
 215                        tags = 1;
 216                else if (!strncmp(arg, "--abbrev=", 9)) {
 217                        abbrev = strtoul(arg + 9, NULL, 10);
 218                        if (abbrev != 0 && (abbrev < MINIMUM_ABBREV || 40 < abbrev))
 219                                abbrev = DEFAULT_ABBREV;
 220                }
 221                else if (!strncmp(arg, "--candidates=", 13)) {
 222                        max_candidates = strtoul(arg + 13, NULL, 10);
 223                        if (max_candidates < 1)
 224                                max_candidates = 1;
 225                        else if (max_candidates > MAX_TAGS)
 226                                max_candidates = MAX_TAGS;
 227                }
 228                else
 229                        usage(describe_usage);
 230        }
 231
 232        save_commit_buffer = 0;
 233
 234        if (argc <= i)
 235                describe("HEAD", 1);
 236        else
 237                while (i < argc) {
 238                        describe(argv[i], (i == argc - 1));
 239                        i++;
 240                }
 241
 242        return 0;
 243}