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