]> git.ipfire.org Git - thirdparty/git.git/blob - describe.c
git-status: use ls-files --others --directory for untracked list.
[thirdparty/git.git] / describe.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "refs.h"
5
6 #define SEEN (1u << 0)
7
8 static const char describe_usage[] =
9 "git-describe [--all] [--tags] [--abbrev=<n>] <committish>*";
10
11 static int all = 0; /* Default to annotated tags only */
12 static int tags = 0; /* But allow any tags if --tags is specified */
13
14 #define DEFAULT_ABBREV 8 /* maybe too many */
15 static int abbrev = DEFAULT_ABBREV;
16
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
24 static struct commit_name *match(struct commit *cmit)
25 {
26 int i = names;
27 struct commit_name **p = name_array;
28
29 while (i-- > 0) {
30 struct commit_name *n = *p++;
31 if (n->commit == cmit)
32 return n;
33 }
34 return NULL;
35 }
36
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) {
50 allocs = (idx + 50) * 3 / 2;
51 name_array = xrealloc(name_array, allocs*sizeof(*name_array));
52 }
53 name_array[idx] = name;
54 names = ++idx;
55 }
56
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) {
80 if (!prio)
81 return 0;
82 if (!tags && prio < 2)
83 return 0;
84 }
85 add_to_known_names(all ? path + 5 : path + 10, commit, prio);
86 return 0;
87 }
88
89 static int compare_names(const void *_a, const void *_b)
90 {
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;
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
101 static void describe(struct commit *cmit)
102 {
103 struct commit_list *list;
104 static int initialized = 0;
105 struct commit_name *n;
106
107 if (!initialized) {
108 initialized = 1;
109 for_each_ref(get_name);
110 qsort(name_array, names, sizeof(*name_array), compare_names);
111 }
112
113 n = match(cmit);
114 if (n) {
115 printf("%s\n", n->path);
116 return;
117 }
118
119 list = NULL;
120 commit_list_insert(cmit, &list);
121 while (list) {
122 struct commit *c = pop_most_recent_commit(&list, SEEN);
123 n = match(c);
124 if (n) {
125 printf("%s-g%s\n", n->path,
126 find_unique_abbrev(cmit->object.sha1, abbrev));
127 return;
128 }
129 }
130 }
131
132 int main(int argc, char **argv)
133 {
134 int i;
135
136 for (i = 1; i < argc; i++) {
137 const char *arg = argv[i];
138 unsigned char sha1[20];
139 struct commit *cmit;
140
141 if (!strcmp(arg, "--all")) {
142 all = 1;
143 continue;
144 }
145 if (!strcmp(arg, "--tags")) {
146 tags = 1;
147 continue;
148 }
149 if (!strncmp(arg, "--abbrev=", 9)) {
150 abbrev = strtoul(arg + 9, NULL, 10);
151 if (abbrev < 4 || 40 <= abbrev)
152 abbrev = DEFAULT_ABBREV;
153 continue;
154 }
155 if (get_sha1(arg, sha1) < 0)
156 usage(describe_usage);
157 cmit = lookup_commit_reference(sha1);
158 if (!cmit)
159 usage(describe_usage);
160 describe(cmit);
161 }
162 return 0;
163 }