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 abbrev
= DEFAULT_ABBREV
;
21 static int max_candidates
= 10;
22 const char *pattern
= NULL
;
25 int prio
; /* annotated tag = 2, tag = 1, head = 0 */
26 char path
[FLEX_ARRAY
]; /* more */
28 static const char *prio_names
[] = {
29 "head", "lightweight", "annotated",
32 static void add_to_known_names(const char *path
,
33 struct commit
*commit
,
36 struct commit_name
*e
= commit
->util
;
37 if (!e
|| e
->prio
< prio
) {
38 size_t len
= strlen(path
)+1;
40 e
= xmalloc(sizeof(struct commit_name
) + len
);
42 memcpy(e
->path
, path
, len
);
47 static int get_name(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
49 int might_be_tag
= !prefixcmp(path
, "refs/tags/");
50 struct commit
*commit
;
51 struct object
*object
;
52 unsigned char peeled
[20];
55 if (!all
&& !might_be_tag
)
58 if (!peel_ref(path
, peeled
) && !is_null_sha1(peeled
)) {
59 commit
= lookup_commit_reference_gently(peeled
, 1);
62 is_tag
= !!hashcmp(sha1
, commit
->object
.sha1
);
64 commit
= lookup_commit_reference_gently(sha1
, 1);
65 object
= parse_object(sha1
);
66 if (!commit
|| !object
)
68 is_tag
= object
->type
== OBJ_TAG
;
71 /* If --all, then any refs are used.
72 * If --tags, then any tags are used.
73 * Otherwise only annotated tags are used.
78 if (pattern
&& fnmatch(pattern
, path
+ 10, 0))
89 if (!tags
&& prio
< 2)
92 add_to_known_names(all
? path
+ 5 : path
+ 10, commit
, prio
);
97 struct commit_name
*name
;
100 unsigned flag_within
;
103 static int compare_pt(const void *a_
, const void *b_
)
105 struct possible_tag
*a
= (struct possible_tag
*)a_
;
106 struct possible_tag
*b
= (struct possible_tag
*)b_
;
107 if (a
->name
->prio
!= b
->name
->prio
)
108 return b
->name
->prio
- a
->name
->prio
;
109 if (a
->depth
!= b
->depth
)
110 return a
->depth
- b
->depth
;
111 if (a
->found_order
!= b
->found_order
)
112 return a
->found_order
- b
->found_order
;
116 static unsigned long finish_depth_computation(
117 struct commit_list
**list
,
118 struct possible_tag
*best
)
120 unsigned long seen_commits
= 0;
122 struct commit
*c
= pop_commit(list
);
123 struct commit_list
*parents
= c
->parents
;
125 if (c
->object
.flags
& best
->flag_within
) {
126 struct commit_list
*a
= *list
;
128 struct commit
*i
= a
->item
;
129 if (!(i
->object
.flags
& best
->flag_within
))
138 struct commit
*p
= parents
->item
;
140 if (!(p
->object
.flags
& SEEN
))
141 insert_by_date(p
, list
);
142 p
->object
.flags
|= c
->object
.flags
;
143 parents
= parents
->next
;
149 static void describe(const char *arg
, int last_one
)
151 unsigned char sha1
[20];
152 struct commit
*cmit
, *gave_up_on
= NULL
;
153 struct commit_list
*list
;
154 static int initialized
= 0;
155 struct commit_name
*n
;
156 struct possible_tag all_matches
[MAX_TAGS
];
157 unsigned int match_cnt
= 0, annotated_cnt
= 0, cur_match
;
158 unsigned long seen_commits
= 0;
160 if (get_sha1(arg
, sha1
))
161 die("Not a valid object name %s", arg
);
162 cmit
= lookup_commit_reference(sha1
);
164 die("%s is not a valid '%s' object", arg
, commit_type
);
168 for_each_ref(get_name
, NULL
);
173 printf("%s\n", n
->path
);
178 fprintf(stderr
, "searching to describe %s\n", arg
);
181 cmit
->object
.flags
= SEEN
;
182 commit_list_insert(cmit
, &list
);
184 struct commit
*c
= pop_commit(&list
);
185 struct commit_list
*parents
= c
->parents
;
189 if (match_cnt
< max_candidates
) {
190 struct possible_tag
*t
= &all_matches
[match_cnt
++];
192 t
->depth
= seen_commits
- 1;
193 t
->flag_within
= 1u << match_cnt
;
194 t
->found_order
= match_cnt
;
195 c
->object
.flags
|= t
->flag_within
;
204 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
205 struct possible_tag
*t
= &all_matches
[cur_match
];
206 if (!(c
->object
.flags
& t
->flag_within
))
209 if (annotated_cnt
&& !list
) {
211 fprintf(stderr
, "finished search at %s\n",
212 sha1_to_hex(c
->object
.sha1
));
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
;
226 die("cannot describe '%s'", sha1_to_hex(cmit
->object
.sha1
));
228 qsort(all_matches
, match_cnt
, sizeof(all_matches
[0]), compare_pt
);
231 insert_by_date(gave_up_on
, &list
);
234 seen_commits
+= finish_depth_computation(&list
, &all_matches
[0]);
235 free_commit_list(list
);
238 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
239 struct possible_tag
*t
= &all_matches
[cur_match
];
240 fprintf(stderr
, " %-11s %8d %s\n",
241 prio_names
[t
->name
->prio
],
242 t
->depth
, t
->name
->path
);
244 fprintf(stderr
, "traversed %lu commits\n", seen_commits
);
247 "more than %i tags found; listed %i most recent\n"
248 "gave up search at %s\n",
249 max_candidates
, max_candidates
,
250 sha1_to_hex(gave_up_on
->object
.sha1
));
254 printf("%s\n", all_matches
[0].name
->path
);
256 printf("%s-%d-g%s\n", all_matches
[0].name
->path
,
257 all_matches
[0].depth
,
258 find_unique_abbrev(cmit
->object
.sha1
, abbrev
));
261 clear_commit_marks(cmit
, -1);
264 int cmd_describe(int argc
, const char **argv
, const char *prefix
)
267 struct option options
[] = {
268 OPT_BOOLEAN(0, "contains", &contains
, "find the tag that comes after the commit"),
269 OPT_BOOLEAN(0, "debug", &debug
, "debug search strategy on stderr"),
270 OPT_BOOLEAN(0, "all", &all
, "use any ref in .git/refs"),
271 OPT_BOOLEAN(0, "tags", &tags
, "use any tag in .git/refs/tags"),
272 OPT__ABBREV(&abbrev
),
273 OPT_INTEGER(0, "candidates", &max_candidates
,
274 "consider <n> most recent tags (default: 10)"),
275 OPT_STRING(0, "match", &pattern
, "pattern",
276 "only consider tags matching <pattern>"),
280 argc
= parse_options(argc
, argv
, options
, describe_usage
, 0);
281 if (max_candidates
< 1)
283 else if (max_candidates
> MAX_TAGS
)
284 max_candidates
= MAX_TAGS
;
286 save_commit_buffer
= 0;
289 const char **args
= xmalloc((6 + argc
) * sizeof(char*));
291 args
[i
++] = "name-rev";
292 args
[i
++] = "--name-only";
293 args
[i
++] = "--no-undefined";
295 args
[i
++] = "--tags";
297 char *s
= xmalloc(strlen("--refs=refs/tags/") + strlen(pattern
) + 1);
298 sprintf(s
, "--refs=refs/tags/%s", pattern
);
302 memcpy(args
+ i
, argv
, argc
* sizeof(char*));
303 args
[i
+ argc
] = NULL
;
304 return cmd_name_rev(i
+ argc
, args
, prefix
);
311 describe(*argv
++, argc
== 0);