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 die("no tag exactly matches '%s'", sha1_to_hex(cmit
->object
.sha1
));
180 fprintf(stderr
, "searching to describe %s\n", arg
);
183 cmit
->object
.flags
= SEEN
;
184 commit_list_insert(cmit
, &list
);
186 struct commit
*c
= pop_commit(&list
);
187 struct commit_list
*parents
= c
->parents
;
191 if (match_cnt
< max_candidates
) {
192 struct possible_tag
*t
= &all_matches
[match_cnt
++];
194 t
->depth
= seen_commits
- 1;
195 t
->flag_within
= 1u << match_cnt
;
196 t
->found_order
= match_cnt
;
197 c
->object
.flags
|= t
->flag_within
;
206 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
207 struct possible_tag
*t
= &all_matches
[cur_match
];
208 if (!(c
->object
.flags
& t
->flag_within
))
211 if (annotated_cnt
&& !list
) {
213 fprintf(stderr
, "finished search at %s\n",
214 sha1_to_hex(c
->object
.sha1
));
218 struct commit
*p
= parents
->item
;
220 if (!(p
->object
.flags
& SEEN
))
221 insert_by_date(p
, &list
);
222 p
->object
.flags
|= c
->object
.flags
;
223 parents
= parents
->next
;
228 die("cannot describe '%s'", sha1_to_hex(cmit
->object
.sha1
));
230 qsort(all_matches
, match_cnt
, sizeof(all_matches
[0]), compare_pt
);
233 insert_by_date(gave_up_on
, &list
);
236 seen_commits
+= finish_depth_computation(&list
, &all_matches
[0]);
237 free_commit_list(list
);
240 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
241 struct possible_tag
*t
= &all_matches
[cur_match
];
242 fprintf(stderr
, " %-11s %8d %s\n",
243 prio_names
[t
->name
->prio
],
244 t
->depth
, t
->name
->path
);
246 fprintf(stderr
, "traversed %lu commits\n", seen_commits
);
249 "more than %i tags found; listed %i most recent\n"
250 "gave up search at %s\n",
251 max_candidates
, max_candidates
,
252 sha1_to_hex(gave_up_on
->object
.sha1
));
256 printf("%s\n", all_matches
[0].name
->path
);
258 printf("%s-%d-g%s\n", all_matches
[0].name
->path
,
259 all_matches
[0].depth
,
260 find_unique_abbrev(cmit
->object
.sha1
, abbrev
));
263 clear_commit_marks(cmit
, -1);
266 int cmd_describe(int argc
, const char **argv
, const char *prefix
)
269 struct option options
[] = {
270 OPT_BOOLEAN(0, "contains", &contains
, "find the tag that comes after the commit"),
271 OPT_BOOLEAN(0, "debug", &debug
, "debug search strategy on stderr"),
272 OPT_BOOLEAN(0, "all", &all
, "use any ref in .git/refs"),
273 OPT_BOOLEAN(0, "tags", &tags
, "use any tag in .git/refs/tags"),
274 OPT__ABBREV(&abbrev
),
275 OPT_SET_INT(0, "exact-match", &max_candidates
,
276 "only output exact matches", 0),
277 OPT_INTEGER(0, "candidates", &max_candidates
,
278 "consider <n> most recent tags (default: 10)"),
279 OPT_STRING(0, "match", &pattern
, "pattern",
280 "only consider tags matching <pattern>"),
284 argc
= parse_options(argc
, argv
, options
, describe_usage
, 0);
285 if (max_candidates
< 0)
287 else if (max_candidates
> MAX_TAGS
)
288 max_candidates
= MAX_TAGS
;
290 save_commit_buffer
= 0;
293 const char **args
= xmalloc((6 + argc
) * sizeof(char*));
295 args
[i
++] = "name-rev";
296 args
[i
++] = "--name-only";
297 args
[i
++] = "--no-undefined";
299 args
[i
++] = "--tags";
301 char *s
= xmalloc(strlen("--refs=refs/tags/") + strlen(pattern
) + 1);
302 sprintf(s
, "--refs=refs/tags/%s", pattern
);
306 memcpy(args
+ i
, argv
, argc
* sizeof(char*));
307 args
[i
+ argc
] = NULL
;
308 return cmd_name_rev(i
+ argc
, args
, prefix
);
315 describe(*argv
++, argc
== 0);