7 #include "parse-options.h"
12 #define MAX_TAGS (FLAG_BITS - 1)
14 static const char * const describe_usage
[] = {
15 "git describe [options] <committish>*",
16 "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 abbrev
= DEFAULT_ABBREV
;
25 static int max_candidates
= 10;
26 static struct hash_table names
;
27 static const char *pattern
;
29 static const char *dirty
;
31 /* diff-index command arguments to check if working tree is dirty. */
32 static const char *diff_index_args
[] = {
33 "diff-index", "--quiet", "HEAD", "--", NULL
38 struct commit_name
*next
;
39 unsigned char peeled
[20];
41 unsigned prio
:2; /* annotated tag = 2, tag = 1, head = 0 */
42 unsigned name_checked
:1;
43 unsigned char sha1
[20];
46 static const char *prio_names
[] = {
47 "head", "lightweight", "annotated",
50 static inline unsigned int hash_sha1(const unsigned char *sha1
)
53 memcpy(&hash
, sha1
, sizeof(hash
));
57 static inline struct commit_name
*find_commit_name(const unsigned char *peeled
)
59 struct commit_name
*n
= lookup_hash(hash_sha1(peeled
), &names
);
60 while (n
&& !!hashcmp(peeled
, n
->peeled
))
65 static int replace_name(struct commit_name
*e
,
67 const unsigned char *sha1
,
70 if (!e
|| e
->prio
< prio
)
73 if (e
->prio
== 2 && prio
== 2) {
74 /* Multiple annotated tags point to the same commit.
75 * Select one to keep based upon their tagger date.
80 t
= lookup_tag(e
->sha1
);
81 if (!t
|| parse_tag(t
))
87 if (!t
|| parse_tag(t
))
91 if (e
->tag
->date
< t
->date
)
98 static void add_to_known_names(const char *path
,
99 struct commit
*commit
,
101 const unsigned char *sha1
)
103 const unsigned char *peeled
= commit
->object
.sha1
;
104 struct commit_name
*e
= find_commit_name(peeled
);
105 struct tag
*tag
= NULL
;
106 if (replace_name(e
, prio
, sha1
, &tag
)) {
109 e
= xmalloc(sizeof(struct commit_name
));
111 hashcpy(e
->peeled
, peeled
);
112 pos
= insert_hash(hash_sha1(peeled
), e
, &names
);
123 hashcpy(e
->sha1
, sha1
);
128 static int get_name(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
130 int might_be_tag
= !prefixcmp(path
, "refs/tags/");
131 struct commit
*commit
;
132 struct object
*object
;
133 unsigned char peeled
[20];
136 if (!all
&& !might_be_tag
)
139 if (!peel_ref(path
, peeled
) && !is_null_sha1(peeled
)) {
140 commit
= lookup_commit_reference_gently(peeled
, 1);
143 is_tag
= !!hashcmp(sha1
, commit
->object
.sha1
);
145 commit
= lookup_commit_reference_gently(sha1
, 1);
146 object
= parse_object(sha1
);
147 if (!commit
|| !object
)
149 is_tag
= object
->type
== OBJ_TAG
;
152 /* If --all, then any refs are used.
153 * If --tags, then any tags are used.
154 * Otherwise only annotated tags are used.
162 if (pattern
&& fnmatch(pattern
, path
+ 10, 0))
172 add_to_known_names(all
? path
+ 5 : path
+ 10, commit
, prio
, sha1
);
176 struct possible_tag
{
177 struct commit_name
*name
;
180 unsigned flag_within
;
183 static int compare_pt(const void *a_
, const void *b_
)
185 struct possible_tag
*a
= (struct possible_tag
*)a_
;
186 struct possible_tag
*b
= (struct possible_tag
*)b_
;
187 if (a
->depth
!= b
->depth
)
188 return a
->depth
- b
->depth
;
189 if (a
->found_order
!= b
->found_order
)
190 return a
->found_order
- b
->found_order
;
194 static unsigned long finish_depth_computation(
195 struct commit_list
**list
,
196 struct possible_tag
*best
)
198 unsigned long seen_commits
= 0;
200 struct commit
*c
= pop_commit(list
);
201 struct commit_list
*parents
= c
->parents
;
203 if (c
->object
.flags
& best
->flag_within
) {
204 struct commit_list
*a
= *list
;
206 struct commit
*i
= a
->item
;
207 if (!(i
->object
.flags
& best
->flag_within
))
216 struct commit
*p
= parents
->item
;
218 if (!(p
->object
.flags
& SEEN
))
219 insert_by_date(p
, list
);
220 p
->object
.flags
|= c
->object
.flags
;
221 parents
= parents
->next
;
227 static void display_name(struct commit_name
*n
)
229 if (n
->prio
== 2 && !n
->tag
) {
230 n
->tag
= lookup_tag(n
->sha1
);
231 if (!n
->tag
|| parse_tag(n
->tag
))
232 die("annotated tag %s not available", n
->path
);
234 if (n
->tag
&& !n
->name_checked
) {
236 die("annotated tag %s has no embedded name", n
->path
);
237 if (strcmp(n
->tag
->tag
, all
? n
->path
+ 5 : n
->path
))
238 warning("tag '%s' is really '%s' here", n
->tag
->tag
, n
->path
);
243 printf("%s", n
->tag
->tag
);
245 printf("%s", n
->path
);
248 static void show_suffix(int depth
, const unsigned char *sha1
)
250 printf("-%d-g%s", depth
, find_unique_abbrev(sha1
, abbrev
));
253 static void describe(const char *arg
, int last_one
)
255 unsigned char sha1
[20];
256 struct commit
*cmit
, *gave_up_on
= NULL
;
257 struct commit_list
*list
;
258 struct commit_name
*n
;
259 struct possible_tag all_matches
[MAX_TAGS
];
260 unsigned int match_cnt
= 0, annotated_cnt
= 0, cur_match
;
261 unsigned long seen_commits
= 0;
262 unsigned int unannotated_cnt
= 0;
264 if (get_sha1(arg
, sha1
))
265 die("Not a valid object name %s", arg
);
266 cmit
= lookup_commit_reference(sha1
);
268 die("%s is not a valid '%s' object", arg
, commit_type
);
270 n
= find_commit_name(cmit
->object
.sha1
);
271 if (n
&& (tags
|| all
|| n
->prio
== 2)) {
273 * Exact match to an existing ref.
277 show_suffix(0, n
->tag
? n
->tag
->tagged
->sha1
: sha1
);
285 die("no tag exactly matches '%s'", sha1_to_hex(cmit
->object
.sha1
));
287 fprintf(stderr
, "searching to describe %s\n", arg
);
290 cmit
->object
.flags
= SEEN
;
291 commit_list_insert(cmit
, &list
);
293 struct commit
*c
= pop_commit(&list
);
294 struct commit_list
*parents
= c
->parents
;
298 if (!tags
&& !all
&& n
->prio
< 2) {
300 } else if (match_cnt
< max_candidates
) {
301 struct possible_tag
*t
= &all_matches
[match_cnt
++];
303 t
->depth
= seen_commits
- 1;
304 t
->flag_within
= 1u << match_cnt
;
305 t
->found_order
= match_cnt
;
306 c
->object
.flags
|= t
->flag_within
;
315 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
316 struct possible_tag
*t
= &all_matches
[cur_match
];
317 if (!(c
->object
.flags
& t
->flag_within
))
320 if (annotated_cnt
&& !list
) {
322 fprintf(stderr
, "finished search at %s\n",
323 sha1_to_hex(c
->object
.sha1
));
327 struct commit
*p
= parents
->item
;
329 if (!(p
->object
.flags
& SEEN
))
330 insert_by_date(p
, &list
);
331 p
->object
.flags
|= c
->object
.flags
;
332 parents
= parents
->next
;
337 const unsigned char *sha1
= cmit
->object
.sha1
;
339 printf("%s", find_unique_abbrev(sha1
, abbrev
));
346 die("No annotated tags can describe '%s'.\n"
347 "However, there were unannotated tags: try --tags.",
350 die("No tags can describe '%s'.\n"
351 "Try --always, or create some tags.",
355 qsort(all_matches
, match_cnt
, sizeof(all_matches
[0]), compare_pt
);
358 insert_by_date(gave_up_on
, &list
);
361 seen_commits
+= finish_depth_computation(&list
, &all_matches
[0]);
362 free_commit_list(list
);
365 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
366 struct possible_tag
*t
= &all_matches
[cur_match
];
367 fprintf(stderr
, " %-11s %8d %s\n",
368 prio_names
[t
->name
->prio
],
369 t
->depth
, t
->name
->path
);
371 fprintf(stderr
, "traversed %lu commits\n", seen_commits
);
374 "more than %i tags found; listed %i most recent\n"
375 "gave up search at %s\n",
376 max_candidates
, max_candidates
,
377 sha1_to_hex(gave_up_on
->object
.sha1
));
381 display_name(all_matches
[0].name
);
383 show_suffix(all_matches
[0].depth
, cmit
->object
.sha1
);
389 clear_commit_marks(cmit
, -1);
392 int cmd_describe(int argc
, const char **argv
, const char *prefix
)
395 struct option options
[] = {
396 OPT_BOOLEAN(0, "contains", &contains
, "find the tag that comes after the commit"),
397 OPT_BOOLEAN(0, "debug", &debug
, "debug search strategy on stderr"),
398 OPT_BOOLEAN(0, "all", &all
, "use any ref in .git/refs"),
399 OPT_BOOLEAN(0, "tags", &tags
, "use any tag in .git/refs/tags"),
400 OPT_BOOLEAN(0, "long", &longformat
, "always use long format"),
401 OPT__ABBREV(&abbrev
),
402 OPT_SET_INT(0, "exact-match", &max_candidates
,
403 "only output exact matches", 0),
404 OPT_INTEGER(0, "candidates", &max_candidates
,
405 "consider <n> most recent tags (default: 10)"),
406 OPT_STRING(0, "match", &pattern
, "pattern",
407 "only consider tags matching <pattern>"),
408 OPT_BOOLEAN(0, "always", &always
,
409 "show abbreviated commit object as fallback"),
410 {OPTION_STRING
, 0, "dirty", &dirty
, "mark",
411 "append <mark> on dirty working tree (default: \"-dirty\")",
412 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "-dirty"},
416 argc
= parse_options(argc
, argv
, prefix
, options
, describe_usage
, 0);
417 if (max_candidates
< 0)
419 else if (max_candidates
> MAX_TAGS
)
420 max_candidates
= MAX_TAGS
;
422 save_commit_buffer
= 0;
424 if (longformat
&& abbrev
== 0)
425 die("--long is incompatible with --abbrev=0");
428 const char **args
= xmalloc((7 + argc
) * sizeof(char *));
430 args
[i
++] = "name-rev";
431 args
[i
++] = "--name-only";
432 args
[i
++] = "--no-undefined";
434 args
[i
++] = "--always";
436 args
[i
++] = "--tags";
438 char *s
= xmalloc(strlen("--refs=refs/tags/") + strlen(pattern
) + 1);
439 sprintf(s
, "--refs=refs/tags/%s", pattern
);
443 memcpy(args
+ i
, argv
, argc
* sizeof(char *));
444 args
[i
+ argc
] = NULL
;
445 return cmd_name_rev(i
+ argc
, args
, prefix
);
449 for_each_rawref(get_name
, NULL
);
450 if (!names
.nr
&& !always
)
451 die("No names found, cannot describe anything.");
454 if (dirty
&& !cmd_diff_index(ARRAY_SIZE(diff_index_args
) - 1, diff_index_args
, prefix
))
458 die("--dirty is incompatible with committishes");
461 describe(*argv
++, argc
== 0);