blob: ff65742615cb6862d3699e4f301d671a7eadc2e2 [file] [log] [blame]
Linus Torvalds908e5312005-12-24 13:50:45 -08001#include "cache.h"
2#include "commit.h"
Junio C Hamano635d4132005-12-27 14:36:49 -08003#include "tag.h"
Linus Torvalds908e5312005-12-24 13:50:45 -08004#include "refs.h"
5
6#define SEEN (1u << 0)
7
Junio C Hamano2d9e7c92005-12-27 14:40:17 -08008static const char describe_usage[] =
9"git-describe [--all] [--tags] [--abbrev=<n>] <committish>*";
Linus Torvalds908e5312005-12-24 13:50:45 -080010
Junio C Hamano635d4132005-12-27 14:36:49 -080011static int all = 0; /* Default to annotated tags only */
Junio C Hamano2d9e7c92005-12-27 14:40:17 -080012static int tags = 0; /* But allow any tags if --tags is specified */
13
Junio C Hamano2d9e7c92005-12-27 14:40:17 -080014static int abbrev = DEFAULT_ABBREV;
Linus Torvalds908e5312005-12-24 13:50:45 -080015
16static int names = 0, allocs = 0;
17static struct commit_name {
18 const struct commit *commit;
Junio C Hamano64deb852005-12-27 16:09:37 -080019 int prio; /* annotated tag = 2, tag = 1, head = 0 */
Junio C Hamano5a2282d2006-01-08 14:22:19 -080020 char path[FLEX_ARRAY]; /* more */
Linus Torvalds908e5312005-12-24 13:50:45 -080021} **name_array = NULL;
22
23static struct commit_name *match(struct commit *cmit)
24{
25 int i = names;
26 struct commit_name **p = name_array;
27
28 while (i-- > 0) {
29 struct commit_name *n = *p++;
30 if (n->commit == cmit)
31 return n;
32 }
33 return NULL;
34}
35
Junio C Hamano64deb852005-12-27 16:09:37 -080036static void add_to_known_names(const char *path,
37 const struct commit *commit,
38 int prio)
Linus Torvalds908e5312005-12-24 13:50:45 -080039{
40 int idx;
41 int len = strlen(path)+1;
42 struct commit_name *name = xmalloc(sizeof(struct commit_name) + len);
43
44 name->commit = commit;
Junio C Hamano64deb852005-12-27 16:09:37 -080045 name->prio = prio;
Linus Torvalds908e5312005-12-24 13:50:45 -080046 memcpy(name->path, path, len);
47 idx = names;
48 if (idx >= allocs) {
49 allocs = (idx + 50) * 3 / 2;
50 name_array = xrealloc(name_array, allocs*sizeof(*name_array));
51 }
52 name_array[idx] = name;
53 names = ++idx;
54}
55
56static int get_name(const char *path, const unsigned char *sha1)
57{
58 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
Junio C Hamano64deb852005-12-27 16:09:37 -080059 struct object *object;
60 int prio;
61
Linus Torvalds908e5312005-12-24 13:50:45 -080062 if (!commit)
63 return 0;
Junio C Hamano64deb852005-12-27 16:09:37 -080064 object = parse_object(sha1);
Junio C Hamano2d9e7c92005-12-27 14:40:17 -080065 /* If --all, then any refs are used.
66 * If --tags, then any tags are used.
67 * Otherwise only annotated tags are used.
68 */
Junio C Hamano64deb852005-12-27 16:09:37 -080069 if (!strncmp(path, "refs/tags/", 10)) {
70 if (object->type == tag_type)
71 prio = 2;
72 else
73 prio = 1;
Junio C Hamano635d4132005-12-27 14:36:49 -080074 }
Junio C Hamano64deb852005-12-27 16:09:37 -080075 else
76 prio = 0;
77
78 if (!all) {
79 if (!prio)
80 return 0;
81 if (!tags && prio < 2)
82 return 0;
83 }
84 add_to_known_names(all ? path + 5 : path + 10, commit, prio);
Linus Torvalds908e5312005-12-24 13:50:45 -080085 return 0;
86}
87
88static int compare_names(const void *_a, const void *_b)
89{
90 struct commit_name *a = *(struct commit_name **)_a;
91 struct commit_name *b = *(struct commit_name **)_b;
92 unsigned long a_date = a->commit->date;
93 unsigned long b_date = b->commit->date;
Junio C Hamano64deb852005-12-27 16:09:37 -080094
95 if (a->prio != b->prio)
96 return b->prio - a->prio;
Linus Torvalds908e5312005-12-24 13:50:45 -080097 return (a_date > b_date) ? -1 : (a_date == b_date) ? 0 : 1;
98}
99
Junio C Hamanofec9ebf2006-01-15 22:25:35 -0800100static void describe(char *arg, int last_one)
Linus Torvalds908e5312005-12-24 13:50:45 -0800101{
Junio C Hamano4c34a2c2006-01-11 13:57:42 -0800102 unsigned char sha1[20];
103 struct commit *cmit;
Linus Torvalds908e5312005-12-24 13:50:45 -0800104 struct commit_list *list;
105 static int initialized = 0;
106 struct commit_name *n;
107
Junio C Hamano4c34a2c2006-01-11 13:57:42 -0800108 if (get_sha1(arg, sha1) < 0)
109 usage(describe_usage);
110 cmit = lookup_commit_reference(sha1);
111 if (!cmit)
112 usage(describe_usage);
113
Linus Torvalds908e5312005-12-24 13:50:45 -0800114 if (!initialized) {
115 initialized = 1;
116 for_each_ref(get_name);
117 qsort(name_array, names, sizeof(*name_array), compare_names);
118 }
119
120 n = match(cmit);
121 if (n) {
122 printf("%s\n", n->path);
123 return;
124 }
125
126 list = NULL;
127 commit_list_insert(cmit, &list);
128 while (list) {
129 struct commit *c = pop_most_recent_commit(&list, SEEN);
130 n = match(c);
131 if (n) {
Junio C Hamano4cdf78b2005-12-27 14:49:22 -0800132 printf("%s-g%s\n", n->path,
Junio C Hamano2d9e7c92005-12-27 14:40:17 -0800133 find_unique_abbrev(cmit->object.sha1, abbrev));
Junio C Hamano181dc772006-01-15 22:15:37 -0800134 if (!last_one)
135 clear_commit_marks(cmit, SEEN);
Junio C Hamano8c23b6f2006-01-11 13:41:25 -0800136 return;
Linus Torvalds908e5312005-12-24 13:50:45 -0800137 }
138 }
Junio C Hamano8c23b6f2006-01-11 13:41:25 -0800139 die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
Linus Torvalds908e5312005-12-24 13:50:45 -0800140}
141
142int main(int argc, char **argv)
143{
144 int i;
145
146 for (i = 1; i < argc; i++) {
147 const char *arg = argv[i];
Linus Torvalds908e5312005-12-24 13:50:45 -0800148
Junio C Hamano4c34a2c2006-01-11 13:57:42 -0800149 if (*arg != '-')
150 break;
151 else if (!strcmp(arg, "--all"))
Linus Torvalds908e5312005-12-24 13:50:45 -0800152 all = 1;
Junio C Hamano4c34a2c2006-01-11 13:57:42 -0800153 else if (!strcmp(arg, "--tags"))
Junio C Hamano2d9e7c92005-12-27 14:40:17 -0800154 tags = 1;
Junio C Hamano4c34a2c2006-01-11 13:57:42 -0800155 else if (!strncmp(arg, "--abbrev=", 9)) {
Junio C Hamano2d9e7c92005-12-27 14:40:17 -0800156 abbrev = strtoul(arg + 9, NULL, 10);
Junio C Hamano46a6c262006-01-25 01:03:18 -0800157 if (abbrev < MINIMUM_ABBREV || 40 <= abbrev)
Junio C Hamano2d9e7c92005-12-27 14:40:17 -0800158 abbrev = DEFAULT_ABBREV;
Junio C Hamano2d9e7c92005-12-27 14:40:17 -0800159 }
Junio C Hamano4c34a2c2006-01-11 13:57:42 -0800160 else
Linus Torvalds908e5312005-12-24 13:50:45 -0800161 usage(describe_usage);
Linus Torvalds908e5312005-12-24 13:50:45 -0800162 }
Junio C Hamano4c34a2c2006-01-11 13:57:42 -0800163
164 if (i == argc)
Junio C Hamanofec9ebf2006-01-15 22:25:35 -0800165 describe("HEAD", 1);
Junio C Hamano4c34a2c2006-01-11 13:57:42 -0800166 else
Junio C Hamanofec9ebf2006-01-15 22:25:35 -0800167 while (i < argc) {
168 describe(argv[i], (i == argc - 1));
169 i++;
170 }
Junio C Hamano4c34a2c2006-01-11 13:57:42 -0800171
Linus Torvalds908e5312005-12-24 13:50:45 -0800172 return 0;
173}