7 #include "parse-options.h"
8 #include "sha1-lookup.h"
10 #define CUTOFF_DATE_SLOP 86400 /* one day */
12 typedef struct rev_name {
14 timestamp_t taggerdate;
20 static timestamp_t cutoff = TIME_MAX;
22 /* How many generations are maximally preferred over _one_ merge traversal? */
23 #define MERGE_TRAVERSAL_WEIGHT 65535
25 static int is_better_name(struct rev_name *name,
27 timestamp_t taggerdate,
33 * When comparing names based on tags, prefer names
34 * based on the older tag, even if it is farther away.
36 if (from_tag && name->from_tag)
37 return (name->taggerdate > taggerdate ||
38 (name->taggerdate == taggerdate &&
39 name->distance > distance));
42 * We know that at least one of them is a non-tag at this point.
43 * favor a tag over a non-tag.
45 if (name->from_tag != from_tag)
49 * We are now looking at two non-tags. Tiebreak to favor
52 if (name->distance != distance)
53 return name->distance > distance;
55 /* ... or tiebreak to favor older date */
56 if (name->taggerdate != taggerdate)
57 return name->taggerdate > taggerdate;
59 /* keep the current one if we cannot decide */
63 static void name_rev(struct commit *commit,
64 const char *tip_name, timestamp_t taggerdate,
65 int generation, int distance, int from_tag,
68 struct rev_name *name = (struct rev_name *)commit->util;
69 struct commit_list *parents;
70 int parent_number = 1;
75 if (commit->date < cutoff)
79 tip_name = to_free = xstrfmt("%s^0", tip_name);
82 die("generation: %d, but deref?", generation);
86 name = xmalloc(sizeof(rev_name));
89 } else if (is_better_name(name, tip_name, taggerdate,
90 generation, distance, from_tag)) {
92 name->tip_name = tip_name;
93 name->taggerdate = taggerdate;
94 name->generation = generation;
95 name->distance = distance;
96 name->from_tag = from_tag;
102 for (parents = commit->parents;
104 parents = parents->next, parent_number++) {
105 if (parent_number > 1) {
109 strip_suffix(tip_name, "^0", &len);
111 new_name = xstrfmt("%.*s~%d^%d", (int)len, tip_name,
112 generation, parent_number);
114 new_name = xstrfmt("%.*s^%d", (int)len, tip_name,
117 name_rev(parents->item, new_name, taggerdate, 0,
118 distance + MERGE_TRAVERSAL_WEIGHT,
121 name_rev(parents->item, tip_name, taggerdate,
122 generation + 1, distance + 1,
128 static int subpath_matches(const char *path, const char *filter)
130 const char *subpath = path;
133 if (!wildmatch(filter, subpath, 0))
134 return subpath - path;
135 subpath = strchr(subpath, '/');
142 static const char *name_ref_abbrev(const char *refname, int shorten_unambiguous)
144 if (shorten_unambiguous)
145 refname = shorten_unambiguous_ref(refname, 0);
146 else if (starts_with(refname, "refs/heads/"))
147 refname = refname + 11;
148 else if (starts_with(refname, "refs/"))
149 refname = refname + 5;
153 struct name_ref_data {
156 struct string_list ref_filters;
157 struct string_list exclude_filters;
160 static struct tip_table {
161 struct tip_table_entry {
162 struct object_id oid;
170 static void add_to_tip_table(const struct object_id *oid, const char *refname,
171 int shorten_unambiguous)
173 refname = name_ref_abbrev(refname, shorten_unambiguous);
175 ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc);
176 oidcpy(&tip_table.table[tip_table.nr].oid, oid);
177 tip_table.table[tip_table.nr].refname = xstrdup(refname);
179 tip_table.sorted = 0;
182 static int tipcmp(const void *a_, const void *b_)
184 const struct tip_table_entry *a = a_, *b = b_;
185 return oidcmp(&a->oid, &b->oid);
188 static int name_ref(const char *path, const struct object_id *oid, int flags, void *cb_data)
190 struct object *o = parse_object(oid);
191 struct name_ref_data *data = cb_data;
192 int can_abbreviate_output = data->tags_only && data->name_only;
194 timestamp_t taggerdate = TIME_MAX;
196 if (data->tags_only && !starts_with(path, "refs/tags/"))
199 if (data->exclude_filters.nr) {
200 struct string_list_item *item;
202 for_each_string_list_item(item, &data->exclude_filters) {
203 if (subpath_matches(path, item->string) >= 0)
208 if (data->ref_filters.nr) {
209 struct string_list_item *item;
212 /* See if any of the patterns match. */
213 for_each_string_list_item(item, &data->ref_filters) {
215 * Check all patterns even after finding a match, so
216 * that we can see if a match with a subpath exists.
217 * When a user asked for 'refs/tags/v*' and 'v1.*',
218 * both of which match, the user is showing her
219 * willingness to accept a shortened output by having
220 * the 'v1.*' in the acceptable refnames, so we
221 * shouldn't stop when seeing 'refs/tags/v1.4' matches
222 * 'refs/tags/v*'. We should show it as 'v1.4'.
224 switch (subpath_matches(path, item->string)) {
225 case -1: /* did not match */
227 case 0: /* matched fully */
230 default: /* matched subpath */
232 can_abbreviate_output = 1;
237 /* If none of the patterns matched, stop now */
242 add_to_tip_table(oid, path, can_abbreviate_output);
244 while (o && o->type == OBJ_TAG) {
245 struct tag *t = (struct tag *) o;
247 break; /* broken repository */
248 o = parse_object(&t->tagged->oid);
250 taggerdate = t->date;
252 if (o && o->type == OBJ_COMMIT) {
253 struct commit *commit = (struct commit *)o;
254 int from_tag = starts_with(path, "refs/tags/");
256 if (taggerdate == TIME_MAX)
257 taggerdate = ((struct commit *)o)->date;
258 path = name_ref_abbrev(path, can_abbreviate_output);
259 name_rev(commit, xstrdup(path), taggerdate, 0, 0,
265 static const unsigned char *nth_tip_table_ent(size_t ix, void *table_)
267 struct tip_table_entry *table = table_;
268 return table[ix].oid.hash;
271 static const char *get_exact_ref_match(const struct object *o)
275 if (!tip_table.table || !tip_table.nr)
278 if (!tip_table.sorted) {
279 QSORT(tip_table.table, tip_table.nr, tipcmp);
280 tip_table.sorted = 1;
283 found = sha1_pos(o->oid.hash, tip_table.table, tip_table.nr,
286 return tip_table.table[found].refname;
290 /* may return a constant string or use "buf" as scratch space */
291 static const char *get_rev_name(const struct object *o, struct strbuf *buf)
296 if (o->type != OBJ_COMMIT)
297 return get_exact_ref_match(o);
298 c = (struct commit *) o;
306 int len = strlen(n->tip_name);
307 if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
310 strbuf_addf(buf, "%.*s~%d", len, n->tip_name, n->generation);
315 static void show_name(const struct object *obj,
316 const char *caller_name,
317 int always, int allow_undefined, int name_only)
320 const struct object_id *oid = &obj->oid;
321 struct strbuf buf = STRBUF_INIT;
324 printf("%s ", caller_name ? caller_name : oid_to_hex(oid));
325 name = get_rev_name(obj, &buf);
327 printf("%s\n", name);
328 else if (allow_undefined)
329 printf("undefined\n");
331 printf("%s\n", find_unique_abbrev(oid, DEFAULT_ABBREV));
333 die("cannot describe '%s'", oid_to_hex(oid));
334 strbuf_release(&buf);
337 static char const * const name_rev_usage[] = {
338 N_("git name-rev [<options>] <commit>..."),
339 N_("git name-rev [<options>] --all"),
340 N_("git name-rev [<options>] --stdin"),
344 static void name_rev_line(char *p, struct name_ref_data *data)
346 struct strbuf buf = STRBUF_INIT;
349 for (p_start = p; *p; p++) {
350 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
353 else if (++forty == GIT_SHA1_HEXSZ &&
355 struct object_id oid;
356 const char *name = NULL;
358 int p_len = p - p_start + 1;
363 if (!get_oid(p - (GIT_SHA1_HEXSZ - 1), &oid)) {
365 lookup_object(oid.hash);
367 name = get_rev_name(o, &buf);
375 printf("%.*s%s", p_len - GIT_SHA1_HEXSZ, p_start, name);
377 printf("%.*s (%s)", p_len, p_start, name);
384 fwrite(p_start, p - p_start, 1, stdout);
386 strbuf_release(&buf);
389 int cmd_name_rev(int argc, const char **argv, const char *prefix)
391 struct object_array revs = OBJECT_ARRAY_INIT;
392 int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
393 struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP };
394 struct option opts[] = {
395 OPT_BOOL(0, "name-only", &data.name_only, N_("print only names (no SHA-1)")),
396 OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")),
397 OPT_STRING_LIST(0, "refs", &data.ref_filters, N_("pattern"),
398 N_("only use refs matching <pattern>")),
399 OPT_STRING_LIST(0, "exclude", &data.exclude_filters, N_("pattern"),
400 N_("ignore refs matching <pattern>")),
402 OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")),
403 OPT_BOOL(0, "stdin", &transform_stdin, N_("read from stdin")),
404 OPT_BOOL(0, "undefined", &allow_undefined, N_("allow to print `undefined` names (default)")),
405 OPT_BOOL(0, "always", &always,
406 N_("show abbreviated commit object as fallback")),
408 /* A Hidden OPT_BOOL */
409 OPTION_SET_INT, 0, "peel-tag", &peel_tag, NULL,
410 N_("dereference tags in the input (internal use)"),
411 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1,
416 git_config(git_default_config, NULL);
417 argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0);
418 if (all + transform_stdin + !!argc > 1) {
419 error("Specify either a list, or --all, not both!");
420 usage_with_options(name_rev_usage, opts);
422 if (all || transform_stdin)
425 for (; argc; argc--, argv++) {
426 struct object_id oid;
427 struct object *object;
428 struct commit *commit;
430 if (get_oid(*argv, &oid)) {
431 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
437 object = parse_object(&oid);
439 struct object *peeled = deref_tag(object, *argv, 0);
440 if (peeled && peeled->type == OBJ_COMMIT)
441 commit = (struct commit *)peeled;
445 fprintf(stderr, "Could not get object for %s. Skipping.\n",
451 if (cutoff > commit->date)
452 cutoff = commit->date;
457 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
461 object = (struct object *)commit;
463 add_object_array(object, *argv, &revs);
467 cutoff = cutoff - CUTOFF_DATE_SLOP;
468 for_each_ref(name_ref, &data);
470 if (transform_stdin) {
473 while (!feof(stdin)) {
474 char *p = fgets(buffer, sizeof(buffer), stdin);
477 name_rev_line(p, &data);
482 max = get_max_object_index();
483 for (i = 0; i < max; i++) {
484 struct object *obj = get_indexed_object(i);
485 if (!obj || obj->type != OBJ_COMMIT)
488 always, allow_undefined, data.name_only);
492 for (i = 0; i < revs.nr; i++)
493 show_name(revs.objects[i].item, revs.objects[i].name,
494 always, allow_undefined, data.name_only);