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