3 #include "repository.h"
8 #include "parse-options.h"
9 #include "sha1-lookup.h"
10 #include "commit-slab.h"
12 #define CUTOFF_DATE_SLOP 86400 /* one day */
14 typedef struct rev_name {
16 timestamp_t taggerdate;
22 define_commit_slab(commit_rev_name, struct rev_name *);
24 static timestamp_t cutoff = TIME_MAX;
25 static struct commit_rev_name rev_names;
27 /* How many generations are maximally preferred over _one_ merge traversal? */
28 #define MERGE_TRAVERSAL_WEIGHT 65535
30 static struct rev_name *get_commit_rev_name(struct commit *commit)
32 struct rev_name **slot = commit_rev_name_peek(&rev_names, commit);
34 return slot ? *slot : NULL;
37 static void set_commit_rev_name(struct commit *commit, struct rev_name *name)
39 *commit_rev_name_at(&rev_names, commit) = name;
42 static int is_better_name(struct rev_name *name,
43 timestamp_t taggerdate,
48 * When comparing names based on tags, prefer names
49 * based on the older tag, even if it is farther away.
51 if (from_tag && name->from_tag)
52 return (name->taggerdate > taggerdate ||
53 (name->taggerdate == taggerdate &&
54 name->distance > distance));
57 * We know that at least one of them is a non-tag at this point.
58 * favor a tag over a non-tag.
60 if (name->from_tag != from_tag)
64 * We are now looking at two non-tags. Tiebreak to favor
67 if (name->distance != distance)
68 return name->distance > distance;
70 /* ... or tiebreak to favor older date */
71 if (name->taggerdate != taggerdate)
72 return name->taggerdate > taggerdate;
74 /* keep the current one if we cannot decide */
78 static void name_rev(struct commit *commit,
79 const char *tip_name, timestamp_t taggerdate,
80 int generation, int distance, int from_tag,
83 struct rev_name *name = get_commit_rev_name(commit);
84 struct commit_list *parents;
85 int parent_number = 1;
90 if (commit->date < cutoff)
94 tip_name = to_free = xstrfmt("%s^0", tip_name);
97 die("generation: %d, but deref?", generation);
101 name = xmalloc(sizeof(rev_name));
102 set_commit_rev_name(commit, name);
104 } else if (is_better_name(name, taggerdate, distance, from_tag)) {
106 name->tip_name = tip_name;
107 name->taggerdate = taggerdate;
108 name->generation = generation;
109 name->distance = distance;
110 name->from_tag = from_tag;
116 for (parents = commit->parents;
118 parents = parents->next, parent_number++) {
119 if (parent_number > 1) {
123 strip_suffix(tip_name, "^0", &len);
125 new_name = xstrfmt("%.*s~%d^%d", (int)len, tip_name,
126 generation, parent_number);
128 new_name = xstrfmt("%.*s^%d", (int)len, tip_name,
131 name_rev(parents->item, new_name, taggerdate, 0,
132 distance + MERGE_TRAVERSAL_WEIGHT,
135 name_rev(parents->item, tip_name, taggerdate,
136 generation + 1, distance + 1,
142 static int subpath_matches(const char *path, const char *filter)
144 const char *subpath = path;
147 if (!wildmatch(filter, subpath, 0))
148 return subpath - path;
149 subpath = strchr(subpath, '/');
156 static const char *name_ref_abbrev(const char *refname, int shorten_unambiguous)
158 if (shorten_unambiguous)
159 refname = shorten_unambiguous_ref(refname, 0);
160 else if (starts_with(refname, "refs/heads/"))
161 refname = refname + 11;
162 else if (starts_with(refname, "refs/"))
163 refname = refname + 5;
167 struct name_ref_data {
170 struct string_list ref_filters;
171 struct string_list exclude_filters;
174 static struct tip_table {
175 struct tip_table_entry {
176 struct object_id oid;
184 static void add_to_tip_table(const struct object_id *oid, const char *refname,
185 int shorten_unambiguous)
187 refname = name_ref_abbrev(refname, shorten_unambiguous);
189 ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc);
190 oidcpy(&tip_table.table[tip_table.nr].oid, oid);
191 tip_table.table[tip_table.nr].refname = xstrdup(refname);
193 tip_table.sorted = 0;
196 static int tipcmp(const void *a_, const void *b_)
198 const struct tip_table_entry *a = a_, *b = b_;
199 return oidcmp(&a->oid, &b->oid);
202 static int name_ref(const char *path, const struct object_id *oid, int flags, void *cb_data)
204 struct object *o = parse_object(the_repository, oid);
205 struct name_ref_data *data = cb_data;
206 int can_abbreviate_output = data->tags_only && data->name_only;
208 timestamp_t taggerdate = TIME_MAX;
210 if (data->tags_only && !starts_with(path, "refs/tags/"))
213 if (data->exclude_filters.nr) {
214 struct string_list_item *item;
216 for_each_string_list_item(item, &data->exclude_filters) {
217 if (subpath_matches(path, item->string) >= 0)
222 if (data->ref_filters.nr) {
223 struct string_list_item *item;
226 /* See if any of the patterns match. */
227 for_each_string_list_item(item, &data->ref_filters) {
229 * Check all patterns even after finding a match, so
230 * that we can see if a match with a subpath exists.
231 * When a user asked for 'refs/tags/v*' and 'v1.*',
232 * both of which match, the user is showing her
233 * willingness to accept a shortened output by having
234 * the 'v1.*' in the acceptable refnames, so we
235 * shouldn't stop when seeing 'refs/tags/v1.4' matches
236 * 'refs/tags/v*'. We should show it as 'v1.4'.
238 switch (subpath_matches(path, item->string)) {
239 case -1: /* did not match */
241 case 0: /* matched fully */
244 default: /* matched subpath */
246 can_abbreviate_output = 1;
251 /* If none of the patterns matched, stop now */
256 add_to_tip_table(oid, path, can_abbreviate_output);
258 while (o && o->type == OBJ_TAG) {
259 struct tag *t = (struct tag *) o;
261 break; /* broken repository */
262 o = parse_object(the_repository, &t->tagged->oid);
264 taggerdate = t->date;
266 if (o && o->type == OBJ_COMMIT) {
267 struct commit *commit = (struct commit *)o;
268 int from_tag = starts_with(path, "refs/tags/");
270 if (taggerdate == TIME_MAX)
271 taggerdate = ((struct commit *)o)->date;
272 path = name_ref_abbrev(path, can_abbreviate_output);
273 name_rev(commit, xstrdup(path), taggerdate, 0, 0,
279 static const unsigned char *nth_tip_table_ent(size_t ix, void *table_)
281 struct tip_table_entry *table = table_;
282 return table[ix].oid.hash;
285 static const char *get_exact_ref_match(const struct object *o)
289 if (!tip_table.table || !tip_table.nr)
292 if (!tip_table.sorted) {
293 QSORT(tip_table.table, tip_table.nr, tipcmp);
294 tip_table.sorted = 1;
297 found = sha1_pos(o->oid.hash, tip_table.table, tip_table.nr,
300 return tip_table.table[found].refname;
304 /* may return a constant string or use "buf" as scratch space */
305 static const char *get_rev_name(const struct object *o, struct strbuf *buf)
310 if (o->type != OBJ_COMMIT)
311 return get_exact_ref_match(o);
312 c = (struct commit *) o;
313 n = get_commit_rev_name(c);
320 int len = strlen(n->tip_name);
321 if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
324 strbuf_addf(buf, "%.*s~%d", len, n->tip_name, n->generation);
329 static void show_name(const struct object *obj,
330 const char *caller_name,
331 int always, int allow_undefined, int name_only)
334 const struct object_id *oid = &obj->oid;
335 struct strbuf buf = STRBUF_INIT;
338 printf("%s ", caller_name ? caller_name : oid_to_hex(oid));
339 name = get_rev_name(obj, &buf);
341 printf("%s\n", name);
342 else if (allow_undefined)
343 printf("undefined\n");
345 printf("%s\n", find_unique_abbrev(oid, DEFAULT_ABBREV));
347 die("cannot describe '%s'", oid_to_hex(oid));
348 strbuf_release(&buf);
351 static char const * const name_rev_usage[] = {
352 N_("git name-rev [<options>] <commit>..."),
353 N_("git name-rev [<options>] --all"),
354 N_("git name-rev [<options>] --stdin"),
358 static void name_rev_line(char *p, struct name_ref_data *data)
360 struct strbuf buf = STRBUF_INIT;
363 const unsigned hexsz = the_hash_algo->hexsz;
365 for (p_start = p; *p; p++) {
366 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
369 else if (++counter == hexsz &&
371 struct object_id oid;
372 const char *name = NULL;
374 int p_len = p - p_start + 1;
379 if (!get_oid(p - (hexsz - 1), &oid)) {
381 lookup_object(the_repository, &oid);
383 name = get_rev_name(o, &buf);
391 printf("%.*s%s", p_len - hexsz, p_start, name);
393 printf("%.*s (%s)", p_len, p_start, name);
400 fwrite(p_start, p - p_start, 1, stdout);
402 strbuf_release(&buf);
405 int cmd_name_rev(int argc, const char **argv, const char *prefix)
407 struct object_array revs = OBJECT_ARRAY_INIT;
408 int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
409 struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP };
410 struct option opts[] = {
411 OPT_BOOL(0, "name-only", &data.name_only, N_("print only names (no SHA-1)")),
412 OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")),
413 OPT_STRING_LIST(0, "refs", &data.ref_filters, N_("pattern"),
414 N_("only use refs matching <pattern>")),
415 OPT_STRING_LIST(0, "exclude", &data.exclude_filters, N_("pattern"),
416 N_("ignore refs matching <pattern>")),
418 OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")),
419 OPT_BOOL(0, "stdin", &transform_stdin, N_("read from stdin")),
420 OPT_BOOL(0, "undefined", &allow_undefined, N_("allow to print `undefined` names (default)")),
421 OPT_BOOL(0, "always", &always,
422 N_("show abbreviated commit object as fallback")),
424 /* A Hidden OPT_BOOL */
425 OPTION_SET_INT, 0, "peel-tag", &peel_tag, NULL,
426 N_("dereference tags in the input (internal use)"),
427 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1,
432 init_commit_rev_name(&rev_names);
433 git_config(git_default_config, NULL);
434 argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0);
435 if (all + transform_stdin + !!argc > 1) {
436 error("Specify either a list, or --all, not both!");
437 usage_with_options(name_rev_usage, opts);
439 if (all || transform_stdin)
442 for (; argc; argc--, argv++) {
443 struct object_id oid;
444 struct object *object;
445 struct commit *commit;
447 if (get_oid(*argv, &oid)) {
448 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
454 object = parse_object(the_repository, &oid);
456 struct object *peeled = deref_tag(the_repository,
458 if (peeled && peeled->type == OBJ_COMMIT)
459 commit = (struct commit *)peeled;
463 fprintf(stderr, "Could not get object for %s. Skipping.\n",
469 if (cutoff > commit->date)
470 cutoff = commit->date;
475 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
479 object = (struct object *)commit;
481 add_object_array(object, *argv, &revs);
485 cutoff = cutoff - CUTOFF_DATE_SLOP;
486 for_each_ref(name_ref, &data);
488 if (transform_stdin) {
491 while (!feof(stdin)) {
492 char *p = fgets(buffer, sizeof(buffer), stdin);
495 name_rev_line(p, &data);
500 max = get_max_object_index();
501 for (i = 0; i < max; i++) {
502 struct object *obj = get_indexed_object(i);
503 if (!obj || obj->type != OBJ_COMMIT)
506 always, allow_undefined, data.name_only);
510 for (i = 0; i < revs.nr; i++)
511 show_name(revs.objects[i].item, revs.objects[i].name,
512 always, allow_undefined, data.name_only);