]>
Commit | Line | Data |
---|---|---|
d6b64ed0 | 1 | #include "builtin.h" |
bd321bcc JS |
2 | #include "cache.h" |
3 | #include "commit.h" | |
4 | #include "tag.h" | |
5 | #include "refs.h" | |
6 | ||
7 | static const char name_rev_usage[] = | |
2afc29aa | 8 | "git-name-rev [--tags | --refs=<pattern>] ( --all | --stdin | committish [committish...] )\n"; |
bd321bcc JS |
9 | |
10 | typedef struct rev_name { | |
11 | const char *tip_name; | |
12 | int merge_traversals; | |
13 | int generation; | |
14 | } rev_name; | |
15 | ||
16 | static long cutoff = LONG_MAX; | |
17 | ||
18 | static void name_rev(struct commit *commit, | |
19 | const char *tip_name, int merge_traversals, int generation, | |
20 | int deref) | |
21 | { | |
d3ff6f55 | 22 | struct rev_name *name = (struct rev_name *)commit->util; |
bd321bcc | 23 | struct commit_list *parents; |
f2e6f1c9 | 24 | int parent_number = 1; |
bd321bcc JS |
25 | |
26 | if (!commit->object.parsed) | |
27 | parse_commit(commit); | |
28 | ||
29 | if (commit->date < cutoff) | |
30 | return; | |
31 | ||
32 | if (deref) { | |
33 | char *new_name = xmalloc(strlen(tip_name)+3); | |
34 | strcpy(new_name, tip_name); | |
35 | strcat(new_name, "^0"); | |
36 | tip_name = new_name; | |
37 | ||
38 | if (generation) | |
39 | die("generation: %d, but deref?", generation); | |
40 | } | |
41 | ||
42 | if (name == NULL) { | |
43 | name = xmalloc(sizeof(rev_name)); | |
d3ff6f55 | 44 | commit->util = name; |
bd321bcc JS |
45 | goto copy_data; |
46 | } else if (name->merge_traversals > merge_traversals || | |
47 | (name->merge_traversals == merge_traversals && | |
48 | name->generation > generation)) { | |
49 | copy_data: | |
50 | name->tip_name = tip_name; | |
51 | name->merge_traversals = merge_traversals; | |
52 | name->generation = generation; | |
53 | } else | |
54 | return; | |
55 | ||
56 | for (parents = commit->parents; | |
57 | parents; | |
58 | parents = parents->next, parent_number++) { | |
f2e6f1c9 | 59 | if (parent_number > 1) { |
59d3f541 | 60 | int len = strlen(tip_name); |
cf606e3d AW |
61 | char *new_name = xmalloc(len + |
62 | 1 + decimal_length(generation) + /* ~<n> */ | |
63 | 1 + 2 + /* ^NN */ | |
64 | 1); | |
bd321bcc | 65 | |
59d3f541 JS |
66 | if (len > 2 && !strcmp(tip_name + len - 2, "^0")) |
67 | len -= 2; | |
bd321bcc | 68 | if (generation > 0) |
59d3f541 | 69 | sprintf(new_name, "%.*s~%d^%d", len, tip_name, |
bd321bcc JS |
70 | generation, parent_number); |
71 | else | |
59d3f541 JS |
72 | sprintf(new_name, "%.*s^%d", len, tip_name, |
73 | parent_number); | |
bd321bcc JS |
74 | |
75 | name_rev(parents->item, new_name, | |
76 | merge_traversals + 1 , 0, 0); | |
77 | } else { | |
78 | name_rev(parents->item, tip_name, merge_traversals, | |
79 | generation + 1, 0); | |
80 | } | |
81 | } | |
82 | } | |
83 | ||
2afc29aa JS |
84 | struct name_ref_data { |
85 | int tags_only; | |
86 | const char *ref_filter; | |
87 | }; | |
88 | ||
8da19775 | 89 | static int name_ref(const char *path, const unsigned char *sha1, int flags, void *cb_data) |
bd321bcc JS |
90 | { |
91 | struct object *o = parse_object(sha1); | |
2afc29aa | 92 | struct name_ref_data *data = cb_data; |
bd321bcc JS |
93 | int deref = 0; |
94 | ||
cc44c765 | 95 | if (data->tags_only && prefixcmp(path, "refs/tags/")) |
2afc29aa JS |
96 | return 0; |
97 | ||
98 | if (data->ref_filter && fnmatch(data->ref_filter, path, 0)) | |
bd321bcc JS |
99 | return 0; |
100 | ||
1974632c | 101 | while (o && o->type == OBJ_TAG) { |
bd321bcc JS |
102 | struct tag *t = (struct tag *) o; |
103 | if (!t->tagged) | |
104 | break; /* broken repository */ | |
105 | o = parse_object(t->tagged->sha1); | |
106 | deref = 1; | |
107 | } | |
1974632c | 108 | if (o && o->type == OBJ_COMMIT) { |
bd321bcc | 109 | struct commit *commit = (struct commit *)o; |
bd321bcc | 110 | |
cc44c765 | 111 | if (!prefixcmp(path, "refs/heads/")) |
2c817df2 | 112 | path = path + 11; |
cc44c765 | 113 | else if (!prefixcmp(path, "refs/")) |
2c817df2 | 114 | path = path + 5; |
bd321bcc | 115 | |
9befac47 | 116 | name_rev(commit, xstrdup(path), 0, 0, deref); |
bd321bcc JS |
117 | } |
118 | return 0; | |
119 | } | |
120 | ||
121 | /* returns a static buffer */ | |
122 | static const char* get_rev_name(struct object *o) | |
123 | { | |
124 | static char buffer[1024]; | |
d3ff6f55 LT |
125 | struct rev_name *n; |
126 | struct commit *c; | |
127 | ||
1974632c | 128 | if (o->type != OBJ_COMMIT) |
d3ff6f55 LT |
129 | return "undefined"; |
130 | c = (struct commit *) o; | |
131 | n = c->util; | |
bd321bcc JS |
132 | if (!n) |
133 | return "undefined"; | |
134 | ||
135 | if (!n->generation) | |
136 | return n->tip_name; | |
59d3f541 JS |
137 | else { |
138 | int len = strlen(n->tip_name); | |
139 | if (len > 2 && !strcmp(n->tip_name + len - 2, "^0")) | |
140 | len -= 2; | |
141 | snprintf(buffer, sizeof(buffer), "%.*s~%d", len, n->tip_name, | |
142 | n->generation); | |
143 | ||
144 | return buffer; | |
145 | } | |
bd321bcc | 146 | } |
1f1e895f | 147 | |
d6b64ed0 | 148 | int cmd_name_rev(int argc, const char **argv, const char *prefix) |
bd321bcc | 149 | { |
1f1e895f | 150 | struct object_array revs = { 0, 0, NULL }; |
bd321bcc | 151 | int as_is = 0, all = 0, transform_stdin = 0; |
2afc29aa | 152 | struct name_ref_data data = { 0, NULL }; |
bd321bcc | 153 | |
84a9b58c | 154 | git_config(git_default_config); |
bd321bcc JS |
155 | |
156 | if (argc < 2) | |
157 | usage(name_rev_usage); | |
158 | ||
159 | for (--argc, ++argv; argc; --argc, ++argv) { | |
160 | unsigned char sha1[20]; | |
161 | struct object *o; | |
162 | struct commit *commit; | |
163 | ||
164 | if (!as_is && (*argv)[0] == '-') { | |
165 | if (!strcmp(*argv, "--")) { | |
166 | as_is = 1; | |
167 | continue; | |
168 | } else if (!strcmp(*argv, "--tags")) { | |
2afc29aa JS |
169 | data.tags_only = 1; |
170 | continue; | |
cc44c765 | 171 | } else if (!prefixcmp(*argv, "--refs=")) { |
2afc29aa | 172 | data.ref_filter = *argv + 7; |
bd321bcc JS |
173 | continue; |
174 | } else if (!strcmp(*argv, "--all")) { | |
175 | if (argc > 1) | |
176 | die("Specify either a list, or --all, not both!"); | |
177 | all = 1; | |
178 | cutoff = 0; | |
179 | continue; | |
180 | } else if (!strcmp(*argv, "--stdin")) { | |
181 | if (argc > 1) | |
182 | die("Specify either a list, or --stdin, not both!"); | |
183 | transform_stdin = 1; | |
184 | cutoff = 0; | |
185 | continue; | |
186 | } | |
187 | usage(name_rev_usage); | |
188 | } | |
189 | ||
190 | if (get_sha1(*argv, sha1)) { | |
191 | fprintf(stderr, "Could not get sha1 for %s. Skipping.\n", | |
192 | *argv); | |
193 | continue; | |
194 | } | |
195 | ||
9534f40b | 196 | o = deref_tag(parse_object(sha1), *argv, 0); |
1974632c | 197 | if (!o || o->type != OBJ_COMMIT) { |
bd321bcc JS |
198 | fprintf(stderr, "Could not get commit for %s. Skipping.\n", |
199 | *argv); | |
200 | continue; | |
201 | } | |
202 | ||
203 | commit = (struct commit *)o; | |
204 | ||
205 | if (cutoff > commit->date) | |
206 | cutoff = commit->date; | |
207 | ||
1f1e895f | 208 | add_object_array((struct object *)commit, *argv, &revs); |
bd321bcc JS |
209 | } |
210 | ||
2afc29aa | 211 | for_each_ref(name_ref, &data); |
bd321bcc JS |
212 | |
213 | if (transform_stdin) { | |
214 | char buffer[2048]; | |
215 | char *p, *p_start; | |
216 | ||
217 | while (!feof(stdin)) { | |
218 | int forty = 0; | |
219 | p = fgets(buffer, sizeof(buffer), stdin); | |
220 | if (!p) | |
221 | break; | |
222 | ||
223 | for (p_start = p; *p; p++) { | |
224 | #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f')) | |
225 | if (!ishex(*p)) | |
226 | forty = 0; | |
227 | else if (++forty == 40 && | |
228 | !ishex(*(p+1))) { | |
229 | unsigned char sha1[40]; | |
230 | const char *name = "undefined"; | |
231 | char c = *(p+1); | |
232 | ||
233 | forty = 0; | |
234 | ||
235 | *(p+1) = 0; | |
236 | if (!get_sha1(p - 39, sha1)) { | |
237 | struct object *o = | |
238 | lookup_object(sha1); | |
239 | if (o) | |
240 | name = get_rev_name(o); | |
241 | } | |
242 | *(p+1) = c; | |
243 | ||
244 | if (!strcmp(name, "undefined")) | |
245 | continue; | |
246 | ||
2d76d0d1 JH |
247 | fwrite(p_start, p - p_start + 1, 1, |
248 | stdout); | |
249 | printf(" (%s)", name); | |
bd321bcc JS |
250 | p_start = p + 1; |
251 | } | |
252 | } | |
253 | ||
254 | /* flush */ | |
255 | if (p_start != p) | |
256 | fwrite(p_start, p - p_start, 1, stdout); | |
257 | } | |
258 | } else if (all) { | |
fc046a75 | 259 | int i, max; |
bd321bcc | 260 | |
fc046a75 LT |
261 | max = get_max_object_index(); |
262 | for (i = 0; i < max; i++) { | |
263 | struct object * obj = get_indexed_object(i); | |
264 | if (!obj) | |
265 | continue; | |
266 | printf("%s %s\n", sha1_to_hex(obj->sha1), get_rev_name(obj)); | |
267 | } | |
1f1e895f LT |
268 | } else { |
269 | int i; | |
270 | for (i = 0; i < revs.nr; i++) | |
271 | printf("%s %s\n", | |
272 | revs.objects[i].name, | |
273 | get_rev_name(revs.objects[i].item)); | |
274 | } | |
bd321bcc JS |
275 | |
276 | return 0; | |
277 | } | |
278 |