2
#include "commit.h"
3
#include "tag.h"
4
#include "refs.h"
5
+#include "diff.h"
6
+#include "diffcore.h"
7
+#include "revision.h"
8
#include "builtin.h"
9
7
-#define SEEN (1u << 0)
8
-
10
static const char describe_usage[] =
11
"git-describe [--all] [--tags] [--abbrev=<n>] <committish>*";
12
17
18
static int names, allocs;
19
static struct commit_name {
19
- const struct commit *commit;
20
+ struct commit *commit;
21
int prio; /* annotated tag = 2, tag = 1, head = 0 */
22
char path[FLEX_ARRAY]; /* more */
23
} **name_array = NULL;
36
}
37
38
static void add_to_known_names(const char *path,
38
- const struct commit *commit,
39
+ struct commit *commit,
40
int prio)
41
{
42
int idx;
99
return (a_date > b_date) ? -1 : (a_date == b_date) ? 0 : 1;
100
}
101
102
+struct possible_tag {
103
+ struct possible_tag *next;
104
+ struct commit_name *name;
105
+ unsigned long depth;
106
+};
107
+
108
static void describe(const char *arg, int last_one)
109
{
110
unsigned char sha1[20];
112
struct commit_list *list;
113
static int initialized = 0;
114
struct commit_name *n;
115
+ struct possible_tag *all_matches, *min_match, *cur_match;
116
117
if (get_sha1(arg, sha1))
118
die("Not a valid object name %s", arg);
133
}
134
135
list = NULL;
136
+ all_matches = NULL;
137
+ cur_match = NULL;
138
commit_list_insert(cmit, &list);
139
while (list) {
130
- struct commit *c = pop_most_recent_commit(&list, SEEN);
140
+ struct commit *c = pop_commit(&list);
141
n = match(c);
142
if (n) {
133
- printf("%s-g%s\n", n->path,
134
- find_unique_abbrev(cmit->object.sha1, abbrev));
135
- if (!last_one)
136
- clear_commit_marks(cmit, SEEN);
137
- return;
143
+ struct possible_tag *p = xmalloc(sizeof(*p));
144
+ p->name = n;
145
+ p->next = NULL;
146
+ if (cur_match)
147
+ cur_match->next = p;
148
+ else
149
+ all_matches = p;
150
+ cur_match = p;
151
+ } else {
152
+ struct commit_list *parents = c->parents;
153
+ while (parents) {
154
+ struct commit *p = parents->item;
155
+ parse_commit(p);
156
+ if (!(p->object.flags & SEEN)) {
157
+ p->object.flags |= SEEN;
158
+ insert_by_date(p, &list);
159
+ }
160
+ parents = parents->next;
161
+ }
162
+ }
163
+ }
164
+
165
+ if (!all_matches)
166
+ die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
167
+
168
+ min_match = NULL;
169
+ for (cur_match = all_matches; cur_match; cur_match = cur_match->next) {
170
+ struct rev_info revs;
171
+ struct commit *tagged = cur_match->name->commit;
172
+
173
+ clear_commit_marks(cmit, -1);
174
+ init_revisions(&revs, NULL);
175
+ tagged->object.flags |= UNINTERESTING;
176
+ add_pending_object(&revs, &tagged->object, NULL);
177
+ add_pending_object(&revs, &cmit->object, NULL);
178
+
179
+ prepare_revision_walk(&revs);
180
+ cur_match->depth = 0;
181
+ while ((!min_match || cur_match->depth < min_match->depth)
182
+ && get_revision(&revs))
183
+ cur_match->depth++;
184
+ if (!min_match || cur_match->depth < min_match->depth)
185
+ min_match = cur_match;
186
+ }
187
+ printf("%s-g%s\n", min_match->name->path,
188
+ find_unique_abbrev(cmit->object.sha1, abbrev));
189
+
190
+ if (!last_one) {
191
+ for (cur_match = all_matches; cur_match; cur_match = min_match) {
192
+ min_match = cur_match->next;
193
+ free(cur_match);
194
}
195
+ clear_commit_marks(cmit, SEEN);
196
}
140
- die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
197
}
198
199
int cmd_describe(int argc, const char **argv, const char *prefix)