git-describe: default to HEAD
[tgit.git] / describe.c
blob1e764026b1554e03eacb0915efbf9b475a6e9c84
1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "refs.h"
6 #define SEEN (1u << 0)
8 static const char describe_usage[] =
9 "git-describe [--all] [--tags] [--abbrev=<n>] <committish>*";
11 static int all = 0; /* Default to annotated tags only */
12 static int tags = 0; /* But allow any tags if --tags is specified */
14 #define DEFAULT_ABBREV 8 /* maybe too many */
15 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[FLEX_ARRAY]; /* more */
22 } **name_array = NULL;
24 static struct commit_name *match(struct commit *cmit)
26 int i = names;
27 struct commit_name **p = name_array;
29 while (i-- > 0) {
30 struct commit_name *n = *p++;
31 if (n->commit == cmit)
32 return n;
34 return NULL;
37 static void add_to_known_names(const char *path,
38 const struct commit *commit,
39 int prio)
41 int idx;
42 int len = strlen(path)+1;
43 struct commit_name *name = xmalloc(sizeof(struct commit_name) + len);
45 name->commit = commit;
46 name->prio = prio;
47 memcpy(name->path, path, len);
48 idx = names;
49 if (idx >= allocs) {
50 allocs = (idx + 50) * 3 / 2;
51 name_array = xrealloc(name_array, allocs*sizeof(*name_array));
53 name_array[idx] = name;
54 names = ++idx;
57 static int get_name(const char *path, const unsigned char *sha1)
59 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
60 struct object *object;
61 int prio;
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.
70 if (!strncmp(path, "refs/tags/", 10)) {
71 if (object->type == tag_type)
72 prio = 2;
73 else
74 prio = 1;
76 else
77 prio = 0;
79 if (!all) {
80 if (!prio)
81 return 0;
82 if (!tags && prio < 2)
83 return 0;
85 add_to_known_names(all ? path + 5 : path + 10, commit, prio);
86 return 0;
89 static int compare_names(const void *_a, const void *_b)
91 struct commit_name *a = *(struct commit_name **)_a;
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;
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;
101 static void describe(char *arg)
103 unsigned char sha1[20];
104 struct commit *cmit;
105 struct commit_list *list;
106 static int initialized = 0;
107 struct commit_name *n;
109 if (get_sha1(arg, sha1) < 0)
110 usage(describe_usage);
111 cmit = lookup_commit_reference(sha1);
112 if (!cmit)
113 usage(describe_usage);
115 if (!initialized) {
116 initialized = 1;
117 for_each_ref(get_name);
118 qsort(name_array, names, sizeof(*name_array), compare_names);
121 n = match(cmit);
122 if (n) {
123 printf("%s\n", n->path);
124 return;
127 list = NULL;
128 commit_list_insert(cmit, &list);
129 while (list) {
130 struct commit *c = pop_most_recent_commit(&list, SEEN);
131 n = match(c);
132 if (n) {
133 printf("%s-g%s\n", n->path,
134 find_unique_abbrev(cmit->object.sha1, abbrev));
135 break;
138 clear_commit_marks(cmit, SEEN);
141 int main(int argc, char **argv)
143 int i;
145 for (i = 1; i < argc; i++) {
146 const char *arg = argv[i];
148 if (*arg != '-')
149 break;
150 else if (!strcmp(arg, "--all"))
151 all = 1;
152 else if (!strcmp(arg, "--tags"))
153 tags = 1;
154 else if (!strncmp(arg, "--abbrev=", 9)) {
155 abbrev = strtoul(arg + 9, NULL, 10);
156 if (abbrev < 4 || 40 <= abbrev)
157 abbrev = DEFAULT_ABBREV;
159 else
160 usage(describe_usage);
163 if (i == argc)
164 describe("HEAD");
165 else
166 while (i < argc)
167 describe(argv[i++]);
169 return 0;