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 struct string_list patterns
= STRING_LIST_INIT_NODUP
;
32 static struct string_list exclude_patterns
= STRING_LIST_INIT_NODUP
;
34 static const char *dirty
;
36 /* diff-index command arguments to check if working tree is dirty. */
37 static const char *diff_index_args
[] = {
38 "diff-index", "--quiet", "HEAD", "--", NULL
42 struct hashmap_entry entry
;
43 unsigned char peeled
[20];
45 unsigned prio
:2; /* annotated tag = 2, tag = 1, head = 0 */
46 unsigned name_checked
:1;
47 unsigned char sha1
[20];
51 static const char *prio_names
[] = {
52 "head", "lightweight", "annotated",
55 static int commit_name_cmp(const struct commit_name
*cn1
,
56 const struct commit_name
*cn2
, const void *peeled
)
58 return hashcmp(cn1
->peeled
, peeled
? peeled
: cn2
->peeled
);
61 static inline struct commit_name
*find_commit_name(const unsigned char *peeled
)
63 return hashmap_get_from_hash(&names
, sha1hash(peeled
), peeled
);
66 static int replace_name(struct commit_name
*e
,
68 const unsigned char *sha1
,
71 if (!e
|| e
->prio
< prio
)
74 if (e
->prio
== 2 && prio
== 2) {
75 /* Multiple annotated tags point to the same commit.
76 * Select one to keep based upon their tagger date.
81 t
= lookup_tag(e
->sha1
);
82 if (!t
|| parse_tag(t
))
88 if (!t
|| parse_tag(t
))
92 if (e
->tag
->date
< t
->date
)
99 static void add_to_known_names(const char *path
,
100 const unsigned char *peeled
,
102 const unsigned char *sha1
)
104 struct commit_name
*e
= find_commit_name(peeled
);
105 struct tag
*tag
= NULL
;
106 if (replace_name(e
, prio
, sha1
, &tag
)) {
108 e
= xmalloc(sizeof(struct commit_name
));
109 hashcpy(e
->peeled
, peeled
);
110 hashmap_entry_init(e
, sha1hash(peeled
));
111 hashmap_add(&names
, e
);
117 hashcpy(e
->sha1
, sha1
);
119 e
->path
= xstrdup(path
);
123 static int get_name(const char *path
, const struct object_id
*oid
, int flag
, void *cb_data
)
125 int is_tag
= starts_with(path
, "refs/tags/");
126 struct object_id peeled
;
127 int is_annotated
, prio
;
129 /* Reject anything outside refs/tags/ unless --all */
134 * If we're given exclude patterns, first exclude any tag which match
135 * any of the exclude pattern.
137 if (exclude_patterns
.nr
) {
138 struct string_list_item
*item
;
143 for_each_string_list_item(item
, &exclude_patterns
) {
144 if (!wildmatch(item
->string
, path
+ 10, 0, NULL
))
150 * If we're given patterns, accept only tags which match at least one
154 struct string_list_item
*item
;
159 for_each_string_list_item(item
, &patterns
) {
160 if (!wildmatch(item
->string
, path
+ 10, 0, NULL
))
163 /* If we get here, no pattern matched. */
168 /* Is it annotated? */
169 if (!peel_ref(path
, peeled
.hash
)) {
170 is_annotated
= !!oidcmp(oid
, &peeled
);
172 oidcpy(&peeled
, oid
);
177 * By default, we only use annotated tags, but with --tags
178 * we fall back to lightweight ones (even without --tags,
179 * we still remember lightweight ones, only to give hints
180 * in an error message). --all allows any refs to be used.
189 add_to_known_names(all
? path
+ 5 : path
+ 10, peeled
.hash
, prio
, oid
->hash
);
193 struct possible_tag
{
194 struct commit_name
*name
;
197 unsigned flag_within
;
200 static int compare_pt(const void *a_
, const void *b_
)
202 struct possible_tag
*a
= (struct possible_tag
*)a_
;
203 struct possible_tag
*b
= (struct possible_tag
*)b_
;
204 if (a
->depth
!= b
->depth
)
205 return a
->depth
- b
->depth
;
206 if (a
->found_order
!= b
->found_order
)
207 return a
->found_order
- b
->found_order
;
211 static unsigned long finish_depth_computation(
212 struct commit_list
**list
,
213 struct possible_tag
*best
)
215 unsigned long seen_commits
= 0;
217 struct commit
*c
= pop_commit(list
);
218 struct commit_list
*parents
= c
->parents
;
220 if (c
->object
.flags
& best
->flag_within
) {
221 struct commit_list
*a
= *list
;
223 struct commit
*i
= a
->item
;
224 if (!(i
->object
.flags
& best
->flag_within
))
233 struct commit
*p
= parents
->item
;
235 if (!(p
->object
.flags
& SEEN
))
236 commit_list_insert_by_date(p
, list
);
237 p
->object
.flags
|= c
->object
.flags
;
238 parents
= parents
->next
;
244 static void display_name(struct commit_name
*n
)
246 if (n
->prio
== 2 && !n
->tag
) {
247 n
->tag
= lookup_tag(n
->sha1
);
248 if (!n
->tag
|| parse_tag(n
->tag
))
249 die(_("annotated tag %s not available"), n
->path
);
251 if (n
->tag
&& !n
->name_checked
) {
253 die(_("annotated tag %s has no embedded name"), n
->path
);
254 if (strcmp(n
->tag
->tag
, all
? n
->path
+ 5 : n
->path
))
255 warning(_("tag '%s' is really '%s' here"), n
->tag
->tag
, n
->path
);
260 printf("%s", n
->tag
->tag
);
262 printf("%s", n
->path
);
265 static void show_suffix(int depth
, const unsigned char *sha1
)
267 printf("-%d-g%s", depth
, find_unique_abbrev(sha1
, abbrev
));
270 static void describe(const char *arg
, int last_one
)
272 unsigned char sha1
[20];
273 struct commit
*cmit
, *gave_up_on
= NULL
;
274 struct commit_list
*list
;
275 struct commit_name
*n
;
276 struct possible_tag all_matches
[MAX_TAGS
];
277 unsigned int match_cnt
= 0, annotated_cnt
= 0, cur_match
;
278 unsigned long seen_commits
= 0;
279 unsigned int unannotated_cnt
= 0;
281 if (get_sha1(arg
, sha1
))
282 die(_("Not a valid object name %s"), arg
);
283 cmit
= lookup_commit_reference(sha1
);
285 die(_("%s is not a valid '%s' object"), arg
, commit_type
);
287 n
= find_commit_name(cmit
->object
.oid
.hash
);
288 if (n
&& (tags
|| all
|| n
->prio
== 2)) {
290 * Exact match to an existing ref.
294 show_suffix(0, n
->tag
? n
->tag
->tagged
->oid
.hash
: sha1
);
302 die(_("no tag exactly matches '%s'"), oid_to_hex(&cmit
->object
.oid
));
304 fprintf(stderr
, _("searching to describe %s\n"), arg
);
307 struct hashmap_iter iter
;
309 struct commit_name
*n
= hashmap_iter_first(&names
, &iter
);
310 for (; n
; n
= hashmap_iter_next(&iter
)) {
311 c
= lookup_commit_reference_gently(n
->peeled
, 1);
319 cmit
->object
.flags
= SEEN
;
320 commit_list_insert(cmit
, &list
);
322 struct commit
*c
= pop_commit(&list
);
323 struct commit_list
*parents
= c
->parents
;
327 if (!tags
&& !all
&& n
->prio
< 2) {
329 } else if (match_cnt
< max_candidates
) {
330 struct possible_tag
*t
= &all_matches
[match_cnt
++];
332 t
->depth
= seen_commits
- 1;
333 t
->flag_within
= 1u << match_cnt
;
334 t
->found_order
= match_cnt
;
335 c
->object
.flags
|= t
->flag_within
;
344 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
345 struct possible_tag
*t
= &all_matches
[cur_match
];
346 if (!(c
->object
.flags
& t
->flag_within
))
349 if (annotated_cnt
&& !list
) {
351 fprintf(stderr
, _("finished search at %s\n"),
352 oid_to_hex(&c
->object
.oid
));
356 struct commit
*p
= parents
->item
;
358 if (!(p
->object
.flags
& SEEN
))
359 commit_list_insert_by_date(p
, &list
);
360 p
->object
.flags
|= c
->object
.flags
;
361 parents
= parents
->next
;
369 struct object_id
*oid
= &cmit
->object
.oid
;
371 printf("%s", find_unique_abbrev(oid
->hash
, abbrev
));
378 die(_("No annotated tags can describe '%s'.\n"
379 "However, there were unannotated tags: try --tags."),
382 die(_("No tags can describe '%s'.\n"
383 "Try --always, or create some tags."),
387 QSORT(all_matches
, match_cnt
, compare_pt
);
390 commit_list_insert_by_date(gave_up_on
, &list
);
393 seen_commits
+= finish_depth_computation(&list
, &all_matches
[0]);
394 free_commit_list(list
);
397 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
398 struct possible_tag
*t
= &all_matches
[cur_match
];
399 fprintf(stderr
, " %-11s %8d %s\n",
400 prio_names
[t
->name
->prio
],
401 t
->depth
, t
->name
->path
);
403 fprintf(stderr
, _("traversed %lu commits\n"), seen_commits
);
406 _("more than %i tags found; listed %i most recent\n"
407 "gave up search at %s\n"),
408 max_candidates
, max_candidates
,
409 oid_to_hex(&gave_up_on
->object
.oid
));
413 display_name(all_matches
[0].name
);
415 show_suffix(all_matches
[0].depth
, cmit
->object
.oid
.hash
);
421 clear_commit_marks(cmit
, -1);
424 int cmd_describe(int argc
, const char **argv
, const char *prefix
)
427 struct option options
[] = {
428 OPT_BOOL(0, "contains", &contains
, N_("find the tag that comes after the commit")),
429 OPT_BOOL(0, "debug", &debug
, N_("debug search strategy on stderr")),
430 OPT_BOOL(0, "all", &all
, N_("use any ref")),
431 OPT_BOOL(0, "tags", &tags
, N_("use any tag, even unannotated")),
432 OPT_BOOL(0, "long", &longformat
, N_("always use long format")),
433 OPT_BOOL(0, "first-parent", &first_parent
, N_("only follow first parent")),
434 OPT__ABBREV(&abbrev
),
435 OPT_SET_INT(0, "exact-match", &max_candidates
,
436 N_("only output exact matches"), 0),
437 OPT_INTEGER(0, "candidates", &max_candidates
,
438 N_("consider <n> most recent tags (default: 10)")),
439 OPT_STRING_LIST(0, "match", &patterns
, N_("pattern"),
440 N_("only consider tags matching <pattern>")),
441 OPT_STRING_LIST(0, "exclude", &exclude_patterns
, N_("pattern"),
442 N_("do not consider tags matching <pattern>")),
443 OPT_BOOL(0, "always", &always
,
444 N_("show abbreviated commit object as fallback")),
445 {OPTION_STRING
, 0, "dirty", &dirty
, N_("mark"),
446 N_("append <mark> on dirty working tree (default: \"-dirty\")"),
447 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "-dirty"},
451 git_config(git_default_config
, NULL
);
452 argc
= parse_options(argc
, argv
, prefix
, options
, describe_usage
, 0);
454 abbrev
= DEFAULT_ABBREV
;
456 if (max_candidates
< 0)
458 else if (max_candidates
> MAX_TAGS
)
459 max_candidates
= MAX_TAGS
;
461 save_commit_buffer
= 0;
463 if (longformat
&& abbrev
== 0)
464 die(_("--long is incompatible with --abbrev=0"));
467 struct string_list_item
*item
;
468 struct argv_array args
;
470 argv_array_init(&args
);
471 argv_array_pushl(&args
, "name-rev",
472 "--peel-tag", "--name-only", "--no-undefined",
475 argv_array_push(&args
, "--always");
477 argv_array_push(&args
, "--tags");
478 for_each_string_list_item(item
, &patterns
)
479 argv_array_pushf(&args
, "--refs=refs/tags/%s", item
->string
);
480 for_each_string_list_item(item
, &exclude_patterns
)
481 argv_array_pushf(&args
, "--exclude=refs/tags/%s", item
->string
);
484 argv_array_pushv(&args
, argv
);
486 argv_array_push(&args
, "HEAD");
487 return cmd_name_rev(args
.argc
, args
.argv
, prefix
);
490 hashmap_init(&names
, (hashmap_cmp_fn
) commit_name_cmp
, 0);
491 for_each_rawref(get_name
, NULL
);
492 if (!names
.size
&& !always
)
493 die(_("No names found, cannot describe anything."));
497 static struct lock_file index_lock
;
500 read_cache_preload(NULL
);
501 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
,
503 fd
= hold_locked_index(&index_lock
, 0);
505 update_index_if_able(&the_index
, &index_lock
);
507 if (!cmd_diff_index(ARRAY_SIZE(diff_index_args
) - 1,
508 diff_index_args
, prefix
))
513 die(_("--dirty is incompatible with commit-ishes"));
516 describe(*argv
++, argc
== 0);