2
#include "commit.h"
3
#include "tag.h"
4
#include "refs.h"
5
-#include "diff.h"
6
-#include "diffcore.h"
7
-#include "revision.h"
5
#include "builtin.h"
6
7
+#define SEEN (1u<<0)
8
+#define MAX_TAGS (FLAG_BITS - 1)
9
+
10
static const char describe_usage[] =
11
"git-describe [--all] [--tags] [--abbrev=<n>] <committish>*";
12
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;
18
19
static unsigned int names[256], allocs[256];
20
static struct commit_name {
117
}
118
119
struct possible_tag {
118
- struct possible_tag *next;
120
struct commit_name *name;
121
unsigned long depth;
122
+ unsigned flag_within;
123
};
124
125
static void describe(const char *arg, int last_one)
126
{
127
unsigned char sha1[20];
126
- struct commit *cmit;
128
+ struct commit *cmit, *gave_up_on = NULL;
129
struct commit_list *list;
130
static int initialized = 0;
131
struct commit_name *n;
130
- struct possible_tag *all_matches, *min_match, *cur_match;
132
+ struct possible_tag all_matches[MAX_TAGS], *min_match;
133
+ unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
134
+ unsigned long seen_commits = 0;
135
136
if (get_sha1(arg, sha1))
137
die("Not a valid object name %s", arg);
154
return;
155
}
156
157
+ if (debug)
158
+ fprintf(stderr, "searching to describe %s\n", arg);
159
+
160
list = NULL;
154
- all_matches = NULL;
155
- cur_match = NULL;
161
+ cmit->object.flags = SEEN;
162
commit_list_insert(cmit, &list);
163
while (list) {
164
struct commit *c = pop_commit(&list);
165
struct commit_list *parents = c->parents;
166
+ seen_commits++;
167
n = match(c);
168
if (n) {
162
- struct possible_tag *p = xmalloc(sizeof(*p));
163
- p->name = n;
164
- p->next = NULL;
165
- if (cur_match)
166
- cur_match->next = p;
167
- else
168
- all_matches = p;
169
- cur_match = p;
170
- if (n->prio == 2)
171
- continue;
169
+ if (match_cnt < max_candidates) {
170
+ struct possible_tag *t = &all_matches[match_cnt++];
171
+ t->name = n;
172
+ t->depth = seen_commits - 1;
173
+ t->flag_within = 1u << match_cnt;
174
+ c->object.flags |= t->flag_within;
175
+ if (n->prio == 2)
176
+ annotated_cnt++;
177
+ }
178
+ else {
179
+ gave_up_on = c;
180
+ break;
181
+ }
182
+ }
183
+ for (cur_match = 0; cur_match < match_cnt; cur_match++) {
184
+ struct possible_tag *t = &all_matches[cur_match];
185
+ if (!(c->object.flags & t->flag_within))
186
+ t->depth++;
187
+ }
188
+ if (annotated_cnt && !list) {
189
+ if (debug)
190
+ fprintf(stderr, "finished search at %s\n",
191
+ sha1_to_hex(c->object.sha1));
192
+ break;
193
}
194
while (parents) {
195
struct commit *p = parents->item;
196
parse_commit(p);
176
- if (!(p->object.flags & SEEN)) {
177
- p->object.flags |= SEEN;
197
+ if (!(p->object.flags & SEEN))
198
insert_by_date(p, &list);
179
- }
199
+ p->object.flags |= c->object.flags;
200
parents = parents->next;
201
}
202
}
203
+ free_commit_list(list);
204
184
- if (!all_matches)
205
+ if (!match_cnt)
206
die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
207
187
- min_match = NULL;
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;
191
-
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);
197
-
198
- prepare_revision_walk(&revs);
199
- cur_match->depth = 0;
200
- while ((!min_match || cur_match->depth < min_match->depth)
201
- && get_revision(&revs))
202
- cur_match->depth++;
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
+ min_match = &all_matches[0];
209
+ for (cur_match = 1; cur_match < match_cnt; cur_match++) {
210
+ struct possible_tag *t = &all_matches[cur_match];
211
+ if (t->depth < min_match->depth
212
+ && t->name->prio >= min_match->name->prio)
213
+ min_match = t;
214
+ }
215
+ if (debug) {
216
+ for (cur_match = 0; cur_match < match_cnt; cur_match++) {
217
+ struct possible_tag *t = &all_matches[cur_match];
218
+ fprintf(stderr, " %c %8lu %s\n",
219
+ min_match == t ? '*' : ' ',
220
+ t->depth, t->name->path);
221
+ }
222
+ fprintf(stderr, "traversed %lu commits\n", seen_commits);
223
+ if (gave_up_on) {
224
+ fprintf(stderr,
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));
229
+ }
230
}
231
printf("%s-g%s\n", min_match->name->path,
232
find_unique_abbrev(cmit->object.sha1, abbrev));
233
211
- if (!last_one) {
212
- for (cur_match = all_matches; cur_match; cur_match = min_match) {
213
- min_match = cur_match->next;
214
- free(cur_match);
215
- }
216
- clear_commit_marks(cmit, SEEN);
217
- }
234
+ if (!last_one)
235
+ clear_commit_marks(cmit, -1);
236
}
237
238
int cmd_describe(int argc, const char **argv, const char *prefix)
244
245
if (*arg != '-')
246
break;
247
+ else if (!strcmp(arg, "--debug"))
248
+ debug = 1;
249
else if (!strcmp(arg, "--all"))
250
all = 1;
251
else if (!strcmp(arg, "--tags"))
255
if (abbrev < MINIMUM_ABBREV || 40 < abbrev)
256
abbrev = DEFAULT_ABBREV;
257
}
258
+ else if (!strncmp(arg, "--candidates=", 13)) {
259
+ max_candidates = strtoul(arg + 13, NULL, 10);
260
+ if (max_candidates < 1)
261
+ max_candidates = 1;
262
+ else if (max_candidates > MAX_TAGS)
263
+ max_candidates = MAX_TAGS;
264
+ }
265
else
266
usage(describe_usage);
267
}