builtin-describe.con commit describe: when failing, tell the user about options that work (4d23660)
   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; /* Any valid ref can be used */
  19static int tags;        /* Allow lightweight tags */
  20static int longformat;
  21static int abbrev = DEFAULT_ABBREV;
  22static int max_candidates = 10;
  23static int found_names;
  24static const char *pattern;
  25static int always;
  26
  27struct commit_name {
  28        struct tag *tag;
  29        int prio; /* annotated tag = 2, tag = 1, head = 0 */
  30        unsigned char sha1[20];
  31        char path[FLEX_ARRAY]; /* more */
  32};
  33static const char *prio_names[] = {
  34        "head", "lightweight", "annotated",
  35};
  36
  37static void add_to_known_names(const char *path,
  38                               struct commit *commit,
  39                               int prio,
  40                               const unsigned char *sha1)
  41{
  42        struct commit_name *e = commit->util;
  43        if (!e || e->prio < prio) {
  44                size_t len = strlen(path)+1;
  45                free(e);
  46                e = xmalloc(sizeof(struct commit_name) + len);
  47                e->tag = NULL;
  48                e->prio = prio;
  49                hashcpy(e->sha1, sha1);
  50                memcpy(e->path, path, len);
  51                commit->util = e;
  52        }
  53        found_names = 1;
  54}
  55
  56static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
  57{
  58        int might_be_tag = !prefixcmp(path, "refs/tags/");
  59        struct commit *commit;
  60        struct object *object;
  61        unsigned char peeled[20];
  62        int is_tag, prio;
  63
  64        if (!all && !might_be_tag)
  65                return 0;
  66
  67        if (!peel_ref(path, peeled) && !is_null_sha1(peeled)) {
  68                commit = lookup_commit_reference_gently(peeled, 1);
  69                if (!commit)
  70                        return 0;
  71                is_tag = !!hashcmp(sha1, commit->object.sha1);
  72        } else {
  73                commit = lookup_commit_reference_gently(sha1, 1);
  74                object = parse_object(sha1);
  75                if (!commit || !object)
  76                        return 0;
  77                is_tag = object->type == OBJ_TAG;
  78        }
  79
  80        /* If --all, then any refs are used.
  81         * If --tags, then any tags are used.
  82         * Otherwise only annotated tags are used.
  83         */
  84        if (might_be_tag) {
  85                if (is_tag)
  86                        prio = 2;
  87                else
  88                        prio = 1;
  89
  90                if (pattern && fnmatch(pattern, path + 10, 0))
  91                        prio = 0;
  92        }
  93        else
  94                prio = 0;
  95
  96        if (!all) {
  97                if (!prio)
  98                        return 0;
  99        }
 100        add_to_known_names(all ? path + 5 : path + 10, commit, prio, sha1);
 101        return 0;
 102}
 103
 104struct possible_tag {
 105        struct commit_name *name;
 106        int depth;
 107        int found_order;
 108        unsigned flag_within;
 109};
 110
 111static int compare_pt(const void *a_, const void *b_)
 112{
 113        struct possible_tag *a = (struct possible_tag *)a_;
 114        struct possible_tag *b = (struct possible_tag *)b_;
 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 (!n->tag || parse_tag(n->tag) || !n->tag->tag)
 160                        die("annotated tag %s not available", n->path);
 161                if (strcmp(n->tag->tag, all ? n->path + 5 : 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}
 170
 171static void show_suffix(int depth, const unsigned char *sha1)
 172{
 173        printf("-%d-g%s", depth, find_unique_abbrev(sha1, abbrev));
 174}
 175
 176static void describe(const char *arg, int last_one)
 177{
 178        unsigned char sha1[20];
 179        struct commit *cmit, *gave_up_on = NULL;
 180        struct commit_list *list;
 181        struct commit_name *n;
 182        struct possible_tag all_matches[MAX_TAGS];
 183        unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
 184        unsigned long seen_commits = 0;
 185        unsigned int unannotated_cnt = 0;
 186
 187        if (get_sha1(arg, sha1))
 188                die("Not a valid object name %s", arg);
 189        cmit = lookup_commit_reference(sha1);
 190        if (!cmit)
 191                die("%s is not a valid '%s' object", arg, commit_type);
 192
 193        n = cmit->util;
 194        if (n) {
 195                /*
 196                 * Exact match to an existing ref.
 197                 */
 198                display_name(n);
 199                if (longformat)
 200                        show_suffix(0, n->tag ? n->tag->tagged->sha1 : sha1);
 201                printf("\n");
 202                return;
 203        }
 204
 205        if (!max_candidates)
 206                die("no tag exactly matches '%s'", sha1_to_hex(cmit->object.sha1));
 207        if (debug)
 208                fprintf(stderr, "searching to describe %s\n", arg);
 209
 210        list = NULL;
 211        cmit->object.flags = SEEN;
 212        commit_list_insert(cmit, &list);
 213        while (list) {
 214                struct commit *c = pop_commit(&list);
 215                struct commit_list *parents = c->parents;
 216                seen_commits++;
 217                n = c->util;
 218                if (n) {
 219                        if (!tags && !all && n->prio < 2) {
 220                                unannotated_cnt++;
 221                        } else if (match_cnt < max_candidates) {
 222                                struct possible_tag *t = &all_matches[match_cnt++];
 223                                t->name = n;
 224                                t->depth = seen_commits - 1;
 225                                t->flag_within = 1u << match_cnt;
 226                                t->found_order = match_cnt;
 227                                c->object.flags |= t->flag_within;
 228                                if (n->prio == 2)
 229                                        annotated_cnt++;
 230                        }
 231                        else {
 232                                gave_up_on = c;
 233                                break;
 234                        }
 235                }
 236                for (cur_match = 0; cur_match < match_cnt; cur_match++) {
 237                        struct possible_tag *t = &all_matches[cur_match];
 238                        if (!(c->object.flags & t->flag_within))
 239                                t->depth++;
 240                }
 241                if (annotated_cnt && !list) {
 242                        if (debug)
 243                                fprintf(stderr, "finished search at %s\n",
 244                                        sha1_to_hex(c->object.sha1));
 245                        break;
 246                }
 247                while (parents) {
 248                        struct commit *p = parents->item;
 249                        parse_commit(p);
 250                        if (!(p->object.flags & SEEN))
 251                                insert_by_date(p, &list);
 252                        p->object.flags |= c->object.flags;
 253                        parents = parents->next;
 254                }
 255        }
 256
 257        if (!match_cnt) {
 258                const unsigned char *sha1 = cmit->object.sha1;
 259                if (always) {
 260                        printf("%s\n", find_unique_abbrev(sha1, abbrev));
 261                        return;
 262                }
 263                if (unannotated_cnt)
 264                        die("No annotated tags can describe '%s'.\n"
 265                            "However, there were unannotated tags: try --tags.",
 266                            sha1_to_hex(sha1));
 267                else
 268                        die("No tags can describe '%s'.\n"
 269                            "Try --always, or create some tags.",
 270                            sha1_to_hex(sha1));
 271        }
 272
 273        qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
 274
 275        if (gave_up_on) {
 276                insert_by_date(gave_up_on, &list);
 277                seen_commits--;
 278        }
 279        seen_commits += finish_depth_computation(&list, &all_matches[0]);
 280        free_commit_list(list);
 281
 282        if (debug) {
 283                for (cur_match = 0; cur_match < match_cnt; cur_match++) {
 284                        struct possible_tag *t = &all_matches[cur_match];
 285                        fprintf(stderr, " %-11s %8d %s\n",
 286                                prio_names[t->name->prio],
 287                                t->depth, t->name->path);
 288                }
 289                fprintf(stderr, "traversed %lu commits\n", seen_commits);
 290                if (gave_up_on) {
 291                        fprintf(stderr,
 292                                "more than %i tags found; listed %i most recent\n"
 293                                "gave up search at %s\n",
 294                                max_candidates, max_candidates,
 295                                sha1_to_hex(gave_up_on->object.sha1));
 296                }
 297        }
 298
 299        display_name(all_matches[0].name);
 300        if (abbrev)
 301                show_suffix(all_matches[0].depth, cmit->object.sha1);
 302        printf("\n");
 303
 304        if (!last_one)
 305                clear_commit_marks(cmit, -1);
 306}
 307
 308int cmd_describe(int argc, const char **argv, const char *prefix)
 309{
 310        int contains = 0;
 311        struct option options[] = {
 312                OPT_BOOLEAN(0, "contains",   &contains, "find the tag that comes after the commit"),
 313                OPT_BOOLEAN(0, "debug",      &debug, "debug search strategy on stderr"),
 314                OPT_BOOLEAN(0, "all",        &all, "use any ref in .git/refs"),
 315                OPT_BOOLEAN(0, "tags",       &tags, "use any tag in .git/refs/tags"),
 316                OPT_BOOLEAN(0, "long",       &longformat, "always use long format"),
 317                OPT__ABBREV(&abbrev),
 318                OPT_SET_INT(0, "exact-match", &max_candidates,
 319                            "only output exact matches", 0),
 320                OPT_INTEGER(0, "candidates", &max_candidates,
 321                            "consider <n> most recent tags (default: 10)"),
 322                OPT_STRING(0, "match",       &pattern, "pattern",
 323                           "only consider tags matching <pattern>"),
 324                OPT_BOOLEAN(0, "always",     &always,
 325                           "show abbreviated commit object as fallback"),
 326                OPT_END(),
 327        };
 328
 329        argc = parse_options(argc, argv, prefix, options, describe_usage, 0);
 330        if (max_candidates < 0)
 331                max_candidates = 0;
 332        else if (max_candidates > MAX_TAGS)
 333                max_candidates = MAX_TAGS;
 334
 335        save_commit_buffer = 0;
 336
 337        if (longformat && abbrev == 0)
 338                die("--long is incompatible with --abbrev=0");
 339
 340        if (contains) {
 341                const char **args = xmalloc((7 + argc) * sizeof(char *));
 342                int i = 0;
 343                args[i++] = "name-rev";
 344                args[i++] = "--name-only";
 345                args[i++] = "--no-undefined";
 346                if (always)
 347                        args[i++] = "--always";
 348                if (!all) {
 349                        args[i++] = "--tags";
 350                        if (pattern) {
 351                                char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
 352                                sprintf(s, "--refs=refs/tags/%s", pattern);
 353                                args[i++] = s;
 354                        }
 355                }
 356                memcpy(args + i, argv, argc * sizeof(char *));
 357                args[i + argc] = NULL;
 358                return cmd_name_rev(i + argc, args, prefix);
 359        }
 360
 361        for_each_ref(get_name, NULL);
 362        if (!found_names && !always)
 363                die("No names found, cannot describe anything.");
 364
 365        if (argc == 0) {
 366                describe("HEAD", 1);
 367        } else {
 368                while (argc-- > 0) {
 369                        describe(*argv++, argc == 0);
 370                }
 371        }
 372        return 0;
 373}