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