8 #define MAX_TAGS (FLAG_BITS - 1)
10 static const char describe_usage
[] =
11 "git-describe [--all] [--tags] [--abbrev=<n>] <committish>*";
13 static int debug
; /* Display lots of verbose info */
14 static int all
; /* Default to annotated tags only */
15 static int tags
; /* But allow any tags if --tags is specified */
16 static int abbrev
= DEFAULT_ABBREV
;
17 static int max_candidates
= 10;
20 int prio
; /* annotated tag = 2, tag = 1, head = 0 */
21 char path
[FLEX_ARRAY
]; /* more */
23 static const char *prio_names
[] = {
24 "head", "lightweight", "annotated",
27 static void add_to_known_names(const char *path
,
28 struct commit
*commit
,
31 struct commit_name
*e
= commit
->util
;
32 if (!e
|| e
->prio
< prio
) {
33 size_t len
= strlen(path
)+1;
35 e
= xmalloc(sizeof(struct commit_name
) + len
);
37 memcpy(e
->path
, path
, len
);
42 static int get_name(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
44 struct commit
*commit
= lookup_commit_reference_gently(sha1
, 1);
45 struct object
*object
;
50 object
= parse_object(sha1
);
51 /* If --all, then any refs are used.
52 * If --tags, then any tags are used.
53 * Otherwise only annotated tags are used.
55 if (!prefixcmp(path
, "refs/tags/")) {
56 if (object
->type
== OBJ_TAG
)
67 if (!tags
&& prio
< 2)
70 add_to_known_names(all
? path
+ 5 : path
+ 10, commit
, prio
);
75 struct commit_name
*name
;
81 static int compare_pt(const void *a_
, const void *b_
)
83 struct possible_tag
*a
= (struct possible_tag
*)a_
;
84 struct possible_tag
*b
= (struct possible_tag
*)b_
;
85 if (a
->name
->prio
!= b
->name
->prio
)
86 return b
->name
->prio
- a
->name
->prio
;
87 if (a
->depth
!= b
->depth
)
88 return a
->depth
- b
->depth
;
89 if (a
->found_order
!= b
->found_order
)
90 return a
->found_order
- b
->found_order
;
94 static unsigned long finish_depth_computation(
95 struct commit_list
**list
,
96 struct possible_tag
*best
)
98 unsigned long seen_commits
= 0;
100 struct commit
*c
= pop_commit(list
);
101 struct commit_list
*parents
= c
->parents
;
103 if (c
->object
.flags
& best
->flag_within
) {
104 struct commit_list
*a
= *list
;
106 struct commit
*i
= a
->item
;
107 if (!(i
->object
.flags
& best
->flag_within
))
116 struct commit
*p
= parents
->item
;
118 if (!(p
->object
.flags
& SEEN
))
119 insert_by_date(p
, list
);
120 p
->object
.flags
|= c
->object
.flags
;
121 parents
= parents
->next
;
127 static void describe(const char *arg
, int last_one
)
129 unsigned char sha1
[20];
130 struct commit
*cmit
, *gave_up_on
= NULL
;
131 struct commit_list
*list
;
132 static int initialized
= 0;
133 struct commit_name
*n
;
134 struct possible_tag all_matches
[MAX_TAGS
];
135 unsigned int match_cnt
= 0, annotated_cnt
= 0, cur_match
;
136 unsigned long seen_commits
= 0;
138 if (get_sha1(arg
, sha1
))
139 die("Not a valid object name %s", arg
);
140 cmit
= lookup_commit_reference(sha1
);
142 die("%s is not a valid '%s' object", arg
, commit_type
);
146 for_each_ref(get_name
, NULL
);
151 printf("%s\n", n
->path
);
156 fprintf(stderr
, "searching to describe %s\n", arg
);
159 cmit
->object
.flags
= SEEN
;
160 commit_list_insert(cmit
, &list
);
162 struct commit
*c
= pop_commit(&list
);
163 struct commit_list
*parents
= c
->parents
;
167 if (match_cnt
< max_candidates
) {
168 struct possible_tag
*t
= &all_matches
[match_cnt
++];
170 t
->depth
= seen_commits
- 1;
171 t
->flag_within
= 1u << match_cnt
;
172 t
->found_order
= match_cnt
;
173 c
->object
.flags
|= t
->flag_within
;
182 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
183 struct possible_tag
*t
= &all_matches
[cur_match
];
184 if (!(c
->object
.flags
& t
->flag_within
))
187 if (annotated_cnt
&& !list
) {
189 fprintf(stderr
, "finished search at %s\n",
190 sha1_to_hex(c
->object
.sha1
));
194 struct commit
*p
= parents
->item
;
196 if (!(p
->object
.flags
& SEEN
))
197 insert_by_date(p
, &list
);
198 p
->object
.flags
|= c
->object
.flags
;
199 parents
= parents
->next
;
204 die("cannot describe '%s'", sha1_to_hex(cmit
->object
.sha1
));
206 qsort(all_matches
, match_cnt
, sizeof(all_matches
[0]), compare_pt
);
209 insert_by_date(gave_up_on
, &list
);
212 seen_commits
+= finish_depth_computation(&list
, &all_matches
[0]);
213 free_commit_list(list
);
216 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
217 struct possible_tag
*t
= &all_matches
[cur_match
];
218 fprintf(stderr
, " %-11s %8d %s\n",
219 prio_names
[t
->name
->prio
],
220 t
->depth
, t
->name
->path
);
222 fprintf(stderr
, "traversed %lu commits\n", seen_commits
);
225 "more than %i tags found; listed %i most recent\n"
226 "gave up search at %s\n",
227 max_candidates
, max_candidates
,
228 sha1_to_hex(gave_up_on
->object
.sha1
));
232 printf("%s\n", all_matches
[0].name
->path
);
234 printf("%s-%d-g%s\n", all_matches
[0].name
->path
,
235 all_matches
[0].depth
,
236 find_unique_abbrev(cmit
->object
.sha1
, abbrev
));
239 clear_commit_marks(cmit
, -1);
242 int cmd_describe(int argc
, const char **argv
, const char *prefix
)
246 for (i
= 1; i
< argc
; i
++) {
247 const char *arg
= argv
[i
];
251 else if (!strcmp(arg
, "--debug"))
253 else if (!strcmp(arg
, "--all"))
255 else if (!strcmp(arg
, "--tags"))
257 else if (!prefixcmp(arg
, "--abbrev=")) {
258 abbrev
= strtoul(arg
+ 9, NULL
, 10);
259 if (abbrev
!= 0 && (abbrev
< MINIMUM_ABBREV
|| 40 < abbrev
))
260 abbrev
= DEFAULT_ABBREV
;
262 else if (!prefixcmp(arg
, "--candidates=")) {
263 max_candidates
= strtoul(arg
+ 13, NULL
, 10);
264 if (max_candidates
< 1)
266 else if (max_candidates
> MAX_TAGS
)
267 max_candidates
= MAX_TAGS
;
270 usage(describe_usage
);
273 save_commit_buffer
= 0;
279 describe(argv
[i
], (i
== argc
- 1));