1#include "builtin.h"
2#include "cache.h"
3#include "config.h"
4#include "commit.h"
5#include "tag.h"
6#include "refs.h"
7#include "parse-options.h"
8#include "sha1-lookup.h"
9
10#define CUTOFF_DATE_SLOP 86400 /* one day */
11
12typedef struct rev_name {
13 const char *tip_name;
14 unsigned long taggerdate;
15 int generation;
16 int distance;
17} rev_name;
18
19static long cutoff = LONG_MAX;
20
21/* How many generations are maximally preferred over _one_ merge traversal? */
22#define MERGE_TRAVERSAL_WEIGHT 65535
23
24static void name_rev(struct commit *commit,
25 const char *tip_name, unsigned long taggerdate,
26 int generation, int distance,
27 int deref)
28{
29 struct rev_name *name = (struct rev_name *)commit->util;
30 struct commit_list *parents;
31 int parent_number = 1;
32
33 parse_commit(commit);
34
35 if (commit->date < cutoff)
36 return;
37
38 if (deref) {
39 tip_name = xstrfmt("%s^0", tip_name);
40
41 if (generation)
42 die("generation: %d, but deref?", generation);
43 }
44
45 if (name == NULL) {
46 name = xmalloc(sizeof(rev_name));
47 commit->util = name;
48 goto copy_data;
49 } else if (name->taggerdate > taggerdate ||
50 (name->taggerdate == taggerdate &&
51 name->distance > distance)) {
52copy_data:
53 name->tip_name = tip_name;
54 name->taggerdate = taggerdate;
55 name->generation = generation;
56 name->distance = distance;
57 } else
58 return;
59
60 for (parents = commit->parents;
61 parents;
62 parents = parents->next, parent_number++) {
63 if (parent_number > 1) {
64 size_t len;
65 char *new_name;
66
67 strip_suffix(tip_name, "^0", &len);
68 if (generation > 0)
69 new_name = xstrfmt("%.*s~%d^%d", (int)len, tip_name,
70 generation, parent_number);
71 else
72 new_name = xstrfmt("%.*s^%d", (int)len, tip_name,
73 parent_number);
74
75 name_rev(parents->item, new_name, taggerdate, 0,
76 distance + MERGE_TRAVERSAL_WEIGHT, 0);
77 } else {
78 name_rev(parents->item, tip_name, taggerdate,
79 generation + 1, distance + 1, 0);
80 }
81 }
82}
83
84static int subpath_matches(const char *path, const char *filter)
85{
86 const char *subpath = path;
87
88 while (subpath) {
89 if (!wildmatch(filter, subpath, 0, NULL))
90 return subpath - path;
91 subpath = strchr(subpath, '/');
92 if (subpath)
93 subpath++;
94 }
95 return -1;
96}
97
98static const char *name_ref_abbrev(const char *refname, int shorten_unambiguous)
99{
100 if (shorten_unambiguous)
101 refname = shorten_unambiguous_ref(refname, 0);
102 else if (starts_with(refname, "refs/heads/"))
103 refname = refname + 11;
104 else if (starts_with(refname, "refs/"))
105 refname = refname + 5;
106 return refname;
107}
108
109struct name_ref_data {
110 int tags_only;
111 int name_only;
112 struct string_list ref_filters;
113 struct string_list exclude_filters;
114};
115
116static struct tip_table {
117 struct tip_table_entry {
118 unsigned char sha1[20];
119 const char *refname;
120 } *table;
121 int nr;
122 int alloc;
123 int sorted;
124} tip_table;
125
126static void add_to_tip_table(const unsigned char *sha1, const char *refname,
127 int shorten_unambiguous)
128{
129 refname = name_ref_abbrev(refname, shorten_unambiguous);
130
131 ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc);
132 hashcpy(tip_table.table[tip_table.nr].sha1, sha1);
133 tip_table.table[tip_table.nr].refname = xstrdup(refname);
134 tip_table.nr++;
135 tip_table.sorted = 0;
136}
137
138static int tipcmp(const void *a_, const void *b_)
139{
140 const struct tip_table_entry *a = a_, *b = b_;
141 return hashcmp(a->sha1, b->sha1);
142}
143
144static int name_ref(const char *path, const struct object_id *oid, int flags, void *cb_data)
145{
146 struct object *o = parse_object(oid->hash);
147 struct name_ref_data *data = cb_data;
148 int can_abbreviate_output = data->tags_only && data->name_only;
149 int deref = 0;
150 unsigned long taggerdate = ULONG_MAX;
151
152 if (data->tags_only && !starts_with(path, "refs/tags/"))
153 return 0;
154
155 if (data->exclude_filters.nr) {
156 struct string_list_item *item;
157
158 for_each_string_list_item(item, &data->exclude_filters) {
159 if (subpath_matches(path, item->string) >= 0)
160 return 0;
161 }
162 }
163
164 if (data->ref_filters.nr) {
165 struct string_list_item *item;
166 int matched = 0;
167
168 /* See if any of the patterns match. */
169 for_each_string_list_item(item, &data->ref_filters) {
170 /*
171 * Check all patterns even after finding a match, so
172 * that we can see if a match with a subpath exists.
173 * When a user asked for 'refs/tags/v*' and 'v1.*',
174 * both of which match, the user is showing her
175 * willingness to accept a shortened output by having
176 * the 'v1.*' in the acceptable refnames, so we
177 * shouldn't stop when seeing 'refs/tags/v1.4' matches
178 * 'refs/tags/v*'. We should show it as 'v1.4'.
179 */
180 switch (subpath_matches(path, item->string)) {
181 case -1: /* did not match */
182 break;
183 case 0: /* matched fully */
184 matched = 1;
185 break;
186 default: /* matched subpath */
187 matched = 1;
188 can_abbreviate_output = 1;
189 break;
190 }
191 }
192
193 /* If none of the patterns matched, stop now */
194 if (!matched)
195 return 0;
196 }
197
198 add_to_tip_table(oid->hash, path, can_abbreviate_output);
199
200 while (o && o->type == OBJ_TAG) {
201 struct tag *t = (struct tag *) o;
202 if (!t->tagged)
203 break; /* broken repository */
204 o = parse_object(t->tagged->oid.hash);
205 deref = 1;
206 taggerdate = t->date;
207 }
208 if (o && o->type == OBJ_COMMIT) {
209 struct commit *commit = (struct commit *)o;
210
211 path = name_ref_abbrev(path, can_abbreviate_output);
212 name_rev(commit, xstrdup(path), taggerdate, 0, 0, deref);
213 }
214 return 0;
215}
216
217static const unsigned char *nth_tip_table_ent(size_t ix, void *table_)
218{
219 struct tip_table_entry *table = table_;
220 return table[ix].sha1;
221}
222
223static const char *get_exact_ref_match(const struct object *o)
224{
225 int found;
226
227 if (!tip_table.table || !tip_table.nr)
228 return NULL;
229
230 if (!tip_table.sorted) {
231 QSORT(tip_table.table, tip_table.nr, tipcmp);
232 tip_table.sorted = 1;
233 }
234
235 found = sha1_pos(o->oid.hash, tip_table.table, tip_table.nr,
236 nth_tip_table_ent);
237 if (0 <= found)
238 return tip_table.table[found].refname;
239 return NULL;
240}
241
242/* may return a constant string or use "buf" as scratch space */
243static const char *get_rev_name(const struct object *o, struct strbuf *buf)
244{
245 struct rev_name *n;
246 struct commit *c;
247
248 if (o->type != OBJ_COMMIT)
249 return get_exact_ref_match(o);
250 c = (struct commit *) o;
251 n = c->util;
252 if (!n)
253 return NULL;
254
255 if (!n->generation)
256 return n->tip_name;
257 else {
258 int len = strlen(n->tip_name);
259 if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
260 len -= 2;
261 strbuf_reset(buf);
262 strbuf_addf(buf, "%.*s~%d", len, n->tip_name, n->generation);
263 return buf->buf;
264 }
265}
266
267static void show_name(const struct object *obj,
268 const char *caller_name,
269 int always, int allow_undefined, int name_only)
270{
271 const char *name;
272 const struct object_id *oid = &obj->oid;
273 struct strbuf buf = STRBUF_INIT;
274
275 if (!name_only)
276 printf("%s ", caller_name ? caller_name : oid_to_hex(oid));
277 name = get_rev_name(obj, &buf);
278 if (name)
279 printf("%s\n", name);
280 else if (allow_undefined)
281 printf("undefined\n");
282 else if (always)
283 printf("%s\n", find_unique_abbrev(oid->hash, DEFAULT_ABBREV));
284 else
285 die("cannot describe '%s'", oid_to_hex(oid));
286 strbuf_release(&buf);
287}
288
289static char const * const name_rev_usage[] = {
290 N_("git name-rev [<options>] <commit>..."),
291 N_("git name-rev [<options>] --all"),
292 N_("git name-rev [<options>] --stdin"),
293 NULL
294};
295
296static void name_rev_line(char *p, struct name_ref_data *data)
297{
298 struct strbuf buf = STRBUF_INIT;
299 int forty = 0;
300 char *p_start;
301 for (p_start = p; *p; p++) {
302#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
303 if (!ishex(*p))
304 forty = 0;
305 else if (++forty == 40 &&
306 !ishex(*(p+1))) {
307 unsigned char sha1[40];
308 const char *name = NULL;
309 char c = *(p+1);
310 int p_len = p - p_start + 1;
311
312 forty = 0;
313
314 *(p+1) = 0;
315 if (!get_sha1(p - 39, sha1)) {
316 struct object *o =
317 lookup_object(sha1);
318 if (o)
319 name = get_rev_name(o, &buf);
320 }
321 *(p+1) = c;
322
323 if (!name)
324 continue;
325
326 if (data->name_only)
327 printf("%.*s%s", p_len - 40, p_start, name);
328 else
329 printf("%.*s (%s)", p_len, p_start, name);
330 p_start = p + 1;
331 }
332 }
333
334 /* flush */
335 if (p_start != p)
336 fwrite(p_start, p - p_start, 1, stdout);
337
338 strbuf_release(&buf);
339}
340
341int cmd_name_rev(int argc, const char **argv, const char *prefix)
342{
343 struct object_array revs = OBJECT_ARRAY_INIT;
344 int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
345 struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP };
346 struct option opts[] = {
347 OPT_BOOL(0, "name-only", &data.name_only, N_("print only names (no SHA-1)")),
348 OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")),
349 OPT_STRING_LIST(0, "refs", &data.ref_filters, N_("pattern"),
350 N_("only use refs matching <pattern>")),
351 OPT_STRING_LIST(0, "exclude", &data.exclude_filters, N_("pattern"),
352 N_("ignore refs matching <pattern>")),
353 OPT_GROUP(""),
354 OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")),
355 OPT_BOOL(0, "stdin", &transform_stdin, N_("read from stdin")),
356 OPT_BOOL(0, "undefined", &allow_undefined, N_("allow to print `undefined` names (default)")),
357 OPT_BOOL(0, "always", &always,
358 N_("show abbreviated commit object as fallback")),
359 {
360 /* A Hidden OPT_BOOL */
361 OPTION_SET_INT, 0, "peel-tag", &peel_tag, NULL,
362 N_("dereference tags in the input (internal use)"),
363 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1,
364 },
365 OPT_END(),
366 };
367
368 git_config(git_default_config, NULL);
369 argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0);
370 if (all + transform_stdin + !!argc > 1) {
371 error("Specify either a list, or --all, not both!");
372 usage_with_options(name_rev_usage, opts);
373 }
374 if (all || transform_stdin)
375 cutoff = 0;
376
377 for (; argc; argc--, argv++) {
378 unsigned char sha1[20];
379 struct object *object;
380 struct commit *commit;
381
382 if (get_sha1(*argv, sha1)) {
383 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
384 *argv);
385 continue;
386 }
387
388 commit = NULL;
389 object = parse_object(sha1);
390 if (object) {
391 struct object *peeled = deref_tag(object, *argv, 0);
392 if (peeled && peeled->type == OBJ_COMMIT)
393 commit = (struct commit *)peeled;
394 }
395
396 if (!object) {
397 fprintf(stderr, "Could not get object for %s. Skipping.\n",
398 *argv);
399 continue;
400 }
401
402 if (commit) {
403 if (cutoff > commit->date)
404 cutoff = commit->date;
405 }
406
407 if (peel_tag) {
408 if (!commit) {
409 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
410 *argv);
411 continue;
412 }
413 object = (struct object *)commit;
414 }
415 add_object_array(object, *argv, &revs);
416 }
417
418 if (cutoff)
419 cutoff = cutoff - CUTOFF_DATE_SLOP;
420 for_each_ref(name_ref, &data);
421
422 if (transform_stdin) {
423 char buffer[2048];
424
425 while (!feof(stdin)) {
426 char *p = fgets(buffer, sizeof(buffer), stdin);
427 if (!p)
428 break;
429 name_rev_line(p, &data);
430 }
431 } else if (all) {
432 int i, max;
433
434 max = get_max_object_index();
435 for (i = 0; i < max; i++) {
436 struct object *obj = get_indexed_object(i);
437 if (!obj || obj->type != OBJ_COMMIT)
438 continue;
439 show_name(obj, NULL,
440 always, allow_undefined, data.name_only);
441 }
442 } else {
443 int i;
444 for (i = 0; i < revs.nr; i++)
445 show_name(revs.objects[i].item, revs.objects[i].name,
446 always, allow_undefined, data.name_only);
447 }
448
449 return 0;
450}