7 #include "parse-options.h"
10 #define MAX_TAGS (FLAG_BITS - 1)
12 static const char * const describe_usage
[] = {
13 "git-describe [options] <committish>*",
17 static int debug
; /* Display lots of verbose info */
18 static int all
; /* Default to annotated tags only */
19 static int tags
; /* But allow any tags if --tags is specified */
20 static int longformat
;
21 static int abbrev
= DEFAULT_ABBREV
;
22 static int max_candidates
= 10;
23 const char *pattern
= NULL
;
28 int prio
; /* annotated tag = 2, tag = 1, head = 0 */
29 unsigned char sha1
[20];
30 char path
[FLEX_ARRAY
]; /* more */
32 static const char *prio_names
[] = {
33 "head", "lightweight", "annotated",
36 static void add_to_known_names(const char *path
,
37 struct commit
*commit
,
39 const unsigned char *sha1
)
41 struct commit_name
*e
= commit
->util
;
42 if (!e
|| e
->prio
< prio
) {
43 size_t len
= strlen(path
)+1;
45 e
= xmalloc(sizeof(struct commit_name
) + len
);
48 hashcpy(e
->sha1
, sha1
);
49 memcpy(e
->path
, path
, len
);
54 static int get_name(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
56 int might_be_tag
= !prefixcmp(path
, "refs/tags/");
57 struct commit
*commit
;
58 struct object
*object
;
59 unsigned char peeled
[20];
62 if (!all
&& !might_be_tag
)
65 if (!peel_ref(path
, peeled
) && !is_null_sha1(peeled
)) {
66 commit
= lookup_commit_reference_gently(peeled
, 1);
69 is_tag
= !!hashcmp(sha1
, commit
->object
.sha1
);
71 commit
= lookup_commit_reference_gently(sha1
, 1);
72 object
= parse_object(sha1
);
73 if (!commit
|| !object
)
75 is_tag
= object
->type
== OBJ_TAG
;
78 /* If --all, then any refs are used.
79 * If --tags, then any tags are used.
80 * Otherwise only annotated tags are used.
85 if (pattern
&& fnmatch(pattern
, path
+ 10, 0))
96 if (!tags
&& prio
< 2)
99 add_to_known_names(all
? path
+ 5 : path
+ 10, commit
, prio
, sha1
);
103 struct possible_tag
{
104 struct commit_name
*name
;
107 unsigned flag_within
;
110 static int compare_pt(const void *a_
, const void *b_
)
112 struct possible_tag
*a
= (struct possible_tag
*)a_
;
113 struct possible_tag
*b
= (struct possible_tag
*)b_
;
114 if (a
->name
->prio
!= b
->name
->prio
)
115 return b
->name
->prio
- a
->name
->prio
;
116 if (a
->depth
!= b
->depth
)
117 return a
->depth
- b
->depth
;
118 if (a
->found_order
!= b
->found_order
)
119 return a
->found_order
- b
->found_order
;
123 static unsigned long finish_depth_computation(
124 struct commit_list
**list
,
125 struct possible_tag
*best
)
127 unsigned long seen_commits
= 0;
129 struct commit
*c
= pop_commit(list
);
130 struct commit_list
*parents
= c
->parents
;
132 if (c
->object
.flags
& best
->flag_within
) {
133 struct commit_list
*a
= *list
;
135 struct commit
*i
= a
->item
;
136 if (!(i
->object
.flags
& best
->flag_within
))
145 struct commit
*p
= parents
->item
;
147 if (!(p
->object
.flags
& SEEN
))
148 insert_by_date(p
, list
);
149 p
->object
.flags
|= c
->object
.flags
;
150 parents
= parents
->next
;
156 static void display_name(struct commit_name
*n
)
158 if (n
->prio
== 2 && !n
->tag
) {
159 n
->tag
= lookup_tag(n
->sha1
);
160 if (!n
->tag
|| parse_tag(n
->tag
) || !n
->tag
->tag
)
161 die("annotated tag %s not available", n
->path
);
162 if (strcmp(n
->tag
->tag
, n
->path
))
163 warning("tag '%s' is really '%s' here", n
->tag
->tag
, n
->path
);
167 printf("%s", n
->tag
->tag
);
169 printf("%s", n
->path
);
172 static void show_suffix(int depth
, const unsigned char *sha1
)
174 printf("-%d-g%s", depth
, find_unique_abbrev(sha1
, abbrev
));
177 static void describe(const char *arg
, int last_one
)
179 unsigned char sha1
[20];
180 struct commit
*cmit
, *gave_up_on
= NULL
;
181 struct commit_list
*list
;
182 static int initialized
= 0;
183 struct commit_name
*n
;
184 struct possible_tag all_matches
[MAX_TAGS
];
185 unsigned int match_cnt
= 0, annotated_cnt
= 0, cur_match
;
186 unsigned long seen_commits
= 0;
188 if (get_sha1(arg
, sha1
))
189 die("Not a valid object name %s", arg
);
190 cmit
= lookup_commit_reference(sha1
);
192 die("%s is not a valid '%s' object", arg
, commit_type
);
196 for_each_ref(get_name
, NULL
);
202 * Exact match to an existing ref.
206 show_suffix(0, n
->tag
->tagged
->sha1
);
212 die("no tag exactly matches '%s'", sha1_to_hex(cmit
->object
.sha1
));
214 fprintf(stderr
, "searching to describe %s\n", arg
);
217 cmit
->object
.flags
= SEEN
;
218 commit_list_insert(cmit
, &list
);
220 struct commit
*c
= pop_commit(&list
);
221 struct commit_list
*parents
= c
->parents
;
225 if (match_cnt
< max_candidates
) {
226 struct possible_tag
*t
= &all_matches
[match_cnt
++];
228 t
->depth
= seen_commits
- 1;
229 t
->flag_within
= 1u << match_cnt
;
230 t
->found_order
= match_cnt
;
231 c
->object
.flags
|= t
->flag_within
;
240 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
241 struct possible_tag
*t
= &all_matches
[cur_match
];
242 if (!(c
->object
.flags
& t
->flag_within
))
245 if (annotated_cnt
&& !list
) {
247 fprintf(stderr
, "finished search at %s\n",
248 sha1_to_hex(c
->object
.sha1
));
252 struct commit
*p
= parents
->item
;
254 if (!(p
->object
.flags
& SEEN
))
255 insert_by_date(p
, &list
);
256 p
->object
.flags
|= c
->object
.flags
;
257 parents
= parents
->next
;
262 const unsigned char *sha1
= cmit
->object
.sha1
;
264 printf("%s\n", find_unique_abbrev(sha1
, abbrev
));
267 die("cannot describe '%s'", sha1_to_hex(sha1
));
270 qsort(all_matches
, match_cnt
, sizeof(all_matches
[0]), compare_pt
);
273 insert_by_date(gave_up_on
, &list
);
276 seen_commits
+= finish_depth_computation(&list
, &all_matches
[0]);
277 free_commit_list(list
);
280 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
281 struct possible_tag
*t
= &all_matches
[cur_match
];
282 fprintf(stderr
, " %-11s %8d %s\n",
283 prio_names
[t
->name
->prio
],
284 t
->depth
, t
->name
->path
);
286 fprintf(stderr
, "traversed %lu commits\n", seen_commits
);
289 "more than %i tags found; listed %i most recent\n"
290 "gave up search at %s\n",
291 max_candidates
, max_candidates
,
292 sha1_to_hex(gave_up_on
->object
.sha1
));
296 display_name(all_matches
[0].name
);
298 show_suffix(all_matches
[0].depth
, cmit
->object
.sha1
);
302 clear_commit_marks(cmit
, -1);
305 int cmd_describe(int argc
, const char **argv
, const char *prefix
)
308 struct option options
[] = {
309 OPT_BOOLEAN(0, "contains", &contains
, "find the tag that comes after the commit"),
310 OPT_BOOLEAN(0, "debug", &debug
, "debug search strategy on stderr"),
311 OPT_BOOLEAN(0, "all", &all
, "use any ref in .git/refs"),
312 OPT_BOOLEAN(0, "tags", &tags
, "use any tag in .git/refs/tags"),
313 OPT_BOOLEAN(0, "long", &longformat
, "always use long format"),
314 OPT__ABBREV(&abbrev
),
315 OPT_SET_INT(0, "exact-match", &max_candidates
,
316 "only output exact matches", 0),
317 OPT_INTEGER(0, "candidates", &max_candidates
,
318 "consider <n> most recent tags (default: 10)"),
319 OPT_STRING(0, "match", &pattern
, "pattern",
320 "only consider tags matching <pattern>"),
321 OPT_BOOLEAN(0, "always", &always
,
322 "show abbreviated commit object as fallback"),
326 argc
= parse_options(argc
, argv
, options
, describe_usage
, 0);
327 if (max_candidates
< 0)
329 else if (max_candidates
> MAX_TAGS
)
330 max_candidates
= MAX_TAGS
;
332 save_commit_buffer
= 0;
334 if (longformat
&& abbrev
== 0)
335 die("--long is incompatible with --abbrev=0");
338 const char **args
= xmalloc((7 + argc
) * sizeof(char*));
340 args
[i
++] = "name-rev";
341 args
[i
++] = "--name-only";
342 args
[i
++] = "--no-undefined";
344 args
[i
++] = "--always";
346 args
[i
++] = "--tags";
348 char *s
= xmalloc(strlen("--refs=refs/tags/") + strlen(pattern
) + 1);
349 sprintf(s
, "--refs=refs/tags/%s", pattern
);
353 memcpy(args
+ i
, argv
, argc
* sizeof(char*));
354 args
[i
+ argc
] = NULL
;
355 return cmd_name_rev(i
+ argc
, args
, prefix
);
362 describe(*argv
++, argc
== 0);