8 #include "parse-options.h"
11 #include "argv-array.h"
12 #include "run-command.h"
14 #define SEEN (1u << 0)
15 #define MAX_TAGS (FLAG_BITS - 1)
17 static const char * const describe_usage
[] = {
18 N_("git describe [<options>] [<commit-ish>...]"),
19 N_("git describe [<options>] --dirty"),
23 static int debug
; /* Display lots of verbose info */
24 static int all
; /* Any valid ref can be used */
25 static int tags
; /* Allow lightweight tags */
26 static int longformat
;
27 static int first_parent
;
28 static int abbrev
= -1; /* unspecified */
29 static int max_candidates
= 10;
30 static struct hashmap names
;
32 static struct string_list patterns
= STRING_LIST_INIT_NODUP
;
33 static struct string_list exclude_patterns
= STRING_LIST_INIT_NODUP
;
35 static const char *suffix
, *dirty
, *broken
;
37 /* diff-index command arguments to check if working tree is dirty. */
38 static const char *diff_index_args
[] = {
39 "diff-index", "--quiet", "HEAD", "--", NULL
43 struct hashmap_entry entry
;
44 struct object_id peeled
;
46 unsigned prio
:2; /* annotated tag = 2, tag = 1, head = 0 */
47 unsigned name_checked
:1;
52 static const char *prio_names
[] = {
53 N_("head"), N_("lightweight"), N_("annotated"),
56 static int commit_name_cmp(const struct commit_name
*cn1
,
57 const struct commit_name
*cn2
, const void *peeled
)
59 return oidcmp(&cn1
->peeled
, peeled
? peeled
: &cn2
->peeled
);
62 static inline struct commit_name
*find_commit_name(const struct object_id
*peeled
)
64 return hashmap_get_from_hash(&names
, sha1hash(peeled
->hash
), peeled
->hash
);
67 static int replace_name(struct commit_name
*e
,
69 const struct object_id
*oid
,
72 if (!e
|| e
->prio
< prio
)
75 if (e
->prio
== 2 && prio
== 2) {
76 /* Multiple annotated tags point to the same commit.
77 * Select one to keep based upon their tagger date.
82 t
= lookup_tag(&e
->oid
);
83 if (!t
|| parse_tag(t
))
89 if (!t
|| parse_tag(t
))
93 if (e
->tag
->date
< t
->date
)
100 static void add_to_known_names(const char *path
,
101 const struct object_id
*peeled
,
103 const struct object_id
*oid
)
105 struct commit_name
*e
= find_commit_name(peeled
);
106 struct tag
*tag
= NULL
;
107 if (replace_name(e
, prio
, oid
, &tag
)) {
109 e
= xmalloc(sizeof(struct commit_name
));
110 oidcpy(&e
->peeled
, peeled
);
111 hashmap_entry_init(e
, sha1hash(peeled
->hash
));
112 hashmap_add(&names
, e
);
118 oidcpy(&e
->oid
, oid
);
120 e
->path
= xstrdup(path
);
124 static int get_name(const char *path
, const struct object_id
*oid
, int flag
, void *cb_data
)
126 int is_tag
= starts_with(path
, "refs/tags/");
127 struct object_id peeled
;
128 int is_annotated
, prio
;
130 /* Reject anything outside refs/tags/ unless --all */
135 * If we're given exclude patterns, first exclude any tag which match
136 * any of the exclude pattern.
138 if (exclude_patterns
.nr
) {
139 struct string_list_item
*item
;
144 for_each_string_list_item(item
, &exclude_patterns
) {
145 if (!wildmatch(item
->string
, path
+ 10, 0, NULL
))
151 * 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
))
164 /* If we get here, no pattern matched. */
169 /* Is it annotated? */
170 if (!peel_ref(path
, peeled
.hash
)) {
171 is_annotated
= !!oidcmp(oid
, &peeled
);
173 oidcpy(&peeled
, oid
);
178 * By default, we only use annotated tags, but with --tags
179 * we fall back to lightweight ones (even without --tags,
180 * we still remember lightweight ones, only to give hints
181 * in an error message). --all allows any refs to be used.
190 add_to_known_names(all
? path
+ 5 : path
+ 10, &peeled
, prio
, oid
);
194 struct possible_tag
{
195 struct commit_name
*name
;
198 unsigned flag_within
;
201 static int compare_pt(const void *a_
, const void *b_
)
203 struct possible_tag
*a
= (struct possible_tag
*)a_
;
204 struct possible_tag
*b
= (struct possible_tag
*)b_
;
205 if (a
->depth
!= b
->depth
)
206 return a
->depth
- b
->depth
;
207 if (a
->found_order
!= b
->found_order
)
208 return a
->found_order
- b
->found_order
;
212 static unsigned long finish_depth_computation(
213 struct commit_list
**list
,
214 struct possible_tag
*best
)
216 unsigned long seen_commits
= 0;
218 struct commit
*c
= pop_commit(list
);
219 struct commit_list
*parents
= c
->parents
;
221 if (c
->object
.flags
& best
->flag_within
) {
222 struct commit_list
*a
= *list
;
224 struct commit
*i
= a
->item
;
225 if (!(i
->object
.flags
& best
->flag_within
))
234 struct commit
*p
= parents
->item
;
236 if (!(p
->object
.flags
& SEEN
))
237 commit_list_insert_by_date(p
, list
);
238 p
->object
.flags
|= c
->object
.flags
;
239 parents
= parents
->next
;
245 static void display_name(struct commit_name
*n
)
247 if (n
->prio
== 2 && !n
->tag
) {
248 n
->tag
= lookup_tag(&n
->oid
);
249 if (!n
->tag
|| parse_tag(n
->tag
))
250 die(_("annotated tag %s not available"), n
->path
);
252 if (n
->tag
&& !n
->name_checked
) {
254 die(_("annotated tag %s has no embedded name"), n
->path
);
255 if (strcmp(n
->tag
->tag
, all
? n
->path
+ 5 : n
->path
))
256 warning(_("tag '%s' is really '%s' here"), n
->tag
->tag
, n
->path
);
261 printf("%s", n
->tag
->tag
);
263 printf("%s", n
->path
);
266 static void show_suffix(int depth
, const struct object_id
*oid
)
268 printf("-%d-g%s", depth
, find_unique_abbrev(oid
->hash
, abbrev
));
271 static void describe(const char *arg
, int last_one
)
273 struct object_id oid
;
274 struct commit
*cmit
, *gave_up_on
= NULL
;
275 struct commit_list
*list
;
276 struct commit_name
*n
;
277 struct possible_tag all_matches
[MAX_TAGS
];
278 unsigned int match_cnt
= 0, annotated_cnt
= 0, cur_match
;
279 unsigned long seen_commits
= 0;
280 unsigned int unannotated_cnt
= 0;
282 if (get_oid(arg
, &oid
))
283 die(_("Not a valid object name %s"), arg
);
284 cmit
= lookup_commit_reference(&oid
);
286 die(_("%s is not a valid '%s' object"), arg
, commit_type
);
288 n
= find_commit_name(&cmit
->object
.oid
);
289 if (n
&& (tags
|| all
|| n
->prio
== 2)) {
291 * Exact match to an existing ref.
295 show_suffix(0, n
->tag
? &n
->tag
->tagged
->oid
: &oid
);
297 printf("%s", suffix
);
303 die(_("no tag exactly matches '%s'"), oid_to_hex(&cmit
->object
.oid
));
305 fprintf(stderr
, _("searching to describe %s\n"), arg
);
308 struct hashmap_iter iter
;
310 struct commit_name
*n
= hashmap_iter_first(&names
, &iter
);
311 for (; n
; n
= hashmap_iter_next(&iter
)) {
312 c
= lookup_commit_reference_gently(&n
->peeled
, 1);
320 cmit
->object
.flags
= SEEN
;
321 commit_list_insert(cmit
, &list
);
323 struct commit
*c
= pop_commit(&list
);
324 struct commit_list
*parents
= c
->parents
;
328 if (!tags
&& !all
&& n
->prio
< 2) {
330 } else if (match_cnt
< max_candidates
) {
331 struct possible_tag
*t
= &all_matches
[match_cnt
++];
333 t
->depth
= seen_commits
- 1;
334 t
->flag_within
= 1u << match_cnt
;
335 t
->found_order
= match_cnt
;
336 c
->object
.flags
|= t
->flag_within
;
345 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
346 struct possible_tag
*t
= &all_matches
[cur_match
];
347 if (!(c
->object
.flags
& t
->flag_within
))
350 if (annotated_cnt
&& !list
) {
352 fprintf(stderr
, _("finished search at %s\n"),
353 oid_to_hex(&c
->object
.oid
));
357 struct commit
*p
= parents
->item
;
359 if (!(p
->object
.flags
& SEEN
))
360 commit_list_insert_by_date(p
, &list
);
361 p
->object
.flags
|= c
->object
.flags
;
362 parents
= parents
->next
;
370 struct object_id
*oid
= &cmit
->object
.oid
;
372 printf("%s", find_unique_abbrev(oid
->hash
, abbrev
));
374 printf("%s", suffix
);
379 die(_("No annotated tags can describe '%s'.\n"
380 "However, there were unannotated tags: try --tags."),
383 die(_("No tags can describe '%s'.\n"
384 "Try --always, or create some tags."),
388 QSORT(all_matches
, match_cnt
, compare_pt
);
391 commit_list_insert_by_date(gave_up_on
, &list
);
394 seen_commits
+= finish_depth_computation(&list
, &all_matches
[0]);
395 free_commit_list(list
);
398 static int label_width
= -1;
399 if (label_width
< 0) {
401 for (i
= 0; i
< ARRAY_SIZE(prio_names
); i
++) {
402 w
= strlen(_(prio_names
[i
]));
407 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
408 struct possible_tag
*t
= &all_matches
[cur_match
];
409 fprintf(stderr
, " %-*s %8d %s\n",
410 label_width
, _(prio_names
[t
->name
->prio
]),
411 t
->depth
, t
->name
->path
);
413 fprintf(stderr
, _("traversed %lu commits\n"), seen_commits
);
416 _("more than %i tags found; listed %i most recent\n"
417 "gave up search at %s\n"),
418 max_candidates
, max_candidates
,
419 oid_to_hex(&gave_up_on
->object
.oid
));
423 display_name(all_matches
[0].name
);
425 show_suffix(all_matches
[0].depth
, &cmit
->object
.oid
);
427 printf("%s", suffix
);
431 clear_commit_marks(cmit
, -1);
434 int cmd_describe(int argc
, const char **argv
, const char *prefix
)
437 struct option options
[] = {
438 OPT_BOOL(0, "contains", &contains
, N_("find the tag that comes after the commit")),
439 OPT_BOOL(0, "debug", &debug
, N_("debug search strategy on stderr")),
440 OPT_BOOL(0, "all", &all
, N_("use any ref")),
441 OPT_BOOL(0, "tags", &tags
, N_("use any tag, even unannotated")),
442 OPT_BOOL(0, "long", &longformat
, N_("always use long format")),
443 OPT_BOOL(0, "first-parent", &first_parent
, N_("only follow first parent")),
444 OPT__ABBREV(&abbrev
),
445 OPT_SET_INT(0, "exact-match", &max_candidates
,
446 N_("only output exact matches"), 0),
447 OPT_INTEGER(0, "candidates", &max_candidates
,
448 N_("consider <n> most recent tags (default: 10)")),
449 OPT_STRING_LIST(0, "match", &patterns
, N_("pattern"),
450 N_("only consider tags matching <pattern>")),
451 OPT_STRING_LIST(0, "exclude", &exclude_patterns
, N_("pattern"),
452 N_("do not consider tags matching <pattern>")),
453 OPT_BOOL(0, "always", &always
,
454 N_("show abbreviated commit object as fallback")),
455 {OPTION_STRING
, 0, "dirty", &dirty
, N_("mark"),
456 N_("append <mark> on dirty working tree (default: \"-dirty\")"),
457 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "-dirty"},
458 {OPTION_STRING
, 0, "broken", &broken
, N_("mark"),
459 N_("append <mark> on broken working tree (default: \"-broken\")"),
460 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "-broken"},
464 git_config(git_default_config
, NULL
);
465 argc
= parse_options(argc
, argv
, prefix
, options
, describe_usage
, 0);
467 abbrev
= DEFAULT_ABBREV
;
469 if (max_candidates
< 0)
471 else if (max_candidates
> MAX_TAGS
)
472 max_candidates
= MAX_TAGS
;
474 save_commit_buffer
= 0;
476 if (longformat
&& abbrev
== 0)
477 die(_("--long is incompatible with --abbrev=0"));
480 struct string_list_item
*item
;
481 struct argv_array args
;
483 argv_array_init(&args
);
484 argv_array_pushl(&args
, "name-rev",
485 "--peel-tag", "--name-only", "--no-undefined",
488 argv_array_push(&args
, "--always");
490 argv_array_push(&args
, "--tags");
491 for_each_string_list_item(item
, &patterns
)
492 argv_array_pushf(&args
, "--refs=refs/tags/%s", item
->string
);
493 for_each_string_list_item(item
, &exclude_patterns
)
494 argv_array_pushf(&args
, "--exclude=refs/tags/%s", item
->string
);
497 argv_array_pushv(&args
, argv
);
499 argv_array_push(&args
, "HEAD");
500 return cmd_name_rev(args
.argc
, args
.argv
, prefix
);
503 hashmap_init(&names
, (hashmap_cmp_fn
) commit_name_cmp
, 0);
504 for_each_rawref(get_name
, NULL
);
505 if (!names
.size
&& !always
)
506 die(_("No names found, cannot describe anything."));
510 struct child_process cp
= CHILD_PROCESS_INIT
;
511 argv_array_pushv(&cp
.args
, diff_index_args
);
519 switch (run_command(&cp
)) {
527 /* diff-index aborted abnormally */
531 static struct lock_file index_lock
;
534 read_cache_preload(NULL
);
535 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
,
537 fd
= hold_locked_index(&index_lock
, 0);
539 update_index_if_able(&the_index
, &index_lock
);
541 if (!cmd_diff_index(ARRAY_SIZE(diff_index_args
) - 1,
542 diff_index_args
, prefix
))
549 die(_("--dirty is incompatible with commit-ishes"));
551 die(_("--broken is incompatible with commit-ishes"));
554 describe(*argv
++, argc
== 0);