@matheusbsbs / matheus-bagliano / commits / cf69fd49ec
Fork 0

Correct priority of lightweight tags in git-describe.

We really want to always favor an annotated tag over a lightweight tag when describing a commit. Unfortunately git-describe wasn't doing this as it was favoring the depth attribute of a possible_tag over the priority. Now priority is the highest sort and we only consider a lightweight tag if no annotated tags were identified. Rather than searching for the minimum tag using a simple loop we now sort them using a stable sort algorithm, this way the possible tags display in order if --debug gets used. The stable sort helps to preseve the inherit topology/date order that we obtain during our search loop. This fix allows the tests in t6120-describe.sh to pass. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Shawn O. Pearce committed Jan 14, 2007 at 04:37 UTC cf69fd49ec815780080dc6a4ee237eee5ffe8745
1 file changed +25 -12
builtin-describe.c
+25 -12
@@ -22,6 +22,9 @@ static struct commit_name {
22 int prio; /* annotated tag = 2, tag = 1, head = 0 */
23 char path[FLEX_ARRAY]; /* more */
24 } **name_array[256];
25 +static const char *prio_names[] = {
26 + "head", "lightweight", "annotated",
27 +};
28
29 static struct commit_name *match(struct commit *cmit)
30 {
@@ -118,10 +121,24 @@ static int compare_names(const void *_a, const void *_b)
121
122 struct possible_tag {
123 struct commit_name *name;
121 - unsigned long depth;
124 + int depth;
125 + int found_order;
126 unsigned flag_within;
127 };
128
129 +static int compare_pt(const void *a_, const void *b_)
130 +{
131 + struct possible_tag *a = (struct possible_tag *)a_;
132 + struct possible_tag *b = (struct possible_tag *)b_;
133 + if (a->name->prio != b->name->prio)
134 + return b->name->prio - a->name->prio;
135 + if (a->depth != b->depth)
136 + return a->depth - b->depth;
137 + if (a->found_order != b->found_order)
138 + return a->found_order - b->found_order;
139 + return 0;
140 +}
141 +
142 static void describe(const char *arg, int last_one)
143 {
144 unsigned char sha1[20];
@@ -129,9 +146,10 @@ static void describe(const char *arg, int last_one)
146 struct commit_list *list;
147 static int initialized = 0;
148 struct commit_name *n;
132 - struct possible_tag all_matches[MAX_TAGS], *min_match;
149 + struct possible_tag all_matches[MAX_TAGS];
150 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
151 unsigned long seen_commits = 0;
152 + int found = 0;
153
154 if (get_sha1(arg, sha1))
155 die("Not a valid object name %s", arg);
@@ -171,6 +189,7 @@ static void describe(const char *arg, int last_one)
189 t->name = n;
190 t->depth = seen_commits - 1;
191 t->flag_within = 1u << match_cnt;
192 + t->found_order = found++;
193 c->object.flags |= t->flag_within;
194 if (n->prio == 2)
195 annotated_cnt++;
@@ -205,18 +224,12 @@ static void describe(const char *arg, int last_one)
224 if (!match_cnt)
225 die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
226
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 - }
227 + qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
228 if (debug) {
229 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
230 struct possible_tag *t = &all_matches[cur_match];
218 - fprintf(stderr, " %c %8lu %s\n",
219 - min_match == t ? '*' : ' ',
231 + fprintf(stderr, " %-11s %8d %s\n",
232 + prio_names[t->name->prio],
233 t->depth, t->name->path);
234 }
235 fprintf(stderr, "traversed %lu commits\n", seen_commits);
@@ -228,7 +241,7 @@ static void describe(const char *arg, int last_one)
241 sha1_to_hex(gave_up_on->object.sha1));
242 }
243 }
231 - printf("%s-g%s\n", min_match->name->path,
244 + printf("%s-g%s\n", all_matches[0].name->path,
245 find_unique_abbrev(cmit->object.sha1, abbrev));
246
247 if (!last_one)