7 #include "parse-options.h"
12 #define MAX_TAGS (FLAG_BITS - 1)
14 static const char * const describe_usage
[] = {
15 N_("git describe [options] <committish>*"),
16 N_("git describe [options] --dirty"),
20 static int debug
; /* Display lots of verbose info */
21 static int all
; /* Any valid ref can be used */
22 static int tags
; /* Allow lightweight tags */
23 static int longformat
;
24 static int first_parent
;
25 static int abbrev
= -1; /* unspecified */
26 static int max_candidates
= 10;
27 static struct hash_table names
;
29 static const char *pattern
;
31 static const char *dirty
;
33 /* diff-index command arguments to check if working tree is dirty. */
34 static const char *diff_index_args
[] = {
35 "diff-index", "--quiet", "HEAD", "--", NULL
40 struct commit_name
*next
;
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];
48 static const char *prio_names
[] = {
49 "head", "lightweight", "annotated",
52 static inline unsigned int hash_sha1(const unsigned char *sha1
)
55 memcpy(&hash
, sha1
, sizeof(hash
));
59 static inline struct commit_name
*find_commit_name(const unsigned char *peeled
)
61 struct commit_name
*n
= lookup_hash(hash_sha1(peeled
), &names
);
62 while (n
&& !!hashcmp(peeled
, n
->peeled
))
67 static int set_util(void *chain
, void *data
)
69 struct commit_name
*n
;
70 for (n
= chain
; n
; n
= n
->next
) {
71 struct commit
*c
= lookup_commit_reference_gently(n
->peeled
, 1);
78 static int replace_name(struct commit_name
*e
,
80 const unsigned char *sha1
,
83 if (!e
|| e
->prio
< prio
)
86 if (e
->prio
== 2 && prio
== 2) {
87 /* Multiple annotated tags point to the same commit.
88 * Select one to keep based upon their tagger date.
93 t
= lookup_tag(e
->sha1
);
94 if (!t
|| parse_tag(t
))
100 if (!t
|| parse_tag(t
))
104 if (e
->tag
->date
< t
->date
)
111 static void add_to_known_names(const char *path
,
112 const unsigned char *peeled
,
114 const unsigned char *sha1
)
116 struct commit_name
*e
= find_commit_name(peeled
);
117 struct tag
*tag
= NULL
;
118 if (replace_name(e
, prio
, sha1
, &tag
)) {
121 e
= xmalloc(sizeof(struct commit_name
));
122 hashcpy(e
->peeled
, peeled
);
123 pos
= insert_hash(hash_sha1(peeled
), e
, &names
);
135 hashcpy(e
->sha1
, sha1
);
137 e
->path
= xstrdup(path
);
141 static int get_name(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
143 int is_tag
= !prefixcmp(path
, "refs/tags/");
144 unsigned char peeled
[20];
145 int is_annotated
, prio
;
147 /* Reject anything outside refs/tags/ unless --all */
151 /* Accept only tags that match the pattern, if given */
152 if (pattern
&& (!is_tag
|| fnmatch(pattern
, path
+ 10, 0)))
155 /* Is it annotated? */
156 if (!peel_ref(path
, peeled
)) {
157 is_annotated
= !!hashcmp(sha1
, peeled
);
159 hashcpy(peeled
, sha1
);
164 * By default, we only use annotated tags, but with --tags
165 * we fall back to lightweight ones (even without --tags,
166 * we still remember lightweight ones, only to give hints
167 * in an error message). --all allows any refs to be used.
176 add_to_known_names(all
? path
+ 5 : path
+ 10, peeled
, prio
, sha1
);
180 struct possible_tag
{
181 struct commit_name
*name
;
184 unsigned flag_within
;
187 static int compare_pt(const void *a_
, const void *b_
)
189 struct possible_tag
*a
= (struct possible_tag
*)a_
;
190 struct possible_tag
*b
= (struct possible_tag
*)b_
;
191 if (a
->depth
!= b
->depth
)
192 return a
->depth
- b
->depth
;
193 if (a
->found_order
!= b
->found_order
)
194 return a
->found_order
- b
->found_order
;
198 static unsigned long finish_depth_computation(
199 struct commit_list
**list
,
200 struct possible_tag
*best
)
202 unsigned long seen_commits
= 0;
204 struct commit
*c
= pop_commit(list
);
205 struct commit_list
*parents
= c
->parents
;
207 if (c
->object
.flags
& best
->flag_within
) {
208 struct commit_list
*a
= *list
;
210 struct commit
*i
= a
->item
;
211 if (!(i
->object
.flags
& best
->flag_within
))
220 struct commit
*p
= parents
->item
;
222 if (!(p
->object
.flags
& SEEN
))
223 commit_list_insert_by_date(p
, list
);
224 p
->object
.flags
|= c
->object
.flags
;
225 parents
= parents
->next
;
231 static void display_name(struct commit_name
*n
)
233 if (n
->prio
== 2 && !n
->tag
) {
234 n
->tag
= lookup_tag(n
->sha1
);
235 if (!n
->tag
|| parse_tag(n
->tag
))
236 die(_("annotated tag %s not available"), n
->path
);
238 if (n
->tag
&& !n
->name_checked
) {
240 die(_("annotated tag %s has no embedded name"), n
->path
);
241 if (strcmp(n
->tag
->tag
, all
? n
->path
+ 5 : n
->path
))
242 warning(_("tag '%s' is really '%s' here"), n
->tag
->tag
, n
->path
);
247 printf("%s", n
->tag
->tag
);
249 printf("%s", n
->path
);
252 static void show_suffix(int depth
, const unsigned char *sha1
)
254 printf("-%d-g%s", depth
, find_unique_abbrev(sha1
, abbrev
));
257 static void describe(const char *arg
, int last_one
)
259 unsigned char sha1
[20];
260 struct commit
*cmit
, *gave_up_on
= NULL
;
261 struct commit_list
*list
;
262 struct commit_name
*n
;
263 struct possible_tag all_matches
[MAX_TAGS
];
264 unsigned int match_cnt
= 0, annotated_cnt
= 0, cur_match
;
265 unsigned long seen_commits
= 0;
266 unsigned int unannotated_cnt
= 0;
268 if (get_sha1(arg
, sha1
))
269 die(_("Not a valid object name %s"), arg
);
270 cmit
= lookup_commit_reference(sha1
);
272 die(_("%s is not a valid '%s' object"), arg
, commit_type
);
274 n
= find_commit_name(cmit
->object
.sha1
);
275 if (n
&& (tags
|| all
|| n
->prio
== 2)) {
277 * Exact match to an existing ref.
281 show_suffix(0, n
->tag
? n
->tag
->tagged
->sha1
: sha1
);
289 die(_("no tag exactly matches '%s'"), sha1_to_hex(cmit
->object
.sha1
));
291 fprintf(stderr
, _("searching to describe %s\n"), arg
);
294 for_each_hash(&names
, set_util
, NULL
);
299 cmit
->object
.flags
= SEEN
;
300 commit_list_insert(cmit
, &list
);
302 struct commit
*c
= pop_commit(&list
);
303 struct commit_list
*parents
= c
->parents
;
307 if (!tags
&& !all
&& n
->prio
< 2) {
309 } else if (match_cnt
< max_candidates
) {
310 struct possible_tag
*t
= &all_matches
[match_cnt
++];
312 t
->depth
= seen_commits
- 1;
313 t
->flag_within
= 1u << match_cnt
;
314 t
->found_order
= match_cnt
;
315 c
->object
.flags
|= t
->flag_within
;
324 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
325 struct possible_tag
*t
= &all_matches
[cur_match
];
326 if (!(c
->object
.flags
& t
->flag_within
))
329 if (annotated_cnt
&& !list
) {
331 fprintf(stderr
, _("finished search at %s\n"),
332 sha1_to_hex(c
->object
.sha1
));
336 struct commit
*p
= parents
->item
;
338 if (!(p
->object
.flags
& SEEN
))
339 commit_list_insert_by_date(p
, &list
);
340 p
->object
.flags
|= c
->object
.flags
;
341 parents
= parents
->next
;
349 const unsigned char *sha1
= cmit
->object
.sha1
;
351 printf("%s", find_unique_abbrev(sha1
, abbrev
));
358 die(_("No annotated tags can describe '%s'.\n"
359 "However, there were unannotated tags: try --tags."),
362 die(_("No tags can describe '%s'.\n"
363 "Try --always, or create some tags."),
367 qsort(all_matches
, match_cnt
, sizeof(all_matches
[0]), compare_pt
);
370 commit_list_insert_by_date(gave_up_on
, &list
);
373 seen_commits
+= finish_depth_computation(&list
, &all_matches
[0]);
374 free_commit_list(list
);
377 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
378 struct possible_tag
*t
= &all_matches
[cur_match
];
379 fprintf(stderr
, " %-11s %8d %s\n",
380 prio_names
[t
->name
->prio
],
381 t
->depth
, t
->name
->path
);
383 fprintf(stderr
, _("traversed %lu commits\n"), seen_commits
);
386 _("more than %i tags found; listed %i most recent\n"
387 "gave up search at %s\n"),
388 max_candidates
, max_candidates
,
389 sha1_to_hex(gave_up_on
->object
.sha1
));
393 display_name(all_matches
[0].name
);
395 show_suffix(all_matches
[0].depth
, cmit
->object
.sha1
);
401 clear_commit_marks(cmit
, -1);
404 int cmd_describe(int argc
, const char **argv
, const char *prefix
)
407 struct option options
[] = {
408 OPT_BOOLEAN(0, "contains", &contains
, N_("find the tag that comes after the commit")),
409 OPT_BOOLEAN(0, "debug", &debug
, N_("debug search strategy on stderr")),
410 OPT_BOOLEAN(0, "all", &all
, N_("use any ref")),
411 OPT_BOOLEAN(0, "tags", &tags
, N_("use any tag, even unannotated")),
412 OPT_BOOLEAN(0, "long", &longformat
, N_("always use long format")),
413 OPT_BOOLEAN(0, "first-parent", &first_parent
, N_("only follow first parent")),
414 OPT__ABBREV(&abbrev
),
415 OPT_SET_INT(0, "exact-match", &max_candidates
,
416 N_("only output exact matches"), 0),
417 OPT_INTEGER(0, "candidates", &max_candidates
,
418 N_("consider <n> most recent tags (default: 10)")),
419 OPT_STRING(0, "match", &pattern
, N_("pattern"),
420 N_("only consider tags matching <pattern>")),
421 OPT_BOOLEAN(0, "always", &always
,
422 N_("show abbreviated commit object as fallback")),
423 {OPTION_STRING
, 0, "dirty", &dirty
, N_("mark"),
424 N_("append <mark> on dirty working tree (default: \"-dirty\")"),
425 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "-dirty"},
429 git_config(git_default_config
, NULL
);
430 argc
= parse_options(argc
, argv
, prefix
, options
, describe_usage
, 0);
432 abbrev
= DEFAULT_ABBREV
;
434 if (max_candidates
< 0)
436 else if (max_candidates
> MAX_TAGS
)
437 max_candidates
= MAX_TAGS
;
439 save_commit_buffer
= 0;
441 if (longformat
&& abbrev
== 0)
442 die(_("--long is incompatible with --abbrev=0"));
445 const char **args
= xmalloc((7 + argc
) * sizeof(char *));
447 args
[i
++] = "name-rev";
448 args
[i
++] = "--name-only";
449 args
[i
++] = "--no-undefined";
451 args
[i
++] = "--always";
453 args
[i
++] = "--tags";
455 char *s
= xmalloc(strlen("--refs=refs/tags/") + strlen(pattern
) + 1);
456 sprintf(s
, "--refs=refs/tags/%s", pattern
);
460 memcpy(args
+ i
, argv
, argc
* sizeof(char *));
461 args
[i
+ argc
] = NULL
;
462 return cmd_name_rev(i
+ argc
, args
, prefix
);
466 for_each_rawref(get_name
, NULL
);
467 if (!names
.nr
&& !always
)
468 die(_("No names found, cannot describe anything."));
472 static struct lock_file index_lock
;
475 read_cache_preload(NULL
);
476 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
,
478 fd
= hold_locked_index(&index_lock
, 0);
480 update_index_if_able(&the_index
, &index_lock
);
482 if (!cmd_diff_index(ARRAY_SIZE(diff_index_args
) - 1,
483 diff_index_args
, prefix
))
488 die(_("--dirty is incompatible with committishes"));
491 describe(*argv
++, argc
== 0);