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
;
26 int prio
; /* annotated tag = 2, tag = 1, head = 0 */
27 unsigned char sha1
[20];
28 char path
[FLEX_ARRAY
]; /* more */
30 static const char *prio_names
[] = {
31 "head", "lightweight", "annotated",
34 static void add_to_known_names(const char *path
,
35 struct commit
*commit
,
37 const unsigned char *sha1
)
39 struct commit_name
*e
= commit
->util
;
40 if (!e
|| e
->prio
< prio
) {
41 size_t len
= strlen(path
)+1;
43 e
= xmalloc(sizeof(struct commit_name
) + len
);
46 hashcpy(e
->sha1
, sha1
);
47 memcpy(e
->path
, path
, len
);
52 static int get_name(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
54 int might_be_tag
= !prefixcmp(path
, "refs/tags/");
55 struct commit
*commit
;
56 struct object
*object
;
57 unsigned char peeled
[20];
60 if (!all
&& !might_be_tag
)
63 if (!peel_ref(path
, peeled
) && !is_null_sha1(peeled
)) {
64 commit
= lookup_commit_reference_gently(peeled
, 1);
67 is_tag
= !!hashcmp(sha1
, commit
->object
.sha1
);
69 commit
= lookup_commit_reference_gently(sha1
, 1);
70 object
= parse_object(sha1
);
71 if (!commit
|| !object
)
73 is_tag
= object
->type
== OBJ_TAG
;
76 /* If --all, then any refs are used.
77 * If --tags, then any tags are used.
78 * Otherwise only annotated tags are used.
83 if (pattern
&& fnmatch(pattern
, path
+ 10, 0))
94 if (!tags
&& prio
< 2)
97 add_to_known_names(all
? path
+ 5 : path
+ 10, commit
, prio
, sha1
);
101 struct possible_tag
{
102 struct commit_name
*name
;
105 unsigned flag_within
;
108 static int compare_pt(const void *a_
, const void *b_
)
110 struct possible_tag
*a
= (struct possible_tag
*)a_
;
111 struct possible_tag
*b
= (struct possible_tag
*)b_
;
112 if (a
->name
->prio
!= b
->name
->prio
)
113 return b
->name
->prio
- a
->name
->prio
;
114 if (a
->depth
!= b
->depth
)
115 return a
->depth
- b
->depth
;
116 if (a
->found_order
!= b
->found_order
)
117 return a
->found_order
- b
->found_order
;
121 static unsigned long finish_depth_computation(
122 struct commit_list
**list
,
123 struct possible_tag
*best
)
125 unsigned long seen_commits
= 0;
127 struct commit
*c
= pop_commit(list
);
128 struct commit_list
*parents
= c
->parents
;
130 if (c
->object
.flags
& best
->flag_within
) {
131 struct commit_list
*a
= *list
;
133 struct commit
*i
= a
->item
;
134 if (!(i
->object
.flags
& best
->flag_within
))
143 struct commit
*p
= parents
->item
;
145 if (!(p
->object
.flags
& SEEN
))
146 insert_by_date(p
, list
);
147 p
->object
.flags
|= c
->object
.flags
;
148 parents
= parents
->next
;
154 static void display_name(struct commit_name
*n
)
156 if (n
->prio
== 2 && !n
->tag
) {
157 n
->tag
= lookup_tag(n
->sha1
);
158 if (!n
->tag
|| !n
->tag
->tag
)
159 die("annotated tag %s not available", n
->path
);
160 if (strcmp(n
->tag
->tag
, n
->path
))
161 warning("tag '%s' is really '%s' here", n
->tag
->tag
, n
->path
);
165 printf("%s", n
->tag
->tag
);
167 printf("%s", n
->path
);
170 static void describe(const char *arg
, int last_one
)
172 unsigned char sha1
[20];
173 struct commit
*cmit
, *gave_up_on
= NULL
;
174 struct commit_list
*list
;
175 static int initialized
= 0;
176 struct commit_name
*n
;
177 struct possible_tag all_matches
[MAX_TAGS
];
178 unsigned int match_cnt
= 0, annotated_cnt
= 0, cur_match
;
179 unsigned long seen_commits
= 0;
181 if (get_sha1(arg
, sha1
))
182 die("Not a valid object name %s", arg
);
183 cmit
= lookup_commit_reference(sha1
);
185 die("%s is not a valid '%s' object", arg
, commit_type
);
189 for_each_ref(get_name
, NULL
);
200 die("no tag exactly matches '%s'", sha1_to_hex(cmit
->object
.sha1
));
202 fprintf(stderr
, "searching to describe %s\n", arg
);
205 cmit
->object
.flags
= SEEN
;
206 commit_list_insert(cmit
, &list
);
208 struct commit
*c
= pop_commit(&list
);
209 struct commit_list
*parents
= c
->parents
;
213 if (match_cnt
< max_candidates
) {
214 struct possible_tag
*t
= &all_matches
[match_cnt
++];
216 t
->depth
= seen_commits
- 1;
217 t
->flag_within
= 1u << match_cnt
;
218 t
->found_order
= match_cnt
;
219 c
->object
.flags
|= t
->flag_within
;
228 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
229 struct possible_tag
*t
= &all_matches
[cur_match
];
230 if (!(c
->object
.flags
& t
->flag_within
))
233 if (annotated_cnt
&& !list
) {
235 fprintf(stderr
, "finished search at %s\n",
236 sha1_to_hex(c
->object
.sha1
));
240 struct commit
*p
= parents
->item
;
242 if (!(p
->object
.flags
& SEEN
))
243 insert_by_date(p
, &list
);
244 p
->object
.flags
|= c
->object
.flags
;
245 parents
= parents
->next
;
250 die("cannot describe '%s'", sha1_to_hex(cmit
->object
.sha1
));
252 qsort(all_matches
, match_cnt
, sizeof(all_matches
[0]), compare_pt
);
255 insert_by_date(gave_up_on
, &list
);
258 seen_commits
+= finish_depth_computation(&list
, &all_matches
[0]);
259 free_commit_list(list
);
262 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
263 struct possible_tag
*t
= &all_matches
[cur_match
];
264 fprintf(stderr
, " %-11s %8d %s\n",
265 prio_names
[t
->name
->prio
],
266 t
->depth
, t
->name
->path
);
268 fprintf(stderr
, "traversed %lu commits\n", seen_commits
);
271 "more than %i tags found; listed %i most recent\n"
272 "gave up search at %s\n",
273 max_candidates
, max_candidates
,
274 sha1_to_hex(gave_up_on
->object
.sha1
));
278 display_name(all_matches
[0].name
);
280 printf("-%d-g%s", all_matches
[0].depth
,
281 find_unique_abbrev(cmit
->object
.sha1
, abbrev
));
285 clear_commit_marks(cmit
, -1);
288 int cmd_describe(int argc
, const char **argv
, const char *prefix
)
291 struct option options
[] = {
292 OPT_BOOLEAN(0, "contains", &contains
, "find the tag that comes after the commit"),
293 OPT_BOOLEAN(0, "debug", &debug
, "debug search strategy on stderr"),
294 OPT_BOOLEAN(0, "all", &all
, "use any ref in .git/refs"),
295 OPT_BOOLEAN(0, "tags", &tags
, "use any tag in .git/refs/tags"),
296 OPT__ABBREV(&abbrev
),
297 OPT_SET_INT(0, "exact-match", &max_candidates
,
298 "only output exact matches", 0),
299 OPT_INTEGER(0, "candidates", &max_candidates
,
300 "consider <n> most recent tags (default: 10)"),
301 OPT_STRING(0, "match", &pattern
, "pattern",
302 "only consider tags matching <pattern>"),
306 argc
= parse_options(argc
, argv
, options
, describe_usage
, 0);
307 if (max_candidates
< 0)
309 else if (max_candidates
> MAX_TAGS
)
310 max_candidates
= MAX_TAGS
;
312 save_commit_buffer
= 0;
315 const char **args
= xmalloc((6 + argc
) * sizeof(char*));
317 args
[i
++] = "name-rev";
318 args
[i
++] = "--name-only";
319 args
[i
++] = "--no-undefined";
321 args
[i
++] = "--tags";
323 char *s
= xmalloc(strlen("--refs=refs/tags/") + strlen(pattern
) + 1);
324 sprintf(s
, "--refs=refs/tags/%s", pattern
);
328 memcpy(args
+ i
, argv
, argc
* sizeof(char*));
329 args
[i
+ argc
] = NULL
;
330 return cmd_name_rev(i
+ argc
, args
, prefix
);
337 describe(*argv
++, argc
== 0);