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