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