]>
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 | |
635d4134 | 11 | static int all = 0; /* Default to annotated tags only */ |
2d9e7c9f JH |
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; | |
908e5310 LT |
16 | |
17 | static int names = 0, allocs = 0; | |
18 | static struct commit_name { | |
19 | const struct commit *commit; | |
20 | char path[]; | |
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 | ||
36 | static void add_to_known_names(const char *path, const struct commit *commit) | |
37 | { | |
38 | int idx; | |
39 | int len = strlen(path)+1; | |
40 | struct commit_name *name = xmalloc(sizeof(struct commit_name) + len); | |
41 | ||
42 | name->commit = commit; | |
43 | memcpy(name->path, path, len); | |
44 | idx = names; | |
45 | if (idx >= allocs) { | |
46 | allocs = (idx + 50) * 3 / 2; | |
47 | name_array = xrealloc(name_array, allocs*sizeof(*name_array)); | |
48 | } | |
49 | name_array[idx] = name; | |
50 | names = ++idx; | |
51 | } | |
52 | ||
53 | static int get_name(const char *path, const unsigned char *sha1) | |
54 | { | |
55 | struct commit *commit = lookup_commit_reference_gently(sha1, 1); | |
56 | if (!commit) | |
57 | return 0; | |
2d9e7c9f JH |
58 | /* If --all, then any refs are used. |
59 | * If --tags, then any tags are used. | |
60 | * Otherwise only annotated tags are used. | |
61 | */ | |
635d4134 | 62 | if (!all) { |
635d4134 JH |
63 | if (strncmp(path, "refs/tags/", 10)) |
64 | return 0; | |
2d9e7c9f JH |
65 | if (!tags) { |
66 | struct object *object; | |
67 | object = parse_object(sha1); | |
68 | if (object->type != tag_type) | |
69 | return 0; | |
70 | } | |
635d4134 JH |
71 | } |
72 | add_to_known_names(all ? path : path + 10, commit); | |
908e5310 LT |
73 | return 0; |
74 | } | |
75 | ||
76 | static int compare_names(const void *_a, const void *_b) | |
77 | { | |
78 | struct commit_name *a = *(struct commit_name **)_a; | |
79 | struct commit_name *b = *(struct commit_name **)_b; | |
80 | unsigned long a_date = a->commit->date; | |
81 | unsigned long b_date = b->commit->date; | |
82 | return (a_date > b_date) ? -1 : (a_date == b_date) ? 0 : 1; | |
83 | } | |
84 | ||
85 | static void describe(struct commit *cmit) | |
86 | { | |
87 | struct commit_list *list; | |
88 | static int initialized = 0; | |
89 | struct commit_name *n; | |
90 | ||
91 | if (!initialized) { | |
92 | initialized = 1; | |
93 | for_each_ref(get_name); | |
94 | qsort(name_array, names, sizeof(*name_array), compare_names); | |
95 | } | |
96 | ||
97 | n = match(cmit); | |
98 | if (n) { | |
99 | printf("%s\n", n->path); | |
100 | return; | |
101 | } | |
102 | ||
103 | list = NULL; | |
104 | commit_list_insert(cmit, &list); | |
105 | while (list) { | |
106 | struct commit *c = pop_most_recent_commit(&list, SEEN); | |
107 | n = match(c); | |
108 | if (n) { | |
4cdf78bf | 109 | printf("%s-g%s\n", n->path, |
2d9e7c9f | 110 | find_unique_abbrev(cmit->object.sha1, abbrev)); |
908e5310 LT |
111 | return; |
112 | } | |
113 | } | |
114 | } | |
115 | ||
116 | int main(int argc, char **argv) | |
117 | { | |
118 | int i; | |
119 | ||
120 | for (i = 1; i < argc; i++) { | |
121 | const char *arg = argv[i]; | |
122 | unsigned char sha1[20]; | |
123 | struct commit *cmit; | |
124 | ||
125 | if (!strcmp(arg, "--all")) { | |
126 | all = 1; | |
127 | continue; | |
128 | } | |
2d9e7c9f JH |
129 | if (!strcmp(arg, "--tags")) { |
130 | tags = 1; | |
131 | continue; | |
132 | } | |
133 | if (!strncmp(arg, "--abbrev=", 9)) { | |
134 | abbrev = strtoul(arg + 9, NULL, 10); | |
135 | if (abbrev < 4 || 40 <= abbrev) | |
136 | abbrev = DEFAULT_ABBREV; | |
137 | continue; | |
138 | } | |
908e5310 LT |
139 | if (get_sha1(arg, sha1) < 0) |
140 | usage(describe_usage); | |
141 | cmit = lookup_commit_reference(sha1); | |
142 | if (!cmit) | |
143 | usage(describe_usage); | |
144 | describe(cmit); | |
145 | } | |
146 | return 0; | |
147 | } |