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 struct commit
*commit
= lookup_commit_reference_gently(sha1
, 1);
51 struct object
*object
;
56 object
= parse_object(sha1
);
57 /* If --all, then any refs are used.
58 * If --tags, then any tags are used.
59 * Otherwise only annotated tags are used.
61 if (!prefixcmp(path
, "refs/tags/")) {
62 if (object
->type
== OBJ_TAG
) {
64 if (pattern
&& fnmatch(pattern
, path
+ 10, 0))
75 if (!tags
&& prio
< 2)
78 add_to_known_names(all
? path
+ 5 : path
+ 10, commit
, prio
);
83 struct commit_name
*name
;
89 static int compare_pt(const void *a_
, const void *b_
)
91 struct possible_tag
*a
= (struct possible_tag
*)a_
;
92 struct possible_tag
*b
= (struct possible_tag
*)b_
;
93 if (a
->name
->prio
!= b
->name
->prio
)
94 return b
->name
->prio
- a
->name
->prio
;
95 if (a
->depth
!= b
->depth
)
96 return a
->depth
- b
->depth
;
97 if (a
->found_order
!= b
->found_order
)
98 return a
->found_order
- b
->found_order
;
102 static unsigned long finish_depth_computation(
103 struct commit_list
**list
,
104 struct possible_tag
*best
)
106 unsigned long seen_commits
= 0;
108 struct commit
*c
= pop_commit(list
);
109 struct commit_list
*parents
= c
->parents
;
111 if (c
->object
.flags
& best
->flag_within
) {
112 struct commit_list
*a
= *list
;
114 struct commit
*i
= a
->item
;
115 if (!(i
->object
.flags
& best
->flag_within
))
124 struct commit
*p
= parents
->item
;
126 if (!(p
->object
.flags
& SEEN
))
127 insert_by_date(p
, list
);
128 p
->object
.flags
|= c
->object
.flags
;
129 parents
= parents
->next
;
135 static void describe(const char *arg
, int last_one
)
137 unsigned char sha1
[20];
138 struct commit
*cmit
, *gave_up_on
= NULL
;
139 struct commit_list
*list
;
140 static int initialized
= 0;
141 struct commit_name
*n
;
142 struct possible_tag all_matches
[MAX_TAGS
];
143 unsigned int match_cnt
= 0, annotated_cnt
= 0, cur_match
;
144 unsigned long seen_commits
= 0;
146 if (get_sha1(arg
, sha1
))
147 die("Not a valid object name %s", arg
);
148 cmit
= lookup_commit_reference(sha1
);
150 die("%s is not a valid '%s' object", arg
, commit_type
);
154 for_each_ref(get_name
, NULL
);
160 printf("%s\n", n
->path
);
162 printf("%s-0-g%s\n", n
->path
,
163 find_unique_abbrev(cmit
->object
.sha1
, abbrev
));
168 fprintf(stderr
, "searching to describe %s\n", arg
);
171 cmit
->object
.flags
= SEEN
;
172 commit_list_insert(cmit
, &list
);
174 struct commit
*c
= pop_commit(&list
);
175 struct commit_list
*parents
= c
->parents
;
179 if (match_cnt
< max_candidates
) {
180 struct possible_tag
*t
= &all_matches
[match_cnt
++];
182 t
->depth
= seen_commits
- 1;
183 t
->flag_within
= 1u << match_cnt
;
184 t
->found_order
= match_cnt
;
185 c
->object
.flags
|= t
->flag_within
;
194 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
195 struct possible_tag
*t
= &all_matches
[cur_match
];
196 if (!(c
->object
.flags
& t
->flag_within
))
199 if (annotated_cnt
&& !list
) {
201 fprintf(stderr
, "finished search at %s\n",
202 sha1_to_hex(c
->object
.sha1
));
206 struct commit
*p
= parents
->item
;
208 if (!(p
->object
.flags
& SEEN
))
209 insert_by_date(p
, &list
);
210 p
->object
.flags
|= c
->object
.flags
;
211 parents
= parents
->next
;
216 die("cannot describe '%s'", sha1_to_hex(cmit
->object
.sha1
));
218 qsort(all_matches
, match_cnt
, sizeof(all_matches
[0]), compare_pt
);
221 insert_by_date(gave_up_on
, &list
);
224 seen_commits
+= finish_depth_computation(&list
, &all_matches
[0]);
225 free_commit_list(list
);
228 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
229 struct possible_tag
*t
= &all_matches
[cur_match
];
230 fprintf(stderr
, " %-11s %8d %s\n",
231 prio_names
[t
->name
->prio
],
232 t
->depth
, t
->name
->path
);
234 fprintf(stderr
, "traversed %lu commits\n", seen_commits
);
237 "more than %i tags found; listed %i most recent\n"
238 "gave up search at %s\n",
239 max_candidates
, max_candidates
,
240 sha1_to_hex(gave_up_on
->object
.sha1
));
244 printf("%s\n", all_matches
[0].name
->path
);
246 printf("%s-%d-g%s\n", all_matches
[0].name
->path
,
247 all_matches
[0].depth
,
248 find_unique_abbrev(cmit
->object
.sha1
, abbrev
));
251 clear_commit_marks(cmit
, -1);
254 int cmd_describe(int argc
, const char **argv
, const char *prefix
)
257 struct option options
[] = {
258 OPT_BOOLEAN(0, "contains", &contains
, "find the tag that comes after the commit"),
259 OPT_BOOLEAN(0, "debug", &debug
, "debug search strategy on stderr"),
260 OPT_BOOLEAN(0, "all", &all
, "use any ref in .git/refs"),
261 OPT_BOOLEAN(0, "tags", &tags
, "use any tag in .git/refs/tags"),
262 OPT_BOOLEAN(0, "long", &longformat
, "always use long format"),
263 OPT__ABBREV(&abbrev
),
264 OPT_INTEGER(0, "candidates", &max_candidates
,
265 "consider <n> most recent tags (default: 10)"),
266 OPT_STRING(0, "match", &pattern
, "pattern",
267 "only consider tags matching <pattern>"),
271 argc
= parse_options(argc
, argv
, options
, describe_usage
, 0);
272 if (max_candidates
< 1)
274 else if (max_candidates
> MAX_TAGS
)
275 max_candidates
= MAX_TAGS
;
277 save_commit_buffer
= 0;
279 if (longformat
&& abbrev
== 0)
280 die("--long is incompatible with --abbrev=0");
283 const char **args
= xmalloc((6 + argc
) * sizeof(char*));
285 args
[i
++] = "name-rev";
286 args
[i
++] = "--name-only";
287 args
[i
++] = "--no-undefined";
289 args
[i
++] = "--tags";
291 char *s
= xmalloc(strlen("--refs=refs/tags/") + strlen(pattern
) + 1);
292 sprintf(s
, "--refs=refs/tags/%s", pattern
);
296 memcpy(args
+ i
, argv
, argc
* sizeof(char*));
297 args
[i
+ argc
] = NULL
;
298 return cmd_name_rev(i
+ argc
, args
, prefix
);
305 describe(*argv
++, argc
== 0);