]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-describe.c
Merge branch 'jc/gitignore-ends-with-slash'
[thirdparty/git.git] / builtin-describe.c
CommitLineData
908e5310
LT
1#include "cache.h"
2#include "commit.h"
635d4134 3#include "tag.h"
908e5310 4#include "refs.h"
9a0eaf83 5#include "builtin.h"
23615708 6#include "exec_cmd.h"
166185be 7#include "parse-options.h"
908e5310 8
8713ab30
SP
9#define SEEN (1u<<0)
10#define MAX_TAGS (FLAG_BITS - 1)
11
166185be
PH
12static const char * const describe_usage[] = {
13 "git-describe [options] <committish>*",
14 NULL
15};
908e5310 16
8713ab30 17static int debug; /* Display lots of verbose info */
96f1e58f
DR
18static int all; /* Default to annotated tags only */
19static int tags; /* But allow any tags if --tags is specified */
2d9e7c9f 20static int abbrev = DEFAULT_ABBREV;
8713ab30 21static int max_candidates = 10;
30ffa603 22const char *pattern = NULL;
908e5310 23
e7eb5034 24struct commit_name {
64deb858 25 int prio; /* annotated tag = 2, tag = 1, head = 0 */
5a2282de 26 char path[FLEX_ARRAY]; /* more */
e7eb5034 27};
cf69fd49
SP
28static const char *prio_names[] = {
29 "head", "lightweight", "annotated",
30};
908e5310 31
64deb858 32static void add_to_known_names(const char *path,
80dbae03 33 struct commit *commit,
64deb858 34 int prio)
908e5310 35{
e7eb5034
SP
36 struct commit_name *e = commit->util;
37 if (!e || e->prio < prio) {
38 size_t len = strlen(path)+1;
39 free(e);
40 e = xmalloc(sizeof(struct commit_name) + len);
41 e->prio = prio;
42 memcpy(e->path, path, len);
43 commit->util = e;
908e5310 44 }
908e5310
LT
45}
46
8da19775 47static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
908e5310
LT
48{
49 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
64deb858
JH
50 struct object *object;
51 int prio;
52
908e5310
LT
53 if (!commit)
54 return 0;
64deb858 55 object = parse_object(sha1);
2d9e7c9f
JH
56 /* If --all, then any refs are used.
57 * If --tags, then any tags are used.
58 * Otherwise only annotated tags are used.
59 */
cc44c765 60 if (!prefixcmp(path, "refs/tags/")) {
30ffa603 61 if (object->type == OBJ_TAG) {
64deb858 62 prio = 2;
30ffa603
PH
63 if (pattern && fnmatch(pattern, path + 10, 0))
64 prio = 0;
65 } else
64deb858
JH
66 prio = 1;
67 }
68 else
69 prio = 0;
70
635d4134 71 if (!all) {
64deb858
JH
72 if (!prio)
73 return 0;
74 if (!tags && prio < 2)
635d4134 75 return 0;
635d4134 76 }
64deb858 77 add_to_known_names(all ? path + 5 : path + 10, commit, prio);
908e5310
LT
78 return 0;
79}
80
80dbae03 81struct possible_tag {
80dbae03 82 struct commit_name *name;
cf69fd49
SP
83 int depth;
84 int found_order;
8713ab30 85 unsigned flag_within;
80dbae03
SP
86};
87
cf69fd49
SP
88static int compare_pt(const void *a_, const void *b_)
89{
90 struct possible_tag *a = (struct possible_tag *)a_;
91 struct possible_tag *b = (struct possible_tag *)b_;
92 if (a->name->prio != b->name->prio)
93 return b->name->prio - a->name->prio;
94 if (a->depth != b->depth)
95 return a->depth - b->depth;
96 if (a->found_order != b->found_order)
97 return a->found_order - b->found_order;
98 return 0;
99}
100
1b600e65
SP
101static unsigned long finish_depth_computation(
102 struct commit_list **list,
103 struct possible_tag *best)
104{
105 unsigned long seen_commits = 0;
106 while (*list) {
107 struct commit *c = pop_commit(list);
108 struct commit_list *parents = c->parents;
109 seen_commits++;
110 if (c->object.flags & best->flag_within) {
111 struct commit_list *a = *list;
112 while (a) {
113 struct commit *i = a->item;
114 if (!(i->object.flags & best->flag_within))
115 break;
116 a = a->next;
117 }
118 if (!a)
119 break;
120 } else
121 best->depth++;
122 while (parents) {
123 struct commit *p = parents->item;
124 parse_commit(p);
125 if (!(p->object.flags & SEEN))
126 insert_by_date(p, list);
127 p->object.flags |= c->object.flags;
128 parents = parents->next;
129 }
130 }
131 return seen_commits;
132}
133
554fe20d 134static void describe(const char *arg, int last_one)
908e5310 135{
4c34a2c5 136 unsigned char sha1[20];
8713ab30 137 struct commit *cmit, *gave_up_on = NULL;
908e5310
LT
138 struct commit_list *list;
139 static int initialized = 0;
140 struct commit_name *n;
cf69fd49 141 struct possible_tag all_matches[MAX_TAGS];
8713ab30
SP
142 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
143 unsigned long seen_commits = 0;
908e5310 144
31fff305
DL
145 if (get_sha1(arg, sha1))
146 die("Not a valid object name %s", arg);
4c34a2c5
JH
147 cmit = lookup_commit_reference(sha1);
148 if (!cmit)
31fff305 149 die("%s is not a valid '%s' object", arg, commit_type);
4c34a2c5 150
908e5310
LT
151 if (!initialized) {
152 initialized = 1;
cb5d709f 153 for_each_ref(get_name, NULL);
908e5310
LT
154 }
155
e7eb5034 156 n = cmit->util;
908e5310
LT
157 if (n) {
158 printf("%s\n", n->path);
159 return;
160 }
161
8713ab30
SP
162 if (debug)
163 fprintf(stderr, "searching to describe %s\n", arg);
164
908e5310 165 list = NULL;
8713ab30 166 cmit->object.flags = SEEN;
908e5310
LT
167 commit_list_insert(cmit, &list);
168 while (list) {
80dbae03 169 struct commit *c = pop_commit(&list);
dccd0c2a 170 struct commit_list *parents = c->parents;
8713ab30 171 seen_commits++;
e7eb5034 172 n = c->util;
908e5310 173 if (n) {
8713ab30
SP
174 if (match_cnt < max_candidates) {
175 struct possible_tag *t = &all_matches[match_cnt++];
176 t->name = n;
177 t->depth = seen_commits - 1;
178 t->flag_within = 1u << match_cnt;
8a8169c0 179 t->found_order = match_cnt;
8713ab30
SP
180 c->object.flags |= t->flag_within;
181 if (n->prio == 2)
182 annotated_cnt++;
183 }
184 else {
185 gave_up_on = c;
186 break;
187 }
188 }
189 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
190 struct possible_tag *t = &all_matches[cur_match];
191 if (!(c->object.flags & t->flag_within))
192 t->depth++;
193 }
194 if (annotated_cnt && !list) {
195 if (debug)
196 fprintf(stderr, "finished search at %s\n",
197 sha1_to_hex(c->object.sha1));
198 break;
dccd0c2a
SP
199 }
200 while (parents) {
201 struct commit *p = parents->item;
202 parse_commit(p);
8713ab30 203 if (!(p->object.flags & SEEN))
dccd0c2a 204 insert_by_date(p, &list);
8713ab30 205 p->object.flags |= c->object.flags;
dccd0c2a 206 parents = parents->next;
80dbae03
SP
207 }
208 }
209
8713ab30 210 if (!match_cnt)
80dbae03
SP
211 die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
212
cf69fd49 213 qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
1b600e65
SP
214
215 if (gave_up_on) {
216 insert_by_date(gave_up_on, &list);
217 seen_commits--;
218 }
219 seen_commits += finish_depth_computation(&list, &all_matches[0]);
220 free_commit_list(list);
221
8713ab30
SP
222 if (debug) {
223 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
224 struct possible_tag *t = &all_matches[cur_match];
cf69fd49
SP
225 fprintf(stderr, " %-11s %8d %s\n",
226 prio_names[t->name->prio],
8713ab30
SP
227 t->depth, t->name->path);
228 }
229 fprintf(stderr, "traversed %lu commits\n", seen_commits);
230 if (gave_up_on) {
231 fprintf(stderr,
232 "more than %i tags found; listed %i most recent\n"
233 "gave up search at %s\n",
234 max_candidates, max_candidates,
235 sha1_to_hex(gave_up_on->object.sha1));
236 }
80dbae03 237 }
f127404c
AP
238 if (abbrev == 0)
239 printf("%s\n", all_matches[0].name->path );
240 else
237fb6ca
SP
241 printf("%s-%d-g%s\n", all_matches[0].name->path,
242 all_matches[0].depth,
243 find_unique_abbrev(cmit->object.sha1, abbrev));
80dbae03 244
8713ab30
SP
245 if (!last_one)
246 clear_commit_marks(cmit, -1);
908e5310
LT
247}
248
9a0eaf83 249int cmd_describe(int argc, const char **argv, const char *prefix)
908e5310 250{
23615708 251 int contains = 0;
166185be
PH
252 struct option options[] = {
253 OPT_BOOLEAN(0, "contains", &contains, "find the tag that comes after the commit"),
254 OPT_BOOLEAN(0, "debug", &debug, "debug search strategy on stderr"),
255 OPT_BOOLEAN(0, "all", &all, "use any ref in .git/refs"),
256 OPT_BOOLEAN(0, "tags", &tags, "use any tag in .git/refs/tags"),
257 OPT__ABBREV(&abbrev),
258 OPT_INTEGER(0, "candidates", &max_candidates,
30ffa603
PH
259 "consider <n> most recent tags (default: 10)"),
260 OPT_STRING(0, "match", &pattern, "pattern",
261 "only consider tags matching <pattern>"),
166185be
PH
262 OPT_END(),
263 };
908e5310 264
166185be
PH
265 argc = parse_options(argc, argv, options, describe_usage, 0);
266 if (max_candidates < 1)
267 max_candidates = 1;
268 else if (max_candidates > MAX_TAGS)
269 max_candidates = MAX_TAGS;
4c34a2c5 270
8c599c74 271 save_commit_buffer = 0;
8112894d 272
23615708 273 if (contains) {
a2cf9f44 274 const char **args = xmalloc((6 + argc) * sizeof(char*));
3f7701a4
NP
275 int i = 0;
276 args[i++] = "name-rev";
277 args[i++] = "--name-only";
a2cf9f44 278 args[i++] = "--no-undefined";
30ffa603 279 if (!all) {
3f7701a4 280 args[i++] = "--tags";
30ffa603
PH
281 if (pattern) {
282 char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
283 sprintf(s, "--refs=refs/tags/%s", pattern);
284 args[i++] = s;
285 }
286 }
3f7701a4
NP
287 memcpy(args + i, argv, argc * sizeof(char*));
288 args[i + argc] = NULL;
289 return cmd_name_rev(i + argc, args, prefix);
23615708
SP
290 }
291
166185be 292 if (argc == 0) {
fec9ebf1 293 describe("HEAD", 1);
166185be
PH
294 } else {
295 while (argc-- > 0) {
296 describe(*argv++, argc == 0);
fec9ebf1 297 }
166185be 298 }
908e5310
LT
299 return 0;
300}