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
155 struct string_list_item
*item
;
160 for_each_string_list_item(item
, &patterns
) {
161 if (!wildmatch(item
->string
, path
+ 10, 0, NULL
)) {
171 /* Is it annotated? */
172 if (!peel_ref(path
, peeled
.hash
)) {
173 is_annotated
= !!oidcmp(oid
, &peeled
);
175 oidcpy(&peeled
, oid
);
180 * By default, we only use annotated tags, but with --tags
181 * we fall back to lightweight ones (even without --tags,
182 * we still remember lightweight ones, only to give hints
183 * in an error message). --all allows any refs to be used.
192 add_to_known_names(all
? path
+ 5 : path
+ 10, peeled
.hash
, prio
, oid
->hash
);
196 struct possible_tag
{
197 struct commit_name
*name
;
200 unsigned flag_within
;
203 static int compare_pt(const void *a_
, const void *b_
)
205 struct possible_tag
*a
= (struct possible_tag
*)a_
;
206 struct possible_tag
*b
= (struct possible_tag
*)b_
;
207 if (a
->depth
!= b
->depth
)
208 return a
->depth
- b
->depth
;
209 if (a
->found_order
!= b
->found_order
)
210 return a
->found_order
- b
->found_order
;
214 static unsigned long finish_depth_computation(
215 struct commit_list
**list
,
216 struct possible_tag
*best
)
218 unsigned long seen_commits
= 0;
220 struct commit
*c
= pop_commit(list
);
221 struct commit_list
*parents
= c
->parents
;
223 if (c
->object
.flags
& best
->flag_within
) {
224 struct commit_list
*a
= *list
;
226 struct commit
*i
= a
->item
;
227 if (!(i
->object
.flags
& best
->flag_within
))
236 struct commit
*p
= parents
->item
;
238 if (!(p
->object
.flags
& SEEN
))
239 commit_list_insert_by_date(p
, list
);
240 p
->object
.flags
|= c
->object
.flags
;
241 parents
= parents
->next
;
247 static void display_name(struct commit_name
*n
)
249 if (n
->prio
== 2 && !n
->tag
) {
250 n
->tag
= lookup_tag(n
->sha1
);
251 if (!n
->tag
|| parse_tag(n
->tag
))
252 die(_("annotated tag %s not available"), n
->path
);
254 if (n
->tag
&& !n
->name_checked
) {
256 die(_("annotated tag %s has no embedded name"), n
->path
);
257 if (strcmp(n
->tag
->tag
, all
? n
->path
+ 5 : n
->path
))
258 warning(_("tag '%s' is really '%s' here"), n
->tag
->tag
, n
->path
);
263 printf("%s", n
->tag
->tag
);
265 printf("%s", n
->path
);
268 static void show_suffix(int depth
, const unsigned char *sha1
)
270 printf("-%d-g%s", depth
, find_unique_abbrev(sha1
, abbrev
));
273 static void describe(const char *arg
, int last_one
)
275 unsigned char sha1
[20];
276 struct commit
*cmit
, *gave_up_on
= NULL
;
277 struct commit_list
*list
;
278 struct commit_name
*n
;
279 struct possible_tag all_matches
[MAX_TAGS
];
280 unsigned int match_cnt
= 0, annotated_cnt
= 0, cur_match
;
281 unsigned long seen_commits
= 0;
282 unsigned int unannotated_cnt
= 0;
284 if (get_sha1(arg
, sha1
))
285 die(_("Not a valid object name %s"), arg
);
286 cmit
= lookup_commit_reference(sha1
);
288 die(_("%s is not a valid '%s' object"), arg
, commit_type
);
290 n
= find_commit_name(cmit
->object
.oid
.hash
);
291 if (n
&& (tags
|| all
|| n
->prio
== 2)) {
293 * Exact match to an existing ref.
297 show_suffix(0, n
->tag
? n
->tag
->tagged
->oid
.hash
: sha1
);
305 die(_("no tag exactly matches '%s'"), oid_to_hex(&cmit
->object
.oid
));
307 fprintf(stderr
, _("searching to describe %s\n"), arg
);
310 struct hashmap_iter iter
;
312 struct commit_name
*n
= hashmap_iter_first(&names
, &iter
);
313 for (; n
; n
= hashmap_iter_next(&iter
)) {
314 c
= lookup_commit_reference_gently(n
->peeled
, 1);
322 cmit
->object
.flags
= SEEN
;
323 commit_list_insert(cmit
, &list
);
325 struct commit
*c
= pop_commit(&list
);
326 struct commit_list
*parents
= c
->parents
;
330 if (!tags
&& !all
&& n
->prio
< 2) {
332 } else if (match_cnt
< max_candidates
) {
333 struct possible_tag
*t
= &all_matches
[match_cnt
++];
335 t
->depth
= seen_commits
- 1;
336 t
->flag_within
= 1u << match_cnt
;
337 t
->found_order
= match_cnt
;
338 c
->object
.flags
|= t
->flag_within
;
347 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
348 struct possible_tag
*t
= &all_matches
[cur_match
];
349 if (!(c
->object
.flags
& t
->flag_within
))
352 if (annotated_cnt
&& !list
) {
354 fprintf(stderr
, _("finished search at %s\n"),
355 oid_to_hex(&c
->object
.oid
));
359 struct commit
*p
= parents
->item
;
361 if (!(p
->object
.flags
& SEEN
))
362 commit_list_insert_by_date(p
, &list
);
363 p
->object
.flags
|= c
->object
.flags
;
364 parents
= parents
->next
;
372 struct object_id
*oid
= &cmit
->object
.oid
;
374 printf("%s", find_unique_abbrev(oid
->hash
, abbrev
));
381 die(_("No annotated tags can describe '%s'.\n"
382 "However, there were unannotated tags: try --tags."),
385 die(_("No tags can describe '%s'.\n"
386 "Try --always, or create some tags."),
390 QSORT(all_matches
, match_cnt
, compare_pt
);
393 commit_list_insert_by_date(gave_up_on
, &list
);
396 seen_commits
+= finish_depth_computation(&list
, &all_matches
[0]);
397 free_commit_list(list
);
400 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
401 struct possible_tag
*t
= &all_matches
[cur_match
];
402 fprintf(stderr
, " %-11s %8d %s\n",
403 prio_names
[t
->name
->prio
],
404 t
->depth
, t
->name
->path
);
406 fprintf(stderr
, _("traversed %lu commits\n"), seen_commits
);
409 _("more than %i tags found; listed %i most recent\n"
410 "gave up search at %s\n"),
411 max_candidates
, max_candidates
,
412 oid_to_hex(&gave_up_on
->object
.oid
));
416 display_name(all_matches
[0].name
);
418 show_suffix(all_matches
[0].depth
, cmit
->object
.oid
.hash
);
424 clear_commit_marks(cmit
, -1);
427 int cmd_describe(int argc
, const char **argv
, const char *prefix
)
430 struct option options
[] = {
431 OPT_BOOL(0, "contains", &contains
, N_("find the tag that comes after the commit")),
432 OPT_BOOL(0, "debug", &debug
, N_("debug search strategy on stderr")),
433 OPT_BOOL(0, "all", &all
, N_("use any ref")),
434 OPT_BOOL(0, "tags", &tags
, N_("use any tag, even unannotated")),
435 OPT_BOOL(0, "long", &longformat
, N_("always use long format")),
436 OPT_BOOL(0, "first-parent", &first_parent
, N_("only follow first parent")),
437 OPT__ABBREV(&abbrev
),
438 OPT_SET_INT(0, "exact-match", &max_candidates
,
439 N_("only output exact matches"), 0),
440 OPT_INTEGER(0, "candidates", &max_candidates
,
441 N_("consider <n> most recent tags (default: 10)")),
442 OPT_STRING_LIST(0, "match", &patterns
, N_("pattern"),
443 N_("only consider tags matching <pattern>")),
444 OPT_STRING_LIST(0, "exclude", &exclude_patterns
, N_("pattern"),
445 N_("do not consider tags matching <pattern>")),
446 OPT_BOOL(0, "always", &always
,
447 N_("show abbreviated commit object as fallback")),
448 {OPTION_STRING
, 0, "dirty", &dirty
, N_("mark"),
449 N_("append <mark> on dirty working tree (default: \"-dirty\")"),
450 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "-dirty"},
454 git_config(git_default_config
, NULL
);
455 argc
= parse_options(argc
, argv
, prefix
, options
, describe_usage
, 0);
457 abbrev
= DEFAULT_ABBREV
;
459 if (max_candidates
< 0)
461 else if (max_candidates
> MAX_TAGS
)
462 max_candidates
= MAX_TAGS
;
464 save_commit_buffer
= 0;
466 if (longformat
&& abbrev
== 0)
467 die(_("--long is incompatible with --abbrev=0"));
470 struct string_list_item
*item
;
471 struct argv_array args
;
473 argv_array_init(&args
);
474 argv_array_pushl(&args
, "name-rev",
475 "--peel-tag", "--name-only", "--no-undefined",
478 argv_array_push(&args
, "--always");
480 argv_array_push(&args
, "--tags");
481 for_each_string_list_item(item
, &patterns
)
482 argv_array_pushf(&args
, "--refs=refs/tags/%s", item
->string
);
483 for_each_string_list_item(item
, &exclude_patterns
)
484 argv_array_pushf(&args
, "--exclude=refs/tags/%s", item
->string
);
487 argv_array_pushv(&args
, argv
);
489 argv_array_push(&args
, "HEAD");
490 return cmd_name_rev(args
.argc
, args
.argv
, prefix
);
493 hashmap_init(&names
, (hashmap_cmp_fn
) commit_name_cmp
, 0);
494 for_each_rawref(get_name
, NULL
);
495 if (!names
.size
&& !always
)
496 die(_("No names found, cannot describe anything."));
500 static struct lock_file index_lock
;
503 read_cache_preload(NULL
);
504 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
,
506 fd
= hold_locked_index(&index_lock
, 0);
508 update_index_if_able(&the_index
, &index_lock
);
510 if (!cmd_diff_index(ARRAY_SIZE(diff_index_args
) - 1,
511 diff_index_args
, prefix
))
516 die(_("--dirty is incompatible with commit-ishes"));
519 describe(*argv
++, argc
== 0);