]>
Commit | Line | Data |
---|---|---|
908e5310 LT |
1 | #include "cache.h" |
2 | #include "commit.h" | |
635d4134 | 3 | #include "tag.h" |
908e5310 LT |
4 | #include "refs.h" |
5 | ||
6 | #define SEEN (1u << 0) | |
7 | ||
2d9e7c9f JH |
8 | static const char describe_usage[] = |
9 | "git-describe [--all] [--tags] [--abbrev=<n>] <committish>*"; | |
908e5310 | 10 | |
96f1e58f DR |
11 | static int all; /* Default to annotated tags only */ |
12 | static int tags; /* But allow any tags if --tags is specified */ | |
2d9e7c9f | 13 | |
2d9e7c9f | 14 | static int abbrev = DEFAULT_ABBREV; |
908e5310 | 15 | |
96f1e58f | 16 | static int names, allocs; |
908e5310 LT |
17 | static struct commit_name { |
18 | const struct commit *commit; | |
64deb858 | 19 | int prio; /* annotated tag = 2, tag = 1, head = 0 */ |
5a2282de | 20 | char path[FLEX_ARRAY]; /* more */ |
908e5310 LT |
21 | } **name_array = NULL; |
22 | ||
23 | static 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 | ||
64deb858 JH |
36 | static void add_to_known_names(const char *path, |
37 | const struct commit *commit, | |
38 | int prio) | |
908e5310 LT |
39 | { |
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; | |
f7122265 | 45 | name->prio = prio; |
908e5310 LT |
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)); | |
51 | } | |
52 | name_array[idx] = name; | |
53 | names = ++idx; | |
54 | } | |
55 | ||
8da19775 | 56 | static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data) |
908e5310 LT |
57 | { |
58 | struct commit *commit = lookup_commit_reference_gently(sha1, 1); | |
64deb858 JH |
59 | struct object *object; |
60 | int prio; | |
61 | ||
908e5310 LT |
62 | if (!commit) |
63 | return 0; | |
64deb858 | 64 | object = parse_object(sha1); |
2d9e7c9f JH |
65 | /* If --all, then any refs are used. |
66 | * If --tags, then any tags are used. | |
67 | * Otherwise only annotated tags are used. | |
68 | */ | |
64deb858 | 69 | if (!strncmp(path, "refs/tags/", 10)) { |
1974632c | 70 | if (object->type == OBJ_TAG) |
64deb858 JH |
71 | prio = 2; |
72 | else | |
73 | prio = 1; | |
74 | } | |
75 | else | |
76 | prio = 0; | |
77 | ||
635d4134 | 78 | if (!all) { |
64deb858 JH |
79 | if (!prio) |
80 | return 0; | |
81 | if (!tags && prio < 2) | |
635d4134 | 82 | return 0; |
635d4134 | 83 | } |
64deb858 | 84 | add_to_known_names(all ? path + 5 : path + 10, commit, prio); |
908e5310 LT |
85 | return 0; |
86 | } | |
87 | ||
88 | static 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; | |
64deb858 JH |
94 | |
95 | if (a->prio != b->prio) | |
96 | return b->prio - a->prio; | |
908e5310 LT |
97 | return (a_date > b_date) ? -1 : (a_date == b_date) ? 0 : 1; |
98 | } | |
99 | ||
554fe20d | 100 | static void describe(const char *arg, int last_one) |
908e5310 | 101 | { |
4c34a2c5 JH |
102 | unsigned char sha1[20]; |
103 | struct commit *cmit; | |
908e5310 LT |
104 | struct commit_list *list; |
105 | static int initialized = 0; | |
106 | struct commit_name *n; | |
107 | ||
31fff305 DL |
108 | if (get_sha1(arg, sha1)) |
109 | die("Not a valid object name %s", arg); | |
4c34a2c5 JH |
110 | cmit = lookup_commit_reference(sha1); |
111 | if (!cmit) | |
31fff305 | 112 | die("%s is not a valid '%s' object", arg, commit_type); |
4c34a2c5 | 113 | |
908e5310 LT |
114 | if (!initialized) { |
115 | initialized = 1; | |
cb5d709f | 116 | for_each_ref(get_name, NULL); |
908e5310 LT |
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) { | |
4cdf78bf | 132 | printf("%s-g%s\n", n->path, |
2d9e7c9f | 133 | find_unique_abbrev(cmit->object.sha1, abbrev)); |
181dc776 JH |
134 | if (!last_one) |
135 | clear_commit_marks(cmit, SEEN); | |
8c23b6fa | 136 | return; |
908e5310 LT |
137 | } |
138 | } | |
8c23b6fa | 139 | die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1)); |
908e5310 LT |
140 | } |
141 | ||
142 | int main(int argc, char **argv) | |
143 | { | |
144 | int i; | |
145 | ||
146 | for (i = 1; i < argc; i++) { | |
147 | const char *arg = argv[i]; | |
908e5310 | 148 | |
4c34a2c5 JH |
149 | if (*arg != '-') |
150 | break; | |
151 | else if (!strcmp(arg, "--all")) | |
908e5310 | 152 | all = 1; |
4c34a2c5 | 153 | else if (!strcmp(arg, "--tags")) |
2d9e7c9f | 154 | tags = 1; |
4c34a2c5 | 155 | else if (!strncmp(arg, "--abbrev=", 9)) { |
2d9e7c9f | 156 | abbrev = strtoul(arg + 9, NULL, 10); |
f7122265 | 157 | if (abbrev < MINIMUM_ABBREV || 40 < abbrev) |
2d9e7c9f | 158 | abbrev = DEFAULT_ABBREV; |
2d9e7c9f | 159 | } |
4c34a2c5 | 160 | else |
908e5310 | 161 | usage(describe_usage); |
908e5310 | 162 | } |
4c34a2c5 | 163 | |
8112894d DL |
164 | setup_git_directory(); |
165 | ||
5b6df8e4 | 166 | if (argc <= i) |
fec9ebf1 | 167 | describe("HEAD", 1); |
4c34a2c5 | 168 | else |
fec9ebf1 JH |
169 | while (i < argc) { |
170 | describe(argv[i], (i == argc - 1)); | |
171 | i++; | |
172 | } | |
4c34a2c5 | 173 | |
908e5310 LT |
174 | return 0; |
175 | } |