Merge branch 'jc/apply'
[git/gitweb.git] / describe.c
blob8a9cd5d52c555330c512b977179ab030ccd28861
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 static int abbrev = DEFAULT_ABBREV;
16 static int names = 0, allocs = 0;
17 static struct commit_name {
18 const struct commit *commit;
19 int prio; /* annotated tag = 2, tag = 1, head = 0 */
20 char path[FLEX_ARRAY]; /* more */
21 } **name_array = NULL;
23 static struct commit_name *match(struct commit *cmit)
25 int i = names;
26 struct commit_name **p = name_array;
28 while (i-- > 0) {
29 struct commit_name *n = *p++;
30 if (n->commit == cmit)
31 return n;
33 return NULL;
36 static void add_to_known_names(const char *path,
37 const struct commit *commit,
38 int prio)
40 int idx;
41 int len = strlen(path)+1;
42 struct commit_name *name = xmalloc(sizeof(struct commit_name) + len);
44 name->commit = commit;
45 name->prio = prio;
46 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));
52 name_array[idx] = name;
53 names = ++idx;
56 static int get_name(const char *path, const unsigned char *sha1)
58 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
59 struct object *object;
60 int prio;
62 if (!commit)
63 return 0;
64 object = parse_object(sha1);
65 /* If --all, then any refs are used.
66 * If --tags, then any tags are used.
67 * Otherwise only annotated tags are used.
69 if (!strncmp(path, "refs/tags/", 10)) {
70 if (object->type == tag_type)
71 prio = 2;
72 else
73 prio = 1;
75 else
76 prio = 0;
78 if (!all) {
79 if (!prio)
80 return 0;
81 if (!tags && prio < 2)
82 return 0;
84 add_to_known_names(all ? path + 5 : path + 10, commit, prio);
85 return 0;
88 static int compare_names(const void *_a, const void *_b)
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;
95 if (a->prio != b->prio)
96 return b->prio - a->prio;
97 return (a_date > b_date) ? -1 : (a_date == b_date) ? 0 : 1;
100 static void describe(char *arg, int last_one)
102 unsigned char sha1[20];
103 struct commit *cmit;
104 struct commit_list *list;
105 static int initialized = 0;
106 struct commit_name *n;
108 if (get_sha1(arg, sha1))
109 die("Not a valid object name %s", arg);
110 cmit = lookup_commit_reference(sha1);
111 if (!cmit)
112 die("%s is not a valid '%s' object", arg, commit_type);
114 if (!initialized) {
115 initialized = 1;
116 for_each_ref(get_name);
117 qsort(name_array, names, sizeof(*name_array), compare_names);
120 n = match(cmit);
121 if (n) {
122 printf("%s\n", n->path);
123 return;
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) {
132 printf("%s-g%s\n", n->path,
133 find_unique_abbrev(cmit->object.sha1, abbrev));
134 if (!last_one)
135 clear_commit_marks(cmit, SEEN);
136 return;
139 die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
142 int main(int argc, char **argv)
144 int i;
146 for (i = 1; i < argc; i++) {
147 const char *arg = argv[i];
149 if (*arg != '-')
150 break;
151 else if (!strcmp(arg, "--all"))
152 all = 1;
153 else if (!strcmp(arg, "--tags"))
154 tags = 1;
155 else if (!strncmp(arg, "--abbrev=", 9)) {
156 abbrev = strtoul(arg + 9, NULL, 10);
157 if (abbrev < MINIMUM_ABBREV || 40 <= abbrev)
158 abbrev = DEFAULT_ABBREV;
160 else
161 usage(describe_usage);
164 if (i == argc)
165 describe("HEAD", 1);
166 else
167 while (i < argc) {
168 describe(argv[i], (i == argc - 1));
169 i++;
172 return 0;