7 #include "parse-options.h"
10 #include "argv-array.h"
12 #define SEEN (1u << 0)
13 #define MAX_TAGS (FLAG_BITS - 1)
15 static const char * const describe_usage
[] = {
16 N_("git describe [options] <commit-ish>*"),
17 N_("git describe [options] --dirty"),
21 static int debug
; /* Display lots of verbose info */
22 static int all
; /* Any valid ref can be used */
23 static int tags
; /* Allow lightweight tags */
24 static int longformat
;
25 static int first_parent
;
26 static int abbrev
= -1; /* unspecified */
27 static int max_candidates
= 10;
28 static struct hashmap names
;
30 static const char *pattern
;
32 static const char *dirty
;
34 /* diff-index command arguments to check if working tree is dirty. */
35 static const char *diff_index_args
[] = {
36 "diff-index", "--quiet", "HEAD", "--", NULL
40 struct hashmap_entry entry
;
41 unsigned char peeled
[20];
43 unsigned prio
:2; /* annotated tag = 2, tag = 1, head = 0 */
44 unsigned name_checked
:1;
45 unsigned char sha1
[20];
49 static const char *prio_names
[] = {
50 "head", "lightweight", "annotated",
53 static int commit_name_cmp(const struct commit_name
*cn1
,
54 const struct commit_name
*cn2
, const void *peeled
)
56 return hashcmp(cn1
->peeled
, peeled
? peeled
: cn2
->peeled
);
59 static inline unsigned int hash_sha1(const unsigned char *sha1
)
62 memcpy(&hash
, sha1
, sizeof(hash
));
66 static inline struct commit_name
*find_commit_name(const unsigned char *peeled
)
68 struct commit_name key
;
69 hashmap_entry_init(&key
, hash_sha1(peeled
));
70 return hashmap_get(&names
, &key
, peeled
);
73 static int replace_name(struct commit_name
*e
,
75 const unsigned char *sha1
,
78 if (!e
|| e
->prio
< prio
)
81 if (e
->prio
== 2 && prio
== 2) {
82 /* Multiple annotated tags point to the same commit.
83 * Select one to keep based upon their tagger date.
88 t
= lookup_tag(e
->sha1
);
89 if (!t
|| parse_tag(t
))
95 if (!t
|| parse_tag(t
))
99 if (e
->tag
->date
< t
->date
)
106 static void add_to_known_names(const char *path
,
107 const unsigned char *peeled
,
109 const unsigned char *sha1
)
111 struct commit_name
*e
= find_commit_name(peeled
);
112 struct tag
*tag
= NULL
;
113 if (replace_name(e
, prio
, sha1
, &tag
)) {
115 e
= xmalloc(sizeof(struct commit_name
));
116 hashcpy(e
->peeled
, peeled
);
117 hashmap_entry_init(e
, hash_sha1(peeled
));
118 hashmap_add(&names
, e
);
124 hashcpy(e
->sha1
, sha1
);
126 e
->path
= xstrdup(path
);
130 static int get_name(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
132 int is_tag
= starts_with(path
, "refs/tags/");
133 unsigned char peeled
[20];
134 int is_annotated
, prio
;
136 /* Reject anything outside refs/tags/ unless --all */
140 /* Accept only tags that match the pattern, if given */
141 if (pattern
&& (!is_tag
|| wildmatch(pattern
, path
+ 10, 0, NULL
)))
144 /* Is it annotated? */
145 if (!peel_ref(path
, peeled
)) {
146 is_annotated
= !!hashcmp(sha1
, peeled
);
148 hashcpy(peeled
, sha1
);
153 * By default, we only use annotated tags, but with --tags
154 * we fall back to lightweight ones (even without --tags,
155 * we still remember lightweight ones, only to give hints
156 * in an error message). --all allows any refs to be used.
165 add_to_known_names(all
? path
+ 5 : path
+ 10, peeled
, prio
, sha1
);
169 struct possible_tag
{
170 struct commit_name
*name
;
173 unsigned flag_within
;
176 static int compare_pt(const void *a_
, const void *b_
)
178 struct possible_tag
*a
= (struct possible_tag
*)a_
;
179 struct possible_tag
*b
= (struct possible_tag
*)b_
;
180 if (a
->depth
!= b
->depth
)
181 return a
->depth
- b
->depth
;
182 if (a
->found_order
!= b
->found_order
)
183 return a
->found_order
- b
->found_order
;
187 static unsigned long finish_depth_computation(
188 struct commit_list
**list
,
189 struct possible_tag
*best
)
191 unsigned long seen_commits
= 0;
193 struct commit
*c
= pop_commit(list
);
194 struct commit_list
*parents
= c
->parents
;
196 if (c
->object
.flags
& best
->flag_within
) {
197 struct commit_list
*a
= *list
;
199 struct commit
*i
= a
->item
;
200 if (!(i
->object
.flags
& best
->flag_within
))
209 struct commit
*p
= parents
->item
;
211 if (!(p
->object
.flags
& SEEN
))
212 commit_list_insert_by_date(p
, list
);
213 p
->object
.flags
|= c
->object
.flags
;
214 parents
= parents
->next
;
220 static void display_name(struct commit_name
*n
)
222 if (n
->prio
== 2 && !n
->tag
) {
223 n
->tag
= lookup_tag(n
->sha1
);
224 if (!n
->tag
|| parse_tag(n
->tag
))
225 die(_("annotated tag %s not available"), n
->path
);
227 if (n
->tag
&& !n
->name_checked
) {
229 die(_("annotated tag %s has no embedded name"), n
->path
);
230 if (strcmp(n
->tag
->tag
, all
? n
->path
+ 5 : n
->path
))
231 warning(_("tag '%s' is really '%s' here"), n
->tag
->tag
, n
->path
);
236 printf("%s", n
->tag
->tag
);
238 printf("%s", n
->path
);
241 static void show_suffix(int depth
, const unsigned char *sha1
)
243 printf("-%d-g%s", depth
, find_unique_abbrev(sha1
, abbrev
));
246 static void describe(const char *arg
, int last_one
)
248 unsigned char sha1
[20];
249 struct commit
*cmit
, *gave_up_on
= NULL
;
250 struct commit_list
*list
;
251 struct commit_name
*n
;
252 struct possible_tag all_matches
[MAX_TAGS
];
253 unsigned int match_cnt
= 0, annotated_cnt
= 0, cur_match
;
254 unsigned long seen_commits
= 0;
255 unsigned int unannotated_cnt
= 0;
257 if (get_sha1(arg
, sha1
))
258 die(_("Not a valid object name %s"), arg
);
259 cmit
= lookup_commit_reference(sha1
);
261 die(_("%s is not a valid '%s' object"), arg
, commit_type
);
263 n
= find_commit_name(cmit
->object
.sha1
);
264 if (n
&& (tags
|| all
|| n
->prio
== 2)) {
266 * Exact match to an existing ref.
270 show_suffix(0, n
->tag
? n
->tag
->tagged
->sha1
: sha1
);
278 die(_("no tag exactly matches '%s'"), sha1_to_hex(cmit
->object
.sha1
));
280 fprintf(stderr
, _("searching to describe %s\n"), arg
);
283 struct hashmap_iter iter
;
285 struct commit_name
*n
= hashmap_iter_first(&names
, &iter
);
286 for (; n
; n
= hashmap_iter_next(&iter
)) {
287 c
= lookup_commit_reference_gently(n
->peeled
, 1);
295 cmit
->object
.flags
= SEEN
;
296 commit_list_insert(cmit
, &list
);
298 struct commit
*c
= pop_commit(&list
);
299 struct commit_list
*parents
= c
->parents
;
303 if (!tags
&& !all
&& n
->prio
< 2) {
305 } else if (match_cnt
< max_candidates
) {
306 struct possible_tag
*t
= &all_matches
[match_cnt
++];
308 t
->depth
= seen_commits
- 1;
309 t
->flag_within
= 1u << match_cnt
;
310 t
->found_order
= match_cnt
;
311 c
->object
.flags
|= t
->flag_within
;
320 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
321 struct possible_tag
*t
= &all_matches
[cur_match
];
322 if (!(c
->object
.flags
& t
->flag_within
))
325 if (annotated_cnt
&& !list
) {
327 fprintf(stderr
, _("finished search at %s\n"),
328 sha1_to_hex(c
->object
.sha1
));
332 struct commit
*p
= parents
->item
;
334 if (!(p
->object
.flags
& SEEN
))
335 commit_list_insert_by_date(p
, &list
);
336 p
->object
.flags
|= c
->object
.flags
;
337 parents
= parents
->next
;
345 const unsigned char *sha1
= cmit
->object
.sha1
;
347 printf("%s", find_unique_abbrev(sha1
, abbrev
));
354 die(_("No annotated tags can describe '%s'.\n"
355 "However, there were unannotated tags: try --tags."),
358 die(_("No tags can describe '%s'.\n"
359 "Try --always, or create some tags."),
363 qsort(all_matches
, match_cnt
, sizeof(all_matches
[0]), compare_pt
);
366 commit_list_insert_by_date(gave_up_on
, &list
);
369 seen_commits
+= finish_depth_computation(&list
, &all_matches
[0]);
370 free_commit_list(list
);
373 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
374 struct possible_tag
*t
= &all_matches
[cur_match
];
375 fprintf(stderr
, " %-11s %8d %s\n",
376 prio_names
[t
->name
->prio
],
377 t
->depth
, t
->name
->path
);
379 fprintf(stderr
, _("traversed %lu commits\n"), seen_commits
);
382 _("more than %i tags found; listed %i most recent\n"
383 "gave up search at %s\n"),
384 max_candidates
, max_candidates
,
385 sha1_to_hex(gave_up_on
->object
.sha1
));
389 display_name(all_matches
[0].name
);
391 show_suffix(all_matches
[0].depth
, cmit
->object
.sha1
);
397 clear_commit_marks(cmit
, -1);
400 int cmd_describe(int argc
, const char **argv
, const char *prefix
)
403 struct option options
[] = {
404 OPT_BOOL(0, "contains", &contains
, N_("find the tag that comes after the commit")),
405 OPT_BOOL(0, "debug", &debug
, N_("debug search strategy on stderr")),
406 OPT_BOOL(0, "all", &all
, N_("use any ref")),
407 OPT_BOOL(0, "tags", &tags
, N_("use any tag, even unannotated")),
408 OPT_BOOL(0, "long", &longformat
, N_("always use long format")),
409 OPT_BOOL(0, "first-parent", &first_parent
, N_("only follow first parent")),
410 OPT__ABBREV(&abbrev
),
411 OPT_SET_INT(0, "exact-match", &max_candidates
,
412 N_("only output exact matches"), 0),
413 OPT_INTEGER(0, "candidates", &max_candidates
,
414 N_("consider <n> most recent tags (default: 10)")),
415 OPT_STRING(0, "match", &pattern
, N_("pattern"),
416 N_("only consider tags matching <pattern>")),
417 OPT_BOOL(0, "always", &always
,
418 N_("show abbreviated commit object as fallback")),
419 {OPTION_STRING
, 0, "dirty", &dirty
, N_("mark"),
420 N_("append <mark> on dirty working tree (default: \"-dirty\")"),
421 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "-dirty"},
425 git_config(git_default_config
, NULL
);
426 argc
= parse_options(argc
, argv
, prefix
, options
, describe_usage
, 0);
428 abbrev
= DEFAULT_ABBREV
;
430 if (max_candidates
< 0)
432 else if (max_candidates
> MAX_TAGS
)
433 max_candidates
= MAX_TAGS
;
435 save_commit_buffer
= 0;
437 if (longformat
&& abbrev
== 0)
438 die(_("--long is incompatible with --abbrev=0"));
441 struct argv_array args
;
443 argv_array_init(&args
);
444 argv_array_pushl(&args
, "name-rev",
445 "--peel-tag", "--name-only", "--no-undefined",
448 argv_array_push(&args
, "--always");
450 argv_array_push(&args
, "--tags");
452 argv_array_pushf(&args
, "--refs=refs/tags/%s", pattern
);
455 argv_array_push(&args
, *argv
);
458 return cmd_name_rev(args
.argc
, args
.argv
, prefix
);
461 hashmap_init(&names
, (hashmap_cmp_fn
) commit_name_cmp
, 0);
462 for_each_rawref(get_name
, NULL
);
463 if (!names
.size
&& !always
)
464 die(_("No names found, cannot describe anything."));
468 static struct lock_file index_lock
;
471 read_cache_preload(NULL
);
472 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
,
474 fd
= hold_locked_index(&index_lock
, 0);
476 update_index_if_able(&the_index
, &index_lock
);
478 if (!cmd_diff_index(ARRAY_SIZE(diff_index_args
) - 1,
479 diff_index_args
, prefix
))
484 die(_("--dirty is incompatible with commit-ishes"));
487 describe(*argv
++, argc
== 0);