6 #include "parse-options.h"
7 #include "sha1-lookup.h"
9 #define CUTOFF_DATE_SLOP 86400 /* one day */
11 typedef struct rev_name {
13 unsigned long taggerdate;
18 static long cutoff = LONG_MAX;
20 /* How many generations are maximally preferred over _one_ merge traversal? */
21 #define MERGE_TRAVERSAL_WEIGHT 65535
23 static void name_rev(struct commit *commit,
24 const char *tip_name, unsigned long taggerdate,
25 int generation, int distance,
28 struct rev_name *name = (struct rev_name *)commit->util;
29 struct commit_list *parents;
30 int parent_number = 1;
35 if (commit->date < cutoff)
39 tip_name = to_free = xstrfmt("%s^0", tip_name);
42 die("generation: %d, but deref?", generation);
46 name = xmalloc(sizeof(rev_name));
49 } else if (name->taggerdate > taggerdate ||
50 (name->taggerdate == taggerdate &&
51 name->distance > distance)) {
53 name->tip_name = tip_name;
54 name->taggerdate = taggerdate;
55 name->generation = generation;
56 name->distance = distance;
62 for (parents = commit->parents;
64 parents = parents->next, parent_number++) {
65 if (parent_number > 1) {
69 strip_suffix(tip_name, "^0", &len);
71 new_name = xstrfmt("%.*s~%d^%d", (int)len, tip_name,
72 generation, parent_number);
74 new_name = xstrfmt("%.*s^%d", (int)len, tip_name,
77 name_rev(parents->item, new_name, taggerdate, 0,
78 distance + MERGE_TRAVERSAL_WEIGHT, 0);
80 name_rev(parents->item, tip_name, taggerdate,
81 generation + 1, distance + 1, 0);
86 static int subpath_matches(const char *path, const char *filter)
88 const char *subpath = path;
91 if (!wildmatch(filter, subpath, 0, NULL))
92 return subpath - path;
93 subpath = strchr(subpath, '/');
100 static const char *name_ref_abbrev(const char *refname, int shorten_unambiguous)
102 if (shorten_unambiguous)
103 refname = shorten_unambiguous_ref(refname, 0);
104 else if (starts_with(refname, "refs/heads/"))
105 refname = refname + 11;
106 else if (starts_with(refname, "refs/"))
107 refname = refname + 5;
111 struct name_ref_data {
114 struct string_list ref_filters;
115 struct string_list exclude_filters;
118 static struct tip_table {
119 struct tip_table_entry {
120 unsigned char sha1[20];
128 static void add_to_tip_table(const unsigned char *sha1, const char *refname,
129 int shorten_unambiguous)
131 refname = name_ref_abbrev(refname, shorten_unambiguous);
133 ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc);
134 hashcpy(tip_table.table[tip_table.nr].sha1, sha1);
135 tip_table.table[tip_table.nr].refname = xstrdup(refname);
137 tip_table.sorted = 0;
140 static int tipcmp(const void *a_, const void *b_)
142 const struct tip_table_entry *a = a_, *b = b_;
143 return hashcmp(a->sha1, b->sha1);
146 static int name_ref(const char *path, const struct object_id *oid, int flags, void *cb_data)
148 struct object *o = parse_object(oid->hash);
149 struct name_ref_data *data = cb_data;
150 int can_abbreviate_output = data->tags_only && data->name_only;
152 unsigned long taggerdate = ULONG_MAX;
154 if (data->tags_only && !starts_with(path, "refs/tags/"))
157 if (data->exclude_filters.nr) {
158 struct string_list_item *item;
160 for_each_string_list_item(item, &data->exclude_filters) {
161 if (subpath_matches(path, item->string) >= 0)
166 if (data->ref_filters.nr) {
167 struct string_list_item *item;
170 /* See if any of the patterns match. */
171 for_each_string_list_item(item, &data->ref_filters) {
173 * Check all patterns even after finding a match, so
174 * that we can see if a match with a subpath exists.
175 * When a user asked for 'refs/tags/v*' and 'v1.*',
176 * both of which match, the user is showing her
177 * willingness to accept a shortened output by having
178 * the 'v1.*' in the acceptable refnames, so we
179 * shouldn't stop when seeing 'refs/tags/v1.4' matches
180 * 'refs/tags/v*'. We should show it as 'v1.4'.
182 switch (subpath_matches(path, item->string)) {
183 case -1: /* did not match */
185 case 0: /* matched fully */
188 default: /* matched subpath */
190 can_abbreviate_output = 1;
195 /* If none of the patterns matched, stop now */
200 add_to_tip_table(oid->hash, path, can_abbreviate_output);
202 while (o && o->type == OBJ_TAG) {
203 struct tag *t = (struct tag *) o;
205 break; /* broken repository */
206 o = parse_object(t->tagged->oid.hash);
208 taggerdate = t->date;
210 if (o && o->type == OBJ_COMMIT) {
211 struct commit *commit = (struct commit *)o;
213 path = name_ref_abbrev(path, can_abbreviate_output);
214 name_rev(commit, xstrdup(path), taggerdate, 0, 0, deref);
219 static const unsigned char *nth_tip_table_ent(size_t ix, void *table_)
221 struct tip_table_entry *table = table_;
222 return table[ix].sha1;
225 static const char *get_exact_ref_match(const struct object *o)
229 if (!tip_table.table || !tip_table.nr)
232 if (!tip_table.sorted) {
233 QSORT(tip_table.table, tip_table.nr, tipcmp);
234 tip_table.sorted = 1;
237 found = sha1_pos(o->oid.hash, tip_table.table, tip_table.nr,
240 return tip_table.table[found].refname;
244 /* may return a constant string or use "buf" as scratch space */
245 static const char *get_rev_name(const struct object *o, struct strbuf *buf)
250 if (o->type != OBJ_COMMIT)
251 return get_exact_ref_match(o);
252 c = (struct commit *) o;
260 int len = strlen(n->tip_name);
261 if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
264 strbuf_addf(buf, "%.*s~%d", len, n->tip_name, n->generation);
269 static void show_name(const struct object *obj,
270 const char *caller_name,
271 int always, int allow_undefined, int name_only)
274 const struct object_id *oid = &obj->oid;
275 struct strbuf buf = STRBUF_INIT;
278 printf("%s ", caller_name ? caller_name : oid_to_hex(oid));
279 name = get_rev_name(obj, &buf);
281 printf("%s\n", name);
282 else if (allow_undefined)
283 printf("undefined\n");
285 printf("%s\n", find_unique_abbrev(oid->hash, DEFAULT_ABBREV));
287 die("cannot describe '%s'", oid_to_hex(oid));
288 strbuf_release(&buf);
291 static char const * const name_rev_usage[] = {
292 N_("git name-rev [<options>] <commit>..."),
293 N_("git name-rev [<options>] --all"),
294 N_("git name-rev [<options>] --stdin"),
298 static void name_rev_line(char *p, struct name_ref_data *data)
300 struct strbuf buf = STRBUF_INIT;
303 for (p_start = p; *p; p++) {
304 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
307 else if (++forty == 40 &&
309 unsigned char sha1[40];
310 const char *name = NULL;
312 int p_len = p - p_start + 1;
317 if (!get_sha1(p - 39, sha1)) {
321 name = get_rev_name(o, &buf);
329 printf("%.*s%s", p_len - 40, p_start, name);
331 printf("%.*s (%s)", p_len, p_start, name);
338 fwrite(p_start, p - p_start, 1, stdout);
340 strbuf_release(&buf);
343 int cmd_name_rev(int argc, const char **argv, const char *prefix)
345 struct object_array revs = OBJECT_ARRAY_INIT;
346 int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
347 struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP };
348 struct option opts[] = {
349 OPT_BOOL(0, "name-only", &data.name_only, N_("print only names (no SHA-1)")),
350 OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")),
351 OPT_STRING_LIST(0, "refs", &data.ref_filters, N_("pattern"),
352 N_("only use refs matching <pattern>")),
353 OPT_STRING_LIST(0, "exclude", &data.exclude_filters, N_("pattern"),
354 N_("ignore refs matching <pattern>")),
356 OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")),
357 OPT_BOOL(0, "stdin", &transform_stdin, N_("read from stdin")),
358 OPT_BOOL(0, "undefined", &allow_undefined, N_("allow to print `undefined` names (default)")),
359 OPT_BOOL(0, "always", &always,
360 N_("show abbreviated commit object as fallback")),
362 /* A Hidden OPT_BOOL */
363 OPTION_SET_INT, 0, "peel-tag", &peel_tag, NULL,
364 N_("dereference tags in the input (internal use)"),
365 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1,
370 git_config(git_default_config, NULL);
371 argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0);
372 if (all + transform_stdin + !!argc > 1) {
373 error("Specify either a list, or --all, not both!");
374 usage_with_options(name_rev_usage, opts);
376 if (all || transform_stdin)
379 for (; argc; argc--, argv++) {
380 unsigned char sha1[20];
381 struct object *object;
382 struct commit *commit;
384 if (get_sha1(*argv, sha1)) {
385 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
391 object = parse_object(sha1);
393 struct object *peeled = deref_tag(object, *argv, 0);
394 if (peeled && peeled->type == OBJ_COMMIT)
395 commit = (struct commit *)peeled;
399 fprintf(stderr, "Could not get object for %s. Skipping.\n",
405 if (cutoff > commit->date)
406 cutoff = commit->date;
411 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
415 object = (struct object *)commit;
417 add_object_array(object, *argv, &revs);
421 cutoff = cutoff - CUTOFF_DATE_SLOP;
422 for_each_ref(name_ref, &data);
424 if (transform_stdin) {
427 while (!feof(stdin)) {
428 char *p = fgets(buffer, sizeof(buffer), stdin);
431 name_rev_line(p, &data);
436 max = get_max_object_index();
437 for (i = 0; i < max; i++) {
438 struct object *obj = get_indexed_object(i);
439 if (!obj || obj->type != OBJ_COMMIT)
442 always, allow_undefined, data.name_only);
446 for (i = 0; i < revs.nr; i++)
447 show_name(revs.objects[i].item, revs.objects[i].name,
448 always, allow_undefined, data.name_only);