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