blob: f4029ee74e80305e9ec9e5793d2e12c07096f45a [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
David Rientjes96f1e582006-08-15 10:23:48 -070011static int all; /* Default to annotated tags only */
12static int tags; /* But allow any tags if --tags is specified */
Junio C Hamano2d9e7c92005-12-27 14:40:17 -080013
Junio C Hamano2d9e7c92005-12-27 14:40:17 -080014static int abbrev = DEFAULT_ABBREV;
Linus Torvalds908e5312005-12-24 13:50:45 -080015
David Rientjes96f1e582006-08-15 10:23:48 -070016static int names, allocs;
Linus Torvalds908e5312005-12-24 13:50:45 -080017static 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;
Jonas Fonsecaf7122262006-08-25 02:48:04 +020045 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
Junio C Hamano8da19772006-09-20 22:02:01 -070056static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
Linus Torvalds908e5312005-12-24 13:50:45 -080057{
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)) {
Linus Torvalds19746322006-07-11 20:45:31 -070070 if (object->type == OBJ_TAG)
Junio C Hamano64deb852005-12-27 16:09:37 -080071 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
Timo Hirvonen554fe202006-06-28 12:04:39 +0300100static void describe(const 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
Dmitry V. Levin31fff302006-05-09 01:43:38 +0400108 if (get_sha1(arg, sha1))
109 die("Not a valid object name %s", arg);
Junio C Hamano4c34a2c2006-01-11 13:57:42 -0800110 cmit = lookup_commit_reference(sha1);
111 if (!cmit)
Dmitry V. Levin31fff302006-05-09 01:43:38 +0400112 die("%s is not a valid '%s' object", arg, commit_type);
Junio C Hamano4c34a2c2006-01-11 13:57:42 -0800113
Linus Torvalds908e5312005-12-24 13:50:45 -0800114 if (!initialized) {
115 initialized = 1;
Junio C Hamanocb5d7092006-09-20 21:47:42 -0700116 for_each_ref(get_name, NULL);
Linus Torvalds908e5312005-12-24 13:50:45 -0800117 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);
Jonas Fonsecaf7122262006-08-25 02:48:04 +0200157 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
Dmitry V. Levin81128942006-09-14 05:03:59 +0400164 setup_git_directory();
165
Dmitry V. Levin5b6df8e2006-09-14 05:04:09 +0400166 if (argc <= i)
Junio C Hamanofec9ebf2006-01-15 22:25:35 -0800167 describe("HEAD", 1);
Junio C Hamano4c34a2c2006-01-11 13:57:42 -0800168 else
Junio C Hamanofec9ebf2006-01-15 22:25:35 -0800169 while (i < argc) {
170 describe(argv[i], (i == argc - 1));
171 i++;
172 }
Junio C Hamano4c34a2c2006-01-11 13:57:42 -0800173
Linus Torvalds908e5312005-12-24 13:50:45 -0800174 return 0;
175}