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"89#define SEEN (1u<<0)10#define MAX_TAGS (FLAG_BITS - 1)1112static const char * const describe_usage[] = {13"git-describe [options] <committish>*",14NULL15};1617static 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 abbrev = DEFAULT_ABBREV;21static int max_candidates = 10;22const char *pattern = NULL;2324struct commit_name {25int prio; /* annotated tag = 2, tag = 1, head = 0 */26char path[FLEX_ARRAY]; /* more */27};28static const char *prio_names[] = {29"head", "lightweight", "annotated",30};3132static void add_to_known_names(const char *path,33struct commit *commit,34int prio)35{36struct commit_name *e = commit->util;37if (!e || e->prio < prio) {38size_t len = strlen(path)+1;39free(e);40e = xmalloc(sizeof(struct commit_name) + len);41e->prio = prio;42memcpy(e->path, path, len);43commit->util = e;44}45}4647static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)48{49struct commit *commit;50struct object *object;51unsigned char peeled[20];52int is_tag, prio;5354if (!peel_ref(path, peeled) && !is_null_sha1(peeled)) {55commit = lookup_commit_reference_gently(peeled, 1);56if (!commit)57return 0;58is_tag = !!hashcmp(sha1, commit->object.sha1);59} else {60commit = lookup_commit_reference_gently(sha1, 1);61object = parse_object(sha1);62if (!commit || !object)63return 0;64is_tag = object->type == OBJ_TAG;65}6667/* If --all, then any refs are used.68* If --tags, then any tags are used.69* Otherwise only annotated tags are used.70*/71if (!prefixcmp(path, "refs/tags/")) {72if (is_tag) {73prio = 2;74if (pattern && fnmatch(pattern, path + 10, 0))75prio = 0;76} else77prio = 1;78}79else80prio = 0;8182if (!all) {83if (!prio)84return 0;85if (!tags && prio < 2)86return 0;87}88add_to_known_names(all ? path + 5 : path + 10, commit, prio);89return 0;90}9192struct possible_tag {93struct commit_name *name;94int depth;95int found_order;96unsigned flag_within;97};9899static int compare_pt(const void *a_, const void *b_)100{101struct possible_tag *a = (struct possible_tag *)a_;102struct possible_tag *b = (struct possible_tag *)b_;103if (a->name->prio != b->name->prio)104return b->name->prio - a->name->prio;105if (a->depth != b->depth)106return a->depth - b->depth;107if (a->found_order != b->found_order)108return a->found_order - b->found_order;109return 0;110}111112static unsigned long finish_depth_computation(113struct commit_list **list,114struct possible_tag *best)115{116unsigned long seen_commits = 0;117while (*list) {118struct commit *c = pop_commit(list);119struct commit_list *parents = c->parents;120seen_commits++;121if (c->object.flags & best->flag_within) {122struct commit_list *a = *list;123while (a) {124struct commit *i = a->item;125if (!(i->object.flags & best->flag_within))126break;127a = a->next;128}129if (!a)130break;131} else132best->depth++;133while (parents) {134struct commit *p = parents->item;135parse_commit(p);136if (!(p->object.flags & SEEN))137insert_by_date(p, list);138p->object.flags |= c->object.flags;139parents = parents->next;140}141}142return seen_commits;143}144145static void describe(const char *arg, int last_one)146{147unsigned char sha1[20];148struct commit *cmit, *gave_up_on = NULL;149struct commit_list *list;150static int initialized = 0;151struct commit_name *n;152struct possible_tag all_matches[MAX_TAGS];153unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;154unsigned long seen_commits = 0;155156if (get_sha1(arg, sha1))157die("Not a valid object name %s", arg);158cmit = lookup_commit_reference(sha1);159if (!cmit)160die("%s is not a valid '%s' object", arg, commit_type);161162if (!initialized) {163initialized = 1;164for_each_ref(get_name, NULL);165}166167n = cmit->util;168if (n) {169printf("%s\n", n->path);170return;171}172173if (debug)174fprintf(stderr, "searching to describe %s\n", arg);175176list = NULL;177cmit->object.flags = SEEN;178commit_list_insert(cmit, &list);179while (list) {180struct commit *c = pop_commit(&list);181struct commit_list *parents = c->parents;182seen_commits++;183n = c->util;184if (n) {185if (match_cnt < max_candidates) {186struct possible_tag *t = &all_matches[match_cnt++];187t->name = n;188t->depth = seen_commits - 1;189t->flag_within = 1u << match_cnt;190t->found_order = match_cnt;191c->object.flags |= t->flag_within;192if (n->prio == 2)193annotated_cnt++;194}195else {196gave_up_on = c;197break;198}199}200for (cur_match = 0; cur_match < match_cnt; cur_match++) {201struct possible_tag *t = &all_matches[cur_match];202if (!(c->object.flags & t->flag_within))203t->depth++;204}205if (annotated_cnt && !list) {206if (debug)207fprintf(stderr, "finished search at %s\n",208sha1_to_hex(c->object.sha1));209break;210}211while (parents) {212struct commit *p = parents->item;213parse_commit(p);214if (!(p->object.flags & SEEN))215insert_by_date(p, &list);216p->object.flags |= c->object.flags;217parents = parents->next;218}219}220221if (!match_cnt)222die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));223224qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);225226if (gave_up_on) {227insert_by_date(gave_up_on, &list);228seen_commits--;229}230seen_commits += finish_depth_computation(&list, &all_matches[0]);231free_commit_list(list);232233if (debug) {234for (cur_match = 0; cur_match < match_cnt; cur_match++) {235struct possible_tag *t = &all_matches[cur_match];236fprintf(stderr, " %-11s %8d %s\n",237prio_names[t->name->prio],238t->depth, t->name->path);239}240fprintf(stderr, "traversed %lu commits\n", seen_commits);241if (gave_up_on) {242fprintf(stderr,243"more than %i tags found; listed %i most recent\n"244"gave up search at %s\n",245max_candidates, max_candidates,246sha1_to_hex(gave_up_on->object.sha1));247}248}249if (abbrev == 0)250printf("%s\n", all_matches[0].name->path );251else252printf("%s-%d-g%s\n", all_matches[0].name->path,253all_matches[0].depth,254find_unique_abbrev(cmit->object.sha1, abbrev));255256if (!last_one)257clear_commit_marks(cmit, -1);258}259260int cmd_describe(int argc, const char **argv, const char *prefix)261{262int contains = 0;263struct option options[] = {264OPT_BOOLEAN(0, "contains", &contains, "find the tag that comes after the commit"),265OPT_BOOLEAN(0, "debug", &debug, "debug search strategy on stderr"),266OPT_BOOLEAN(0, "all", &all, "use any ref in .git/refs"),267OPT_BOOLEAN(0, "tags", &tags, "use any tag in .git/refs/tags"),268OPT__ABBREV(&abbrev),269OPT_INTEGER(0, "candidates", &max_candidates,270"consider <n> most recent tags (default: 10)"),271OPT_STRING(0, "match", &pattern, "pattern",272"only consider tags matching <pattern>"),273OPT_END(),274};275276argc = parse_options(argc, argv, options, describe_usage, 0);277if (max_candidates < 1)278max_candidates = 1;279else if (max_candidates > MAX_TAGS)280max_candidates = MAX_TAGS;281282save_commit_buffer = 0;283284if (contains) {285const char **args = xmalloc((6 + argc) * sizeof(char*));286int i = 0;287args[i++] = "name-rev";288args[i++] = "--name-only";289args[i++] = "--no-undefined";290if (!all) {291args[i++] = "--tags";292if (pattern) {293char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);294sprintf(s, "--refs=refs/tags/%s", pattern);295args[i++] = s;296}297}298memcpy(args + i, argv, argc * sizeof(char*));299args[i + argc] = NULL;300return cmd_name_rev(i + argc, args, prefix);301}302303if (argc == 0) {304describe("HEAD", 1);305} else {306while (argc-- > 0) {307describe(*argv++, argc == 0);308}309}310return 0;311}