builtin-describe.con commit git-describe: --long shows the object name even for a tagged commit (518120e)
   1#include "cache.h"
   2#include "commit.h"
   3#include "tag.h"
   4#include "refs.h"
   5#include "builtin.h"
   6#include "exec_cmd.h"
   7#include "parse-options.h"
   8
   9#define SEEN            (1u<<0)
  10#define MAX_TAGS        (FLAG_BITS - 1)
  11
  12static const char * const describe_usage[] = {
  13        "git-describe [options] <committish>*",
  14        NULL
  15};
  16
  17static int debug;       /* Display lots of verbose info */
  18static int all; /* Default to annotated tags only */
  19static int tags;        /* But allow any tags if --tags is specified */
  20static int longformat;
  21static int abbrev = DEFAULT_ABBREV;
  22static int max_candidates = 10;
  23const char *pattern = NULL;
  24
  25struct commit_name {
  26        int prio; /* annotated tag = 2, tag = 1, head = 0 */
  27        char path[FLEX_ARRAY]; /* more */
  28};
  29static const char *prio_names[] = {
  30        "head", "lightweight", "annotated",
  31};
  32
  33static void add_to_known_names(const char *path,
  34                               struct commit *commit,
  35                               int prio)
  36{
  37        struct commit_name *e = commit->util;
  38        if (!e || e->prio < prio) {
  39                size_t len = strlen(path)+1;
  40                free(e);
  41                e = xmalloc(sizeof(struct commit_name) + len);
  42                e->prio = prio;
  43                memcpy(e->path, path, len);
  44                commit->util = e;
  45        }
  46}
  47
  48static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
  49{
  50        struct commit *commit = lookup_commit_reference_gently(sha1, 1);
  51        struct object *object;
  52        int prio;
  53
  54        if (!commit)
  55                return 0;
  56        object = parse_object(sha1);
  57        /* If --all, then any refs are used.
  58         * If --tags, then any tags are used.
  59         * Otherwise only annotated tags are used.
  60         */
  61        if (!prefixcmp(path, "refs/tags/")) {
  62                if (object->type == OBJ_TAG) {
  63                        prio = 2;
  64                        if (pattern && fnmatch(pattern, path + 10, 0))
  65                                prio = 0;
  66                } else
  67                        prio = 1;
  68        }
  69        else
  70                prio = 0;
  71
  72        if (!all) {
  73                if (!prio)
  74                        return 0;
  75                if (!tags && prio < 2)
  76                        return 0;
  77        }
  78        add_to_known_names(all ? path + 5 : path + 10, commit, prio);
  79        return 0;
  80}
  81
  82struct possible_tag {
  83        struct commit_name *name;
  84        int depth;
  85        int found_order;
  86        unsigned flag_within;
  87};
  88
  89static int compare_pt(const void *a_, const void *b_)
  90{
  91        struct possible_tag *a = (struct possible_tag *)a_;
  92        struct possible_tag *b = (struct possible_tag *)b_;
  93        if (a->name->prio != b->name->prio)
  94                return b->name->prio - a->name->prio;
  95        if (a->depth != b->depth)
  96                return a->depth - b->depth;
  97        if (a->found_order != b->found_order)
  98                return a->found_order - b->found_order;
  99        return 0;
 100}
 101
 102static unsigned long finish_depth_computation(
 103        struct commit_list **list,
 104        struct possible_tag *best)
 105{
 106        unsigned long seen_commits = 0;
 107        while (*list) {
 108                struct commit *c = pop_commit(list);
 109                struct commit_list *parents = c->parents;
 110                seen_commits++;
 111                if (c->object.flags & best->flag_within) {
 112                        struct commit_list *a = *list;
 113                        while (a) {
 114                                struct commit *i = a->item;
 115                                if (!(i->object.flags & best->flag_within))
 116                                        break;
 117                                a = a->next;
 118                        }
 119                        if (!a)
 120                                break;
 121                } else
 122                        best->depth++;
 123                while (parents) {
 124                        struct commit *p = parents->item;
 125                        parse_commit(p);
 126                        if (!(p->object.flags & SEEN))
 127                                insert_by_date(p, list);
 128                        p->object.flags |= c->object.flags;
 129                        parents = parents->next;
 130                }
 131        }
 132        return seen_commits;
 133}
 134
 135static void describe(const char *arg, int last_one)
 136{
 137        unsigned char sha1[20];
 138        struct commit *cmit, *gave_up_on = NULL;
 139        struct commit_list *list;
 140        static int initialized = 0;
 141        struct commit_name *n;
 142        struct possible_tag all_matches[MAX_TAGS];
 143        unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
 144        unsigned long seen_commits = 0;
 145
 146        if (get_sha1(arg, sha1))
 147                die("Not a valid object name %s", arg);
 148        cmit = lookup_commit_reference(sha1);
 149        if (!cmit)
 150                die("%s is not a valid '%s' object", arg, commit_type);
 151
 152        if (!initialized) {
 153                initialized = 1;
 154                for_each_ref(get_name, NULL);
 155        }
 156
 157        n = cmit->util;
 158        if (n) {
 159                if (!longformat)
 160                        printf("%s\n", n->path);
 161                else
 162                        printf("%s-0-g%s\n", n->path,
 163                                find_unique_abbrev(cmit->object.sha1, abbrev));
 164                return;
 165        }
 166
 167        if (debug)
 168                fprintf(stderr, "searching to describe %s\n", arg);
 169
 170        list = NULL;
 171        cmit->object.flags = SEEN;
 172        commit_list_insert(cmit, &list);
 173        while (list) {
 174                struct commit *c = pop_commit(&list);
 175                struct commit_list *parents = c->parents;
 176                seen_commits++;
 177                n = c->util;
 178                if (n) {
 179                        if (match_cnt < max_candidates) {
 180                                struct possible_tag *t = &all_matches[match_cnt++];
 181                                t->name = n;
 182                                t->depth = seen_commits - 1;
 183                                t->flag_within = 1u << match_cnt;
 184                                t->found_order = match_cnt;
 185                                c->object.flags |= t->flag_within;
 186                                if (n->prio == 2)
 187                                        annotated_cnt++;
 188                        }
 189                        else {
 190                                gave_up_on = c;
 191                                break;
 192                        }
 193                }
 194                for (cur_match = 0; cur_match < match_cnt; cur_match++) {
 195                        struct possible_tag *t = &all_matches[cur_match];
 196                        if (!(c->object.flags & t->flag_within))
 197                                t->depth++;
 198                }
 199                if (annotated_cnt && !list) {
 200                        if (debug)
 201                                fprintf(stderr, "finished search at %s\n",
 202                                        sha1_to_hex(c->object.sha1));
 203                        break;
 204                }
 205                while (parents) {
 206                        struct commit *p = parents->item;
 207                        parse_commit(p);
 208                        if (!(p->object.flags & SEEN))
 209                                insert_by_date(p, &list);
 210                        p->object.flags |= c->object.flags;
 211                        parents = parents->next;
 212                }
 213        }
 214
 215        if (!match_cnt)
 216                die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
 217
 218        qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
 219
 220        if (gave_up_on) {
 221                insert_by_date(gave_up_on, &list);
 222                seen_commits--;
 223        }
 224        seen_commits += finish_depth_computation(&list, &all_matches[0]);
 225        free_commit_list(list);
 226
 227        if (debug) {
 228                for (cur_match = 0; cur_match < match_cnt; cur_match++) {
 229                        struct possible_tag *t = &all_matches[cur_match];
 230                        fprintf(stderr, " %-11s %8d %s\n",
 231                                prio_names[t->name->prio],
 232                                t->depth, t->name->path);
 233                }
 234                fprintf(stderr, "traversed %lu commits\n", seen_commits);
 235                if (gave_up_on) {
 236                        fprintf(stderr,
 237                                "more than %i tags found; listed %i most recent\n"
 238                                "gave up search at %s\n",
 239                                max_candidates, max_candidates,
 240                                sha1_to_hex(gave_up_on->object.sha1));
 241                }
 242        }
 243        if (abbrev == 0)
 244                printf("%s\n", all_matches[0].name->path );
 245        else
 246                printf("%s-%d-g%s\n", all_matches[0].name->path,
 247                       all_matches[0].depth,
 248                       find_unique_abbrev(cmit->object.sha1, abbrev));
 249
 250        if (!last_one)
 251                clear_commit_marks(cmit, -1);
 252}
 253
 254int cmd_describe(int argc, const char **argv, const char *prefix)
 255{
 256        int contains = 0;
 257        struct option options[] = {
 258                OPT_BOOLEAN(0, "contains",   &contains, "find the tag that comes after the commit"),
 259                OPT_BOOLEAN(0, "debug",      &debug, "debug search strategy on stderr"),
 260                OPT_BOOLEAN(0, "all",        &all, "use any ref in .git/refs"),
 261                OPT_BOOLEAN(0, "tags",       &tags, "use any tag in .git/refs/tags"),
 262                OPT_BOOLEAN(0, "long",       &longformat, "always use long format"),
 263                OPT__ABBREV(&abbrev),
 264                OPT_INTEGER(0, "candidates", &max_candidates,
 265                            "consider <n> most recent tags (default: 10)"),
 266                OPT_STRING(0, "match",       &pattern, "pattern",
 267                           "only consider tags matching <pattern>"),
 268                OPT_END(),
 269        };
 270
 271        argc = parse_options(argc, argv, options, describe_usage, 0);
 272        if (max_candidates < 1)
 273                max_candidates = 1;
 274        else if (max_candidates > MAX_TAGS)
 275                max_candidates = MAX_TAGS;
 276
 277        save_commit_buffer = 0;
 278
 279        if (longformat && abbrev == 0)
 280                die("--long is incompatible with --abbrev=0");
 281
 282        if (contains) {
 283                const char **args = xmalloc((6 + argc) * sizeof(char*));
 284                int i = 0;
 285                args[i++] = "name-rev";
 286                args[i++] = "--name-only";
 287                args[i++] = "--no-undefined";
 288                if (!all) {
 289                        args[i++] = "--tags";
 290                        if (pattern) {
 291                                char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
 292                                sprintf(s, "--refs=refs/tags/%s", pattern);
 293                                args[i++] = s;
 294                        }
 295                }
 296                memcpy(args + i, argv, argc * sizeof(char*));
 297                args[i + argc] = NULL;
 298                return cmd_name_rev(i + argc, args, prefix);
 299        }
 300
 301        if (argc == 0) {
 302                describe("HEAD", 1);
 303        } else {
 304                while (argc-- > 0) {
 305                        describe(*argv++, argc == 0);
 306                }
 307        }
 308        return 0;
 309}