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
;
26 int prio
; /* annotated tag = 2, tag = 1, head = 0 */
27 char path
[FLEX_ARRAY
]; /* more */
29 static const char *prio_names
[] = {
30 "head", "lightweight", "annotated",
33 static void add_to_known_names(const char *path
,
34 struct commit
*commit
,
37 struct commit_name
*e
= commit
->util
;
38 if (!e
|| e
->prio
< prio
) {
39 size_t len
= strlen(path
)+1;
41 e
= xmalloc(sizeof(struct commit_name
) + len
);
43 memcpy(e
->path
, path
, len
);
48 static int get_name(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
50 int might_be_tag
= !prefixcmp(path
, "refs/tags/");
51 struct commit
*commit
;
52 struct object
*object
;
53 unsigned char peeled
[20];
56 if (!all
&& !might_be_tag
)
59 if (!peel_ref(path
, peeled
) && !is_null_sha1(peeled
)) {
60 commit
= lookup_commit_reference_gently(peeled
, 1);
63 is_tag
= !!hashcmp(sha1
, commit
->object
.sha1
);
65 commit
= lookup_commit_reference_gently(sha1
, 1);
66 object
= parse_object(sha1
);
67 if (!commit
|| !object
)
69 is_tag
= object
->type
== OBJ_TAG
;
72 /* If --all, then any refs are used.
73 * If --tags, then any tags are used.
74 * Otherwise only annotated tags are used.
79 if (pattern
&& fnmatch(pattern
, path
+ 10, 0))
90 if (!tags
&& prio
< 2)
93 add_to_known_names(all
? path
+ 5 : path
+ 10, commit
, prio
);
98 struct commit_name
*name
;
101 unsigned flag_within
;
104 static int compare_pt(const void *a_
, const void *b_
)
106 struct possible_tag
*a
= (struct possible_tag
*)a_
;
107 struct possible_tag
*b
= (struct possible_tag
*)b_
;
108 if (a
->name
->prio
!= b
->name
->prio
)
109 return b
->name
->prio
- a
->name
->prio
;
110 if (a
->depth
!= b
->depth
)
111 return a
->depth
- b
->depth
;
112 if (a
->found_order
!= b
->found_order
)
113 return a
->found_order
- b
->found_order
;
117 static unsigned long finish_depth_computation(
118 struct commit_list
**list
,
119 struct possible_tag
*best
)
121 unsigned long seen_commits
= 0;
123 struct commit
*c
= pop_commit(list
);
124 struct commit_list
*parents
= c
->parents
;
126 if (c
->object
.flags
& best
->flag_within
) {
127 struct commit_list
*a
= *list
;
129 struct commit
*i
= a
->item
;
130 if (!(i
->object
.flags
& best
->flag_within
))
139 struct commit
*p
= parents
->item
;
141 if (!(p
->object
.flags
& SEEN
))
142 insert_by_date(p
, list
);
143 p
->object
.flags
|= c
->object
.flags
;
144 parents
= parents
->next
;
150 static void describe(const char *arg
, int last_one
)
152 unsigned char sha1
[20];
153 struct commit
*cmit
, *gave_up_on
= NULL
;
154 struct commit_list
*list
;
155 static int initialized
= 0;
156 struct commit_name
*n
;
157 struct possible_tag all_matches
[MAX_TAGS
];
158 unsigned int match_cnt
= 0, annotated_cnt
= 0, cur_match
;
159 unsigned long seen_commits
= 0;
161 if (get_sha1(arg
, sha1
))
162 die("Not a valid object name %s", arg
);
163 cmit
= lookup_commit_reference(sha1
);
165 die("%s is not a valid '%s' object", arg
, commit_type
);
169 for_each_ref(get_name
, NULL
);
175 printf("%s\n", n
->path
);
177 printf("%s-0-g%s\n", n
->path
,
178 find_unique_abbrev(cmit
->object
.sha1
, abbrev
));
183 die("no tag exactly matches '%s'", sha1_to_hex(cmit
->object
.sha1
));
185 fprintf(stderr
, "searching to describe %s\n", arg
);
188 cmit
->object
.flags
= SEEN
;
189 commit_list_insert(cmit
, &list
);
191 struct commit
*c
= pop_commit(&list
);
192 struct commit_list
*parents
= c
->parents
;
196 if (match_cnt
< max_candidates
) {
197 struct possible_tag
*t
= &all_matches
[match_cnt
++];
199 t
->depth
= seen_commits
- 1;
200 t
->flag_within
= 1u << match_cnt
;
201 t
->found_order
= match_cnt
;
202 c
->object
.flags
|= t
->flag_within
;
211 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
212 struct possible_tag
*t
= &all_matches
[cur_match
];
213 if (!(c
->object
.flags
& t
->flag_within
))
216 if (annotated_cnt
&& !list
) {
218 fprintf(stderr
, "finished search at %s\n",
219 sha1_to_hex(c
->object
.sha1
));
223 struct commit
*p
= parents
->item
;
225 if (!(p
->object
.flags
& SEEN
))
226 insert_by_date(p
, &list
);
227 p
->object
.flags
|= c
->object
.flags
;
228 parents
= parents
->next
;
233 die("cannot describe '%s'", sha1_to_hex(cmit
->object
.sha1
));
235 qsort(all_matches
, match_cnt
, sizeof(all_matches
[0]), compare_pt
);
238 insert_by_date(gave_up_on
, &list
);
241 seen_commits
+= finish_depth_computation(&list
, &all_matches
[0]);
242 free_commit_list(list
);
245 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
246 struct possible_tag
*t
= &all_matches
[cur_match
];
247 fprintf(stderr
, " %-11s %8d %s\n",
248 prio_names
[t
->name
->prio
],
249 t
->depth
, t
->name
->path
);
251 fprintf(stderr
, "traversed %lu commits\n", seen_commits
);
254 "more than %i tags found; listed %i most recent\n"
255 "gave up search at %s\n",
256 max_candidates
, max_candidates
,
257 sha1_to_hex(gave_up_on
->object
.sha1
));
261 printf("%s\n", all_matches
[0].name
->path
);
263 printf("%s-%d-g%s\n", all_matches
[0].name
->path
,
264 all_matches
[0].depth
,
265 find_unique_abbrev(cmit
->object
.sha1
, abbrev
));
268 clear_commit_marks(cmit
, -1);
271 int cmd_describe(int argc
, const char **argv
, const char *prefix
)
274 struct option options
[] = {
275 OPT_BOOLEAN(0, "contains", &contains
, "find the tag that comes after the commit"),
276 OPT_BOOLEAN(0, "debug", &debug
, "debug search strategy on stderr"),
277 OPT_BOOLEAN(0, "all", &all
, "use any ref in .git/refs"),
278 OPT_BOOLEAN(0, "tags", &tags
, "use any tag in .git/refs/tags"),
279 OPT_BOOLEAN(0, "long", &longformat
, "always use long format"),
280 OPT__ABBREV(&abbrev
),
281 OPT_SET_INT(0, "exact-match", &max_candidates
,
282 "only output exact matches", 0),
283 OPT_INTEGER(0, "candidates", &max_candidates
,
284 "consider <n> most recent tags (default: 10)"),
285 OPT_STRING(0, "match", &pattern
, "pattern",
286 "only consider tags matching <pattern>"),
290 argc
= parse_options(argc
, argv
, options
, describe_usage
, 0);
291 if (max_candidates
< 0)
293 else if (max_candidates
> MAX_TAGS
)
294 max_candidates
= MAX_TAGS
;
296 save_commit_buffer
= 0;
298 if (longformat
&& abbrev
== 0)
299 die("--long is incompatible with --abbrev=0");
302 const char **args
= xmalloc((6 + argc
) * sizeof(char*));
304 args
[i
++] = "name-rev";
305 args
[i
++] = "--name-only";
306 args
[i
++] = "--no-undefined";
308 args
[i
++] = "--tags";
310 char *s
= xmalloc(strlen("--refs=refs/tags/") + strlen(pattern
) + 1);
311 sprintf(s
, "--refs=refs/tags/%s", pattern
);
315 memcpy(args
+ i
, argv
, argc
* sizeof(char*));
316 args
[i
+ argc
] = NULL
;
317 return cmd_name_rev(i
+ argc
, args
, prefix
);
324 describe(*argv
++, argc
== 0);