10 static const char describe_usage
[] =
11 "git-describe [--all] [--tags] [--abbrev=<n>] <committish>*";
13 static int all
; /* Default to annotated tags only */
14 static int tags
; /* But allow any tags if --tags is specified */
15 static int abbrev
= DEFAULT_ABBREV
;
17 static unsigned int names
[256], allocs
[256];
18 static struct commit_name
{
19 struct commit
*commit
;
20 int prio
; /* annotated tag = 2, tag = 1, head = 0 */
21 char path
[FLEX_ARRAY
]; /* more */
24 static struct commit_name
*match(struct commit
*cmit
)
26 unsigned char level0
= cmit
->object
.sha1
[0];
27 struct commit_name
**p
= name_array
[level0
];
28 unsigned int hi
= names
[level0
];
32 unsigned int mi
= (lo
+ hi
) / 2;
33 int cmp
= hashcmp(p
[mi
]->commit
->object
.sha1
,
36 while (mi
&& p
[mi
- 1]->commit
== cmit
)
48 static void add_to_known_names(const char *path
,
49 struct commit
*commit
,
53 int len
= strlen(path
)+1;
54 struct commit_name
*name
= xmalloc(sizeof(struct commit_name
) + len
);
55 unsigned char m
= commit
->object
.sha1
[0];
57 name
->commit
= commit
;
59 memcpy(name
->path
, path
, len
);
61 if (idx
>= allocs
[m
]) {
62 allocs
[m
] = (idx
+ 50) * 3 / 2;
63 name_array
[m
] = xrealloc(name_array
[m
],
64 allocs
[m
] * sizeof(*name_array
));
66 name_array
[m
][idx
] = name
;
70 static int get_name(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
72 struct commit
*commit
= lookup_commit_reference_gently(sha1
, 1);
73 struct object
*object
;
78 object
= parse_object(sha1
);
79 /* If --all, then any refs are used.
80 * If --tags, then any tags are used.
81 * Otherwise only annotated tags are used.
83 if (!strncmp(path
, "refs/tags/", 10)) {
84 if (object
->type
== OBJ_TAG
)
95 if (!tags
&& prio
< 2)
98 add_to_known_names(all
? path
+ 5 : path
+ 10, commit
, prio
);
102 static int compare_names(const void *_a
, const void *_b
)
104 struct commit_name
*a
= *(struct commit_name
**)_a
;
105 struct commit_name
*b
= *(struct commit_name
**)_b
;
106 unsigned long a_date
= a
->commit
->date
;
107 unsigned long b_date
= b
->commit
->date
;
108 int cmp
= hashcmp(a
->commit
->object
.sha1
, b
->commit
->object
.sha1
);
112 if (a
->prio
!= b
->prio
)
113 return b
->prio
- a
->prio
;
114 return (a_date
> b_date
) ? -1 : (a_date
== b_date
) ? 0 : 1;
117 struct possible_tag
{
118 struct possible_tag
*next
;
119 struct commit_name
*name
;
123 static void describe(const char *arg
, int last_one
)
125 unsigned char sha1
[20];
127 struct commit_list
*list
;
128 static int initialized
= 0;
129 struct commit_name
*n
;
130 struct possible_tag
*all_matches
, *min_match
, *cur_match
;
132 if (get_sha1(arg
, sha1
))
133 die("Not a valid object name %s", arg
);
134 cmit
= lookup_commit_reference(sha1
);
136 die("%s is not a valid '%s' object", arg
, commit_type
);
141 for_each_ref(get_name
, NULL
);
142 for (m
= 0; m
< ARRAY_SIZE(name_array
); m
++)
143 qsort(name_array
[m
], names
[m
],
144 sizeof(*name_array
[m
]), compare_names
);
149 printf("%s\n", n
->path
);
156 commit_list_insert(cmit
, &list
);
158 struct commit
*c
= pop_commit(&list
);
159 struct commit_list
*parents
= c
->parents
;
162 struct possible_tag
*p
= xmalloc(sizeof(*p
));
174 struct commit
*p
= parents
->item
;
176 if (!(p
->object
.flags
& SEEN
)) {
177 p
->object
.flags
|= SEEN
;
178 insert_by_date(p
, &list
);
180 parents
= parents
->next
;
185 die("cannot describe '%s'", sha1_to_hex(cmit
->object
.sha1
));
188 for (cur_match
= all_matches
; cur_match
; cur_match
= cur_match
->next
) {
189 struct rev_info revs
;
190 struct commit
*tagged
= cur_match
->name
->commit
;
192 clear_commit_marks(cmit
, -1);
193 init_revisions(&revs
, NULL
);
194 tagged
->object
.flags
|= UNINTERESTING
;
195 add_pending_object(&revs
, &tagged
->object
, NULL
);
196 add_pending_object(&revs
, &cmit
->object
, NULL
);
198 prepare_revision_walk(&revs
);
199 cur_match
->depth
= 0;
200 while ((!min_match
|| cur_match
->depth
< min_match
->depth
)
201 && get_revision(&revs
))
203 if (!min_match
|| (cur_match
->depth
< min_match
->depth
204 && cur_match
->name
->prio
>= min_match
->name
->prio
))
205 min_match
= cur_match
;
206 free_commit_list(revs
.commits
);
208 printf("%s-g%s\n", min_match
->name
->path
,
209 find_unique_abbrev(cmit
->object
.sha1
, abbrev
));
212 for (cur_match
= all_matches
; cur_match
; cur_match
= min_match
) {
213 min_match
= cur_match
->next
;
216 clear_commit_marks(cmit
, SEEN
);
220 int cmd_describe(int argc
, const char **argv
, const char *prefix
)
224 for (i
= 1; i
< argc
; i
++) {
225 const char *arg
= argv
[i
];
229 else if (!strcmp(arg
, "--all"))
231 else if (!strcmp(arg
, "--tags"))
233 else if (!strncmp(arg
, "--abbrev=", 9)) {
234 abbrev
= strtoul(arg
+ 9, NULL
, 10);
235 if (abbrev
< MINIMUM_ABBREV
|| 40 < abbrev
)
236 abbrev
= DEFAULT_ABBREV
;
239 usage(describe_usage
);
242 save_commit_buffer
= 0;
248 describe(argv
[i
], (i
== argc
- 1));