builtin / describe.con commit Merge branch 'rs/clean-menu-item-defn' into maint (caea1a2)
   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#include "diff.h"
   9#include "hashmap.h"
  10#include "argv-array.h"
  11
  12#define SEEN            (1u << 0)
  13#define MAX_TAGS        (FLAG_BITS - 1)
  14
  15static const char * const describe_usage[] = {
  16        N_("git describe [options] <commit-ish>*"),
  17        N_("git describe [options] --dirty"),
  18        NULL
  19};
  20
  21static int debug;       /* Display lots of verbose info */
  22static int all; /* Any valid ref can be used */
  23static int tags;        /* Allow lightweight tags */
  24static int longformat;
  25static int first_parent;
  26static int abbrev = -1; /* unspecified */
  27static int max_candidates = 10;
  28static struct hashmap names;
  29static int have_util;
  30static const char *pattern;
  31static int always;
  32static const char *dirty;
  33
  34/* diff-index command arguments to check if working tree is dirty. */
  35static const char *diff_index_args[] = {
  36        "diff-index", "--quiet", "HEAD", "--", NULL
  37};
  38
  39struct commit_name {
  40        struct hashmap_entry entry;
  41        unsigned char peeled[20];
  42        struct tag *tag;
  43        unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */
  44        unsigned name_checked:1;
  45        unsigned char sha1[20];
  46        char *path;
  47};
  48
  49static const char *prio_names[] = {
  50        "head", "lightweight", "annotated",
  51};
  52
  53static int commit_name_cmp(const struct commit_name *cn1,
  54                const struct commit_name *cn2, const void *peeled)
  55{
  56        return hashcmp(cn1->peeled, peeled ? peeled : cn2->peeled);
  57}
  58
  59static inline struct commit_name *find_commit_name(const unsigned char *peeled)
  60{
  61        return hashmap_get_from_hash(&names, sha1hash(peeled), peeled);
  62}
  63
  64static int replace_name(struct commit_name *e,
  65                               int prio,
  66                               const unsigned char *sha1,
  67                               struct tag **tag)
  68{
  69        if (!e || e->prio < prio)
  70                return 1;
  71
  72        if (e->prio == 2 && prio == 2) {
  73                /* Multiple annotated tags point to the same commit.
  74                 * Select one to keep based upon their tagger date.
  75                 */
  76                struct tag *t;
  77
  78                if (!e->tag) {
  79                        t = lookup_tag(e->sha1);
  80                        if (!t || parse_tag(t))
  81                                return 1;
  82                        e->tag = t;
  83                }
  84
  85                t = lookup_tag(sha1);
  86                if (!t || parse_tag(t))
  87                        return 0;
  88                *tag = t;
  89
  90                if (e->tag->date < t->date)
  91                        return 1;
  92        }
  93
  94        return 0;
  95}
  96
  97static void add_to_known_names(const char *path,
  98                               const unsigned char *peeled,
  99                               int prio,
 100                               const unsigned char *sha1)
 101{
 102        struct commit_name *e = find_commit_name(peeled);
 103        struct tag *tag = NULL;
 104        if (replace_name(e, prio, sha1, &tag)) {
 105                if (!e) {
 106                        e = xmalloc(sizeof(struct commit_name));
 107                        hashcpy(e->peeled, peeled);
 108                        hashmap_entry_init(e, sha1hash(peeled));
 109                        hashmap_add(&names, e);
 110                        e->path = NULL;
 111                }
 112                e->tag = tag;
 113                e->prio = prio;
 114                e->name_checked = 0;
 115                hashcpy(e->sha1, sha1);
 116                free(e->path);
 117                e->path = xstrdup(path);
 118        }
 119}
 120
 121static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
 122{
 123        int is_tag = starts_with(path, "refs/tags/");
 124        unsigned char peeled[20];
 125        int is_annotated, prio;
 126
 127        /* Reject anything outside refs/tags/ unless --all */
 128        if (!all && !is_tag)
 129                return 0;
 130
 131        /* Accept only tags that match the pattern, if given */
 132        if (pattern && (!is_tag || wildmatch(pattern, path + 10, 0, NULL)))
 133                return 0;
 134
 135        /* Is it annotated? */
 136        if (!peel_ref(path, peeled)) {
 137                is_annotated = !!hashcmp(sha1, peeled);
 138        } else {
 139                hashcpy(peeled, sha1);
 140                is_annotated = 0;
 141        }
 142
 143        /*
 144         * By default, we only use annotated tags, but with --tags
 145         * we fall back to lightweight ones (even without --tags,
 146         * we still remember lightweight ones, only to give hints
 147         * in an error message).  --all allows any refs to be used.
 148         */
 149        if (is_annotated)
 150                prio = 2;
 151        else if (is_tag)
 152                prio = 1;
 153        else
 154                prio = 0;
 155
 156        add_to_known_names(all ? path + 5 : path + 10, peeled, prio, sha1);
 157        return 0;
 158}
 159
 160struct possible_tag {
 161        struct commit_name *name;
 162        int depth;
 163        int found_order;
 164        unsigned flag_within;
 165};
 166
 167static int compare_pt(const void *a_, const void *b_)
 168{
 169        struct possible_tag *a = (struct possible_tag *)a_;
 170        struct possible_tag *b = (struct possible_tag *)b_;
 171        if (a->depth != b->depth)
 172                return a->depth - b->depth;
 173        if (a->found_order != b->found_order)
 174                return a->found_order - b->found_order;
 175        return 0;
 176}
 177
 178static unsigned long finish_depth_computation(
 179        struct commit_list **list,
 180        struct possible_tag *best)
 181{
 182        unsigned long seen_commits = 0;
 183        while (*list) {
 184                struct commit *c = pop_commit(list);
 185                struct commit_list *parents = c->parents;
 186                seen_commits++;
 187                if (c->object.flags & best->flag_within) {
 188                        struct commit_list *a = *list;
 189                        while (a) {
 190                                struct commit *i = a->item;
 191                                if (!(i->object.flags & best->flag_within))
 192                                        break;
 193                                a = a->next;
 194                        }
 195                        if (!a)
 196                                break;
 197                } else
 198                        best->depth++;
 199                while (parents) {
 200                        struct commit *p = parents->item;
 201                        parse_commit(p);
 202                        if (!(p->object.flags & SEEN))
 203                                commit_list_insert_by_date(p, list);
 204                        p->object.flags |= c->object.flags;
 205                        parents = parents->next;
 206                }
 207        }
 208        return seen_commits;
 209}
 210
 211static void display_name(struct commit_name *n)
 212{
 213        if (n->prio == 2 && !n->tag) {
 214                n->tag = lookup_tag(n->sha1);
 215                if (!n->tag || parse_tag(n->tag))
 216                        die(_("annotated tag %s not available"), n->path);
 217        }
 218        if (n->tag && !n->name_checked) {
 219                if (!n->tag->tag)
 220                        die(_("annotated tag %s has no embedded name"), n->path);
 221                if (strcmp(n->tag->tag, all ? n->path + 5 : n->path))
 222                        warning(_("tag '%s' is really '%s' here"), n->tag->tag, n->path);
 223                n->name_checked = 1;
 224        }
 225
 226        if (n->tag)
 227                printf("%s", n->tag->tag);
 228        else
 229                printf("%s", n->path);
 230}
 231
 232static void show_suffix(int depth, const unsigned char *sha1)
 233{
 234        printf("-%d-g%s", depth, find_unique_abbrev(sha1, abbrev));
 235}
 236
 237static void describe(const char *arg, int last_one)
 238{
 239        unsigned char sha1[20];
 240        struct commit *cmit, *gave_up_on = NULL;
 241        struct commit_list *list;
 242        struct commit_name *n;
 243        struct possible_tag all_matches[MAX_TAGS];
 244        unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
 245        unsigned long seen_commits = 0;
 246        unsigned int unannotated_cnt = 0;
 247
 248        if (get_sha1(arg, sha1))
 249                die(_("Not a valid object name %s"), arg);
 250        cmit = lookup_commit_reference(sha1);
 251        if (!cmit)
 252                die(_("%s is not a valid '%s' object"), arg, commit_type);
 253
 254        n = find_commit_name(cmit->object.sha1);
 255        if (n && (tags || all || n->prio == 2)) {
 256                /*
 257                 * Exact match to an existing ref.
 258                 */
 259                display_name(n);
 260                if (longformat)
 261                        show_suffix(0, n->tag ? n->tag->tagged->sha1 : sha1);
 262                if (dirty)
 263                        printf("%s", dirty);
 264                printf("\n");
 265                return;
 266        }
 267
 268        if (!max_candidates)
 269                die(_("no tag exactly matches '%s'"), sha1_to_hex(cmit->object.sha1));
 270        if (debug)
 271                fprintf(stderr, _("searching to describe %s\n"), arg);
 272
 273        if (!have_util) {
 274                struct hashmap_iter iter;
 275                struct commit *c;
 276                struct commit_name *n = hashmap_iter_first(&names, &iter);
 277                for (; n; n = hashmap_iter_next(&iter)) {
 278                        c = lookup_commit_reference_gently(n->peeled, 1);
 279                        if (c)
 280                                c->util = n;
 281                }
 282                have_util = 1;
 283        }
 284
 285        list = NULL;
 286        cmit->object.flags = SEEN;
 287        commit_list_insert(cmit, &list);
 288        while (list) {
 289                struct commit *c = pop_commit(&list);
 290                struct commit_list *parents = c->parents;
 291                seen_commits++;
 292                n = c->util;
 293                if (n) {
 294                        if (!tags && !all && n->prio < 2) {
 295                                unannotated_cnt++;
 296                        } else if (match_cnt < max_candidates) {
 297                                struct possible_tag *t = &all_matches[match_cnt++];
 298                                t->name = n;
 299                                t->depth = seen_commits - 1;
 300                                t->flag_within = 1u << match_cnt;
 301                                t->found_order = match_cnt;
 302                                c->object.flags |= t->flag_within;
 303                                if (n->prio == 2)
 304                                        annotated_cnt++;
 305                        }
 306                        else {
 307                                gave_up_on = c;
 308                                break;
 309                        }
 310                }
 311                for (cur_match = 0; cur_match < match_cnt; cur_match++) {
 312                        struct possible_tag *t = &all_matches[cur_match];
 313                        if (!(c->object.flags & t->flag_within))
 314                                t->depth++;
 315                }
 316                if (annotated_cnt && !list) {
 317                        if (debug)
 318                                fprintf(stderr, _("finished search at %s\n"),
 319                                        sha1_to_hex(c->object.sha1));
 320                        break;
 321                }
 322                while (parents) {
 323                        struct commit *p = parents->item;
 324                        parse_commit(p);
 325                        if (!(p->object.flags & SEEN))
 326                                commit_list_insert_by_date(p, &list);
 327                        p->object.flags |= c->object.flags;
 328                        parents = parents->next;
 329
 330                        if (first_parent)
 331                                break;
 332                }
 333        }
 334
 335        if (!match_cnt) {
 336                const unsigned char *sha1 = cmit->object.sha1;
 337                if (always) {
 338                        printf("%s", find_unique_abbrev(sha1, abbrev));
 339                        if (dirty)
 340                                printf("%s", dirty);
 341                        printf("\n");
 342                        return;
 343                }
 344                if (unannotated_cnt)
 345                        die(_("No annotated tags can describe '%s'.\n"
 346                            "However, there were unannotated tags: try --tags."),
 347                            sha1_to_hex(sha1));
 348                else
 349                        die(_("No tags can describe '%s'.\n"
 350                            "Try --always, or create some tags."),
 351                            sha1_to_hex(sha1));
 352        }
 353
 354        qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
 355
 356        if (gave_up_on) {
 357                commit_list_insert_by_date(gave_up_on, &list);
 358                seen_commits--;
 359        }
 360        seen_commits += finish_depth_computation(&list, &all_matches[0]);
 361        free_commit_list(list);
 362
 363        if (debug) {
 364                for (cur_match = 0; cur_match < match_cnt; cur_match++) {
 365                        struct possible_tag *t = &all_matches[cur_match];
 366                        fprintf(stderr, " %-11s %8d %s\n",
 367                                prio_names[t->name->prio],
 368                                t->depth, t->name->path);
 369                }
 370                fprintf(stderr, _("traversed %lu commits\n"), seen_commits);
 371                if (gave_up_on) {
 372                        fprintf(stderr,
 373                                _("more than %i tags found; listed %i most recent\n"
 374                                "gave up search at %s\n"),
 375                                max_candidates, max_candidates,
 376                                sha1_to_hex(gave_up_on->object.sha1));
 377                }
 378        }
 379
 380        display_name(all_matches[0].name);
 381        if (abbrev)
 382                show_suffix(all_matches[0].depth, cmit->object.sha1);
 383        if (dirty)
 384                printf("%s", dirty);
 385        printf("\n");
 386
 387        if (!last_one)
 388                clear_commit_marks(cmit, -1);
 389}
 390
 391int cmd_describe(int argc, const char **argv, const char *prefix)
 392{
 393        int contains = 0;
 394        struct option options[] = {
 395                OPT_BOOL(0, "contains",   &contains, N_("find the tag that comes after the commit")),
 396                OPT_BOOL(0, "debug",      &debug, N_("debug search strategy on stderr")),
 397                OPT_BOOL(0, "all",        &all, N_("use any ref")),
 398                OPT_BOOL(0, "tags",       &tags, N_("use any tag, even unannotated")),
 399                OPT_BOOL(0, "long",       &longformat, N_("always use long format")),
 400                OPT_BOOL(0, "first-parent", &first_parent, N_("only follow first parent")),
 401                OPT__ABBREV(&abbrev),
 402                OPT_SET_INT(0, "exact-match", &max_candidates,
 403                            N_("only output exact matches"), 0),
 404                OPT_INTEGER(0, "candidates", &max_candidates,
 405                            N_("consider <n> most recent tags (default: 10)")),
 406                OPT_STRING(0, "match",       &pattern, N_("pattern"),
 407                           N_("only consider tags matching <pattern>")),
 408                OPT_BOOL(0, "always",        &always,
 409                        N_("show abbreviated commit object as fallback")),
 410                {OPTION_STRING, 0, "dirty",  &dirty, N_("mark"),
 411                        N_("append <mark> on dirty working tree (default: \"-dirty\")"),
 412                        PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"},
 413                OPT_END(),
 414        };
 415
 416        git_config(git_default_config, NULL);
 417        argc = parse_options(argc, argv, prefix, options, describe_usage, 0);
 418        if (abbrev < 0)
 419                abbrev = DEFAULT_ABBREV;
 420
 421        if (max_candidates < 0)
 422                max_candidates = 0;
 423        else if (max_candidates > MAX_TAGS)
 424                max_candidates = MAX_TAGS;
 425
 426        save_commit_buffer = 0;
 427
 428        if (longformat && abbrev == 0)
 429                die(_("--long is incompatible with --abbrev=0"));
 430
 431        if (contains) {
 432                struct argv_array args;
 433
 434                argv_array_init(&args);
 435                argv_array_pushl(&args, "name-rev",
 436                                 "--peel-tag", "--name-only", "--no-undefined",
 437                                 NULL);
 438                if (always)
 439                        argv_array_push(&args, "--always");
 440                if (!all) {
 441                        argv_array_push(&args, "--tags");
 442                        if (pattern)
 443                                argv_array_pushf(&args, "--refs=refs/tags/%s", pattern);
 444                }
 445                while (*argv) {
 446                        argv_array_push(&args, *argv);
 447                        argv++;
 448                }
 449                return cmd_name_rev(args.argc, args.argv, prefix);
 450        }
 451
 452        hashmap_init(&names, (hashmap_cmp_fn) commit_name_cmp, 0);
 453        for_each_rawref(get_name, NULL);
 454        if (!names.size && !always)
 455                die(_("No names found, cannot describe anything."));
 456
 457        if (argc == 0) {
 458                if (dirty) {
 459                        static struct lock_file index_lock;
 460                        int fd;
 461
 462                        read_cache_preload(NULL);
 463                        refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED,
 464                                      NULL, NULL, NULL);
 465                        fd = hold_locked_index(&index_lock, 0);
 466                        if (0 <= fd)
 467                                update_index_if_able(&the_index, &index_lock);
 468
 469                        if (!cmd_diff_index(ARRAY_SIZE(diff_index_args) - 1,
 470                                            diff_index_args, prefix))
 471                                dirty = NULL;
 472                }
 473                describe("HEAD", 1);
 474        } else if (dirty) {
 475                die(_("--dirty is incompatible with commit-ishes"));
 476        } else {
 477                while (argc-- > 0)
 478                        describe(*argv++, argc == 0);
 479        }
 480        return 0;
 481}