8 #include "parse-options.h"
11 #include "argv-array.h"
13 #define SEEN (1u << 0)
14 #define MAX_TAGS (FLAG_BITS - 1)
16 static const char * const describe_usage
[] = {
17 N_("git describe [<options>] [<commit-ish>...]"),
18 N_("git describe [<options>] --dirty"),
22 static int debug
; /* Display lots of verbose info */
23 static int all
; /* Any valid ref can be used */
24 static int tags
; /* Allow lightweight tags */
25 static int longformat
;
26 static int first_parent
;
27 static int abbrev
= -1; /* unspecified */
28 static int max_candidates
= 10;
29 static struct hashmap names
;
31 static const char *pattern
;
33 static const char *dirty
;
35 /* diff-index command arguments to check if working tree is dirty. */
36 static const char *diff_index_args
[] = {
37 "diff-index", "--quiet", "HEAD", "--", NULL
41 struct hashmap_entry entry
;
42 unsigned char peeled
[20];
44 unsigned prio
:2; /* annotated tag = 2, tag = 1, head = 0 */
45 unsigned name_checked
:1;
46 unsigned char sha1
[20];
50 static const char *prio_names
[] = {
51 "head", "lightweight", "annotated",
54 static int commit_name_cmp(const struct commit_name
*cn1
,
55 const struct commit_name
*cn2
, const void *peeled
)
57 return hashcmp(cn1
->peeled
, peeled
? peeled
: cn2
->peeled
);
60 static inline struct commit_name
*find_commit_name(const unsigned char *peeled
)
62 return hashmap_get_from_hash(&names
, sha1hash(peeled
), peeled
);
65 static int replace_name(struct commit_name
*e
,
67 const unsigned char *sha1
,
70 if (!e
|| e
->prio
< prio
)
73 if (e
->prio
== 2 && prio
== 2) {
74 /* Multiple annotated tags point to the same commit.
75 * Select one to keep based upon their tagger date.
80 t
= lookup_tag(e
->sha1
);
81 if (!t
|| parse_tag(t
))
87 if (!t
|| parse_tag(t
))
91 if (e
->tag
->date
< t
->date
)
98 static void add_to_known_names(const char *path
,
99 const unsigned char *peeled
,
101 const unsigned char *sha1
)
103 struct commit_name
*e
= find_commit_name(peeled
);
104 struct tag
*tag
= NULL
;
105 if (replace_name(e
, prio
, sha1
, &tag
)) {
107 e
= xmalloc(sizeof(struct commit_name
));
108 hashcpy(e
->peeled
, peeled
);
109 hashmap_entry_init(e
, sha1hash(peeled
));
110 hashmap_add(&names
, e
);
116 hashcpy(e
->sha1
, sha1
);
118 e
->path
= xstrdup(path
);
122 static int get_name(const char *path
, const struct object_id
*oid
, int flag
, void *cb_data
)
124 int is_tag
= starts_with(path
, "refs/tags/");
125 struct object_id peeled
;
126 int is_annotated
, prio
;
128 /* Reject anything outside refs/tags/ unless --all */
132 /* Accept only tags that match the pattern, if given */
133 if (pattern
&& (!is_tag
|| wildmatch(pattern
, path
+ 10, 0, NULL
)))
136 /* Is it annotated? */
137 if (!peel_ref(path
, peeled
.hash
)) {
138 is_annotated
= !!oidcmp(oid
, &peeled
);
140 oidcpy(&peeled
, oid
);
145 * By default, we only use annotated tags, but with --tags
146 * we fall back to lightweight ones (even without --tags,
147 * we still remember lightweight ones, only to give hints
148 * in an error message). --all allows any refs to be used.
157 add_to_known_names(all
? path
+ 5 : path
+ 10, peeled
.hash
, prio
, oid
->hash
);
161 struct possible_tag
{
162 struct commit_name
*name
;
165 unsigned flag_within
;
168 static int compare_pt(const void *a_
, const void *b_
)
170 struct possible_tag
*a
= (struct possible_tag
*)a_
;
171 struct possible_tag
*b
= (struct possible_tag
*)b_
;
172 if (a
->depth
!= b
->depth
)
173 return a
->depth
- b
->depth
;
174 if (a
->found_order
!= b
->found_order
)
175 return a
->found_order
- b
->found_order
;
179 static unsigned long finish_depth_computation(
180 struct commit_list
**list
,
181 struct possible_tag
*best
)
183 unsigned long seen_commits
= 0;
185 struct commit
*c
= pop_commit(list
);
186 struct commit_list
*parents
= c
->parents
;
188 if (c
->object
.flags
& best
->flag_within
) {
189 struct commit_list
*a
= *list
;
191 struct commit
*i
= a
->item
;
192 if (!(i
->object
.flags
& best
->flag_within
))
201 struct commit
*p
= parents
->item
;
203 if (!(p
->object
.flags
& SEEN
))
204 commit_list_insert_by_date(p
, list
);
205 p
->object
.flags
|= c
->object
.flags
;
206 parents
= parents
->next
;
212 static void display_name(struct commit_name
*n
)
214 if (n
->prio
== 2 && !n
->tag
) {
215 n
->tag
= lookup_tag(n
->sha1
);
216 if (!n
->tag
|| parse_tag(n
->tag
))
217 die(_("annotated tag %s not available"), n
->path
);
219 if (n
->tag
&& !n
->name_checked
) {
221 die(_("annotated tag %s has no embedded name"), n
->path
);
222 if (strcmp(n
->tag
->tag
, all
? n
->path
+ 5 : n
->path
))
223 warning(_("tag '%s' is really '%s' here"), n
->tag
->tag
, n
->path
);
228 printf("%s", n
->tag
->tag
);
230 printf("%s", n
->path
);
233 static void show_suffix(int depth
, const unsigned char *sha1
)
235 printf("-%d-g%s", depth
, find_unique_abbrev(sha1
, abbrev
));
238 static void describe(const char *arg
, int last_one
)
240 unsigned char sha1
[20];
241 struct commit
*cmit
, *gave_up_on
= NULL
;
242 struct commit_list
*list
;
243 struct commit_name
*n
;
244 struct possible_tag all_matches
[MAX_TAGS
];
245 unsigned int match_cnt
= 0, annotated_cnt
= 0, cur_match
;
246 unsigned long seen_commits
= 0;
247 unsigned int unannotated_cnt
= 0;
249 if (get_sha1(arg
, sha1
))
250 die(_("Not a valid object name %s"), arg
);
251 cmit
= lookup_commit_reference(sha1
);
253 die(_("%s is not a valid '%s' object"), arg
, commit_type
);
255 n
= find_commit_name(cmit
->object
.oid
.hash
);
256 if (n
&& (tags
|| all
|| n
->prio
== 2)) {
258 * Exact match to an existing ref.
262 show_suffix(0, n
->tag
? n
->tag
->tagged
->oid
.hash
: sha1
);
270 die(_("no tag exactly matches '%s'"), oid_to_hex(&cmit
->object
.oid
));
272 fprintf(stderr
, _("searching to describe %s\n"), arg
);
275 struct hashmap_iter iter
;
277 struct commit_name
*n
= hashmap_iter_first(&names
, &iter
);
278 for (; n
; n
= hashmap_iter_next(&iter
)) {
279 c
= lookup_commit_reference_gently(n
->peeled
, 1);
287 cmit
->object
.flags
= SEEN
;
288 commit_list_insert(cmit
, &list
);
290 struct commit
*c
= pop_commit(&list
);
291 struct commit_list
*parents
= c
->parents
;
295 if (!tags
&& !all
&& n
->prio
< 2) {
297 } else if (match_cnt
< max_candidates
) {
298 struct possible_tag
*t
= &all_matches
[match_cnt
++];
300 t
->depth
= seen_commits
- 1;
301 t
->flag_within
= 1u << match_cnt
;
302 t
->found_order
= match_cnt
;
303 c
->object
.flags
|= t
->flag_within
;
312 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
313 struct possible_tag
*t
= &all_matches
[cur_match
];
314 if (!(c
->object
.flags
& t
->flag_within
))
317 if (annotated_cnt
&& !list
) {
319 fprintf(stderr
, _("finished search at %s\n"),
320 oid_to_hex(&c
->object
.oid
));
324 struct commit
*p
= parents
->item
;
326 if (!(p
->object
.flags
& SEEN
))
327 commit_list_insert_by_date(p
, &list
);
328 p
->object
.flags
|= c
->object
.flags
;
329 parents
= parents
->next
;
337 struct object_id
*oid
= &cmit
->object
.oid
;
339 printf("%s", find_unique_abbrev(oid
->hash
, abbrev
));
346 die(_("No annotated tags can describe '%s'.\n"
347 "However, there were unannotated tags: try --tags."),
350 die(_("No tags can describe '%s'.\n"
351 "Try --always, or create some tags."),
355 QSORT(all_matches
, match_cnt
, compare_pt
);
358 commit_list_insert_by_date(gave_up_on
, &list
);
361 seen_commits
+= finish_depth_computation(&list
, &all_matches
[0]);
362 free_commit_list(list
);
365 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
366 struct possible_tag
*t
= &all_matches
[cur_match
];
367 fprintf(stderr
, " %-11s %8d %s\n",
368 prio_names
[t
->name
->prio
],
369 t
->depth
, t
->name
->path
);
371 fprintf(stderr
, _("traversed %lu commits\n"), seen_commits
);
374 _("more than %i tags found; listed %i most recent\n"
375 "gave up search at %s\n"),
376 max_candidates
, max_candidates
,
377 oid_to_hex(&gave_up_on
->object
.oid
));
381 display_name(all_matches
[0].name
);
383 show_suffix(all_matches
[0].depth
, cmit
->object
.oid
.hash
);
389 clear_commit_marks(cmit
, -1);
392 int cmd_describe(int argc
, const char **argv
, const char *prefix
)
395 struct option options
[] = {
396 OPT_BOOL(0, "contains", &contains
, N_("find the tag that comes after the commit")),
397 OPT_BOOL(0, "debug", &debug
, N_("debug search strategy on stderr")),
398 OPT_BOOL(0, "all", &all
, N_("use any ref")),
399 OPT_BOOL(0, "tags", &tags
, N_("use any tag, even unannotated")),
400 OPT_BOOL(0, "long", &longformat
, N_("always use long format")),
401 OPT_BOOL(0, "first-parent", &first_parent
, N_("only follow first parent")),
402 OPT__ABBREV(&abbrev
),
403 OPT_SET_INT(0, "exact-match", &max_candidates
,
404 N_("only output exact matches"), 0),
405 OPT_INTEGER(0, "candidates", &max_candidates
,
406 N_("consider <n> most recent tags (default: 10)")),
407 OPT_STRING(0, "match", &pattern
, N_("pattern"),
408 N_("only consider tags matching <pattern>")),
409 OPT_BOOL(0, "always", &always
,
410 N_("show abbreviated commit object as fallback")),
411 {OPTION_STRING
, 0, "dirty", &dirty
, N_("mark"),
412 N_("append <mark> on dirty working tree (default: \"-dirty\")"),
413 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "-dirty"},
417 git_config(git_default_config
, NULL
);
418 argc
= parse_options(argc
, argv
, prefix
, options
, describe_usage
, 0);
420 abbrev
= DEFAULT_ABBREV
;
422 if (max_candidates
< 0)
424 else if (max_candidates
> MAX_TAGS
)
425 max_candidates
= MAX_TAGS
;
427 save_commit_buffer
= 0;
429 if (longformat
&& abbrev
== 0)
430 die(_("--long is incompatible with --abbrev=0"));
433 struct argv_array args
;
435 argv_array_init(&args
);
436 argv_array_pushl(&args
, "name-rev",
437 "--peel-tag", "--name-only", "--no-undefined",
440 argv_array_push(&args
, "--always");
442 argv_array_push(&args
, "--tags");
444 argv_array_pushf(&args
, "--refs=refs/tags/%s", pattern
);
447 argv_array_pushv(&args
, argv
);
449 argv_array_push(&args
, "HEAD");
450 return cmd_name_rev(args
.argc
, args
.argv
, prefix
);
453 hashmap_init(&names
, (hashmap_cmp_fn
) commit_name_cmp
, 0);
454 for_each_rawref(get_name
, NULL
);
455 if (!names
.size
&& !always
)
456 die(_("No names found, cannot describe anything."));
460 static struct lock_file index_lock
;
463 read_cache_preload(NULL
);
464 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
,
466 fd
= hold_locked_index(&index_lock
, 0);
468 update_index_if_able(&the_index
, &index_lock
);
470 if (!cmd_diff_index(ARRAY_SIZE(diff_index_args
) - 1,
471 diff_index_args
, prefix
))
476 die(_("--dirty is incompatible with commit-ishes"));
479 describe(*argv
++, argc
== 0);