@matheusbsbs / matheus-bagliano / commits / 64deb858b0
Fork 0

git-describe: still prefer annotated tag under --all and --tags

Even though --all and --tags can be used to include non annotated tags in the reference point candidates, prefer to use annotated tags if there are more than one refs that name the same commit. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Dec 27, 2005 at 16:09 UTC 64deb858b0c71f485d186e91cd00cf8b6ec64087
1 file changed +25 -9
describe.c
+25 -9
@@ -17,6 +17,7 @@ static int abbrev = DEFAULT_ABBREV;
17 static int names = 0, allocs = 0;
18 static struct commit_name {
19 const struct commit *commit;
20 + int prio; /* annotated tag = 2, tag = 1, head = 0 */
21 char path[];
22 } **name_array = NULL;
23
@@ -33,13 +34,16 @@ static struct commit_name *match(struct commit *cmit)
34 return NULL;
35 }
36
36 -static void add_to_known_names(const char *path, const struct commit *commit)
37 +static void add_to_known_names(const char *path,
38 + const struct commit *commit,
39 + int prio)
40 {
41 int idx;
42 int len = strlen(path)+1;
43 struct commit_name *name = xmalloc(sizeof(struct commit_name) + len);
44
45 name->commit = commit;
46 + name->prio = prio;
47 memcpy(name->path, path, len);
48 idx = names;
49 if (idx >= allocs) {
@@ -53,23 +57,32 @@ static void add_to_known_names(const char *path, const struct commit *commit)
57 static int get_name(const char *path, const unsigned char *sha1)
58 {
59 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
60 + struct object *object;
61 + int prio;
62 +
63 if (!commit)
64 return 0;
65 + object = parse_object(sha1);
66 /* If --all, then any refs are used.
67 * If --tags, then any tags are used.
68 * Otherwise only annotated tags are used.
69 */
70 + if (!strncmp(path, "refs/tags/", 10)) {
71 + if (object->type == tag_type)
72 + prio = 2;
73 + else
74 + prio = 1;
75 + }
76 + else
77 + prio = 0;
78 +
79 if (!all) {
63 - if (strncmp(path, "refs/tags/", 10))
80 + if (!prio)
81 + return 0;
82 + if (!tags && prio < 2)
83 return 0;
65 - if (!tags) {
66 - struct object *object;
67 - object = parse_object(sha1);
68 - if (object->type != tag_type)
69 - return 0;
70 - }
84 }
72 - add_to_known_names(all ? path : path + 10, commit);
85 + add_to_known_names(all ? path + 5 : path + 10, commit, prio);
86 return 0;
87 }
88
@@ -79,6 +92,9 @@ static int compare_names(const void *_a, const void *_b)
92 struct commit_name *b = *(struct commit_name **)_b;
93 unsigned long a_date = a->commit->date;
94 unsigned long b_date = b->commit->date;
95 +
96 + if (a->prio != b->prio)
97 + return b->prio - a->prio;
98 return (a_date > b_date) ? -1 : (a_date == b_date) ? 0 : 1;
99 }
100