Merge branch 'da/smerge'
[git/raj.git] / builtin / name-rev.c
blobf1cb45c22746bbcfb76d8fb1fefd5061f582cf57
1 #include "builtin.h"
2 #include "cache.h"
3 #include "repository.h"
4 #include "config.h"
5 #include "commit.h"
6 #include "tag.h"
7 #include "refs.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 {
15 const char *tip_name;
16 timestamp_t taggerdate;
17 int generation;
18 int distance;
19 int from_tag;
20 } rev_name;
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 const char *tip_name,
44 timestamp_t taggerdate,
45 int generation,
46 int distance,
47 int from_tag)
50 * When comparing names based on tags, prefer names
51 * based on the older tag, even if it is farther away.
53 if (from_tag && name->from_tag)
54 return (name->taggerdate > taggerdate ||
55 (name->taggerdate == taggerdate &&
56 name->distance > distance));
59 * We know that at least one of them is a non-tag at this point.
60 * favor a tag over a non-tag.
62 if (name->from_tag != from_tag)
63 return from_tag;
66 * We are now looking at two non-tags. Tiebreak to favor
67 * shorter hops.
69 if (name->distance != distance)
70 return name->distance > distance;
72 /* ... or tiebreak to favor older date */
73 if (name->taggerdate != taggerdate)
74 return name->taggerdate > taggerdate;
76 /* keep the current one if we cannot decide */
77 return 0;
80 static void name_rev(struct commit *commit,
81 const char *tip_name, timestamp_t taggerdate,
82 int generation, int distance, int from_tag,
83 int deref)
85 struct rev_name *name = get_commit_rev_name(commit);
86 struct commit_list *parents;
87 int parent_number = 1;
88 char *to_free = NULL;
90 parse_commit(commit);
92 if (commit->date < cutoff)
93 return;
95 if (deref) {
96 tip_name = to_free = xstrfmt("%s^0", tip_name);
98 if (generation)
99 die("generation: %d, but deref?", generation);
102 if (name == NULL) {
103 name = xmalloc(sizeof(rev_name));
104 set_commit_rev_name(commit, name);
105 goto copy_data;
106 } else if (is_better_name(name, tip_name, taggerdate,
107 generation, distance, from_tag)) {
108 copy_data:
109 name->tip_name = tip_name;
110 name->taggerdate = taggerdate;
111 name->generation = generation;
112 name->distance = distance;
113 name->from_tag = from_tag;
114 } else {
115 free(to_free);
116 return;
119 for (parents = commit->parents;
120 parents;
121 parents = parents->next, parent_number++) {
122 if (parent_number > 1) {
123 size_t len;
124 char *new_name;
126 strip_suffix(tip_name, "^0", &len);
127 if (generation > 0)
128 new_name = xstrfmt("%.*s~%d^%d", (int)len, tip_name,
129 generation, parent_number);
130 else
131 new_name = xstrfmt("%.*s^%d", (int)len, tip_name,
132 parent_number);
134 name_rev(parents->item, new_name, taggerdate, 0,
135 distance + MERGE_TRAVERSAL_WEIGHT,
136 from_tag, 0);
137 } else {
138 name_rev(parents->item, tip_name, taggerdate,
139 generation + 1, distance + 1,
140 from_tag, 0);
145 static int subpath_matches(const char *path, const char *filter)
147 const char *subpath = path;
149 while (subpath) {
150 if (!wildmatch(filter, subpath, 0))
151 return subpath - path;
152 subpath = strchr(subpath, '/');
153 if (subpath)
154 subpath++;
156 return -1;
159 static const char *name_ref_abbrev(const char *refname, int shorten_unambiguous)
161 if (shorten_unambiguous)
162 refname = shorten_unambiguous_ref(refname, 0);
163 else if (starts_with(refname, "refs/heads/"))
164 refname = refname + 11;
165 else if (starts_with(refname, "refs/"))
166 refname = refname + 5;
167 return refname;
170 struct name_ref_data {
171 int tags_only;
172 int name_only;
173 struct string_list ref_filters;
174 struct string_list exclude_filters;
177 static struct tip_table {
178 struct tip_table_entry {
179 struct object_id oid;
180 const char *refname;
181 } *table;
182 int nr;
183 int alloc;
184 int sorted;
185 } tip_table;
187 static void add_to_tip_table(const struct object_id *oid, const char *refname,
188 int shorten_unambiguous)
190 refname = name_ref_abbrev(refname, shorten_unambiguous);
192 ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc);
193 oidcpy(&tip_table.table[tip_table.nr].oid, oid);
194 tip_table.table[tip_table.nr].refname = xstrdup(refname);
195 tip_table.nr++;
196 tip_table.sorted = 0;
199 static int tipcmp(const void *a_, const void *b_)
201 const struct tip_table_entry *a = a_, *b = b_;
202 return oidcmp(&a->oid, &b->oid);
205 static int name_ref(const char *path, const struct object_id *oid, int flags, void *cb_data)
207 struct object *o = parse_object(the_repository, oid);
208 struct name_ref_data *data = cb_data;
209 int can_abbreviate_output = data->tags_only && data->name_only;
210 int deref = 0;
211 timestamp_t taggerdate = TIME_MAX;
213 if (data->tags_only && !starts_with(path, "refs/tags/"))
214 return 0;
216 if (data->exclude_filters.nr) {
217 struct string_list_item *item;
219 for_each_string_list_item(item, &data->exclude_filters) {
220 if (subpath_matches(path, item->string) >= 0)
221 return 0;
225 if (data->ref_filters.nr) {
226 struct string_list_item *item;
227 int matched = 0;
229 /* See if any of the patterns match. */
230 for_each_string_list_item(item, &data->ref_filters) {
232 * Check all patterns even after finding a match, so
233 * that we can see if a match with a subpath exists.
234 * When a user asked for 'refs/tags/v*' and 'v1.*',
235 * both of which match, the user is showing her
236 * willingness to accept a shortened output by having
237 * the 'v1.*' in the acceptable refnames, so we
238 * shouldn't stop when seeing 'refs/tags/v1.4' matches
239 * 'refs/tags/v*'. We should show it as 'v1.4'.
241 switch (subpath_matches(path, item->string)) {
242 case -1: /* did not match */
243 break;
244 case 0: /* matched fully */
245 matched = 1;
246 break;
247 default: /* matched subpath */
248 matched = 1;
249 can_abbreviate_output = 1;
250 break;
254 /* If none of the patterns matched, stop now */
255 if (!matched)
256 return 0;
259 add_to_tip_table(oid, path, can_abbreviate_output);
261 while (o && o->type == OBJ_TAG) {
262 struct tag *t = (struct tag *) o;
263 if (!t->tagged)
264 break; /* broken repository */
265 o = parse_object(the_repository, &t->tagged->oid);
266 deref = 1;
267 taggerdate = t->date;
269 if (o && o->type == OBJ_COMMIT) {
270 struct commit *commit = (struct commit *)o;
271 int from_tag = starts_with(path, "refs/tags/");
273 if (taggerdate == TIME_MAX)
274 taggerdate = ((struct commit *)o)->date;
275 path = name_ref_abbrev(path, can_abbreviate_output);
276 name_rev(commit, xstrdup(path), taggerdate, 0, 0,
277 from_tag, deref);
279 return 0;
282 static const unsigned char *nth_tip_table_ent(size_t ix, void *table_)
284 struct tip_table_entry *table = table_;
285 return table[ix].oid.hash;
288 static const char *get_exact_ref_match(const struct object *o)
290 int found;
292 if (!tip_table.table || !tip_table.nr)
293 return NULL;
295 if (!tip_table.sorted) {
296 QSORT(tip_table.table, tip_table.nr, tipcmp);
297 tip_table.sorted = 1;
300 found = sha1_pos(o->oid.hash, tip_table.table, tip_table.nr,
301 nth_tip_table_ent);
302 if (0 <= found)
303 return tip_table.table[found].refname;
304 return NULL;
307 /* may return a constant string or use "buf" as scratch space */
308 static const char *get_rev_name(const struct object *o, struct strbuf *buf)
310 struct rev_name *n;
311 struct commit *c;
313 if (o->type != OBJ_COMMIT)
314 return get_exact_ref_match(o);
315 c = (struct commit *) o;
316 n = get_commit_rev_name(c);
317 if (!n)
318 return NULL;
320 if (!n->generation)
321 return n->tip_name;
322 else {
323 int len = strlen(n->tip_name);
324 if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
325 len -= 2;
326 strbuf_reset(buf);
327 strbuf_addf(buf, "%.*s~%d", len, n->tip_name, n->generation);
328 return buf->buf;
332 static void show_name(const struct object *obj,
333 const char *caller_name,
334 int always, int allow_undefined, int name_only)
336 const char *name;
337 const struct object_id *oid = &obj->oid;
338 struct strbuf buf = STRBUF_INIT;
340 if (!name_only)
341 printf("%s ", caller_name ? caller_name : oid_to_hex(oid));
342 name = get_rev_name(obj, &buf);
343 if (name)
344 printf("%s\n", name);
345 else if (allow_undefined)
346 printf("undefined\n");
347 else if (always)
348 printf("%s\n", find_unique_abbrev(oid, DEFAULT_ABBREV));
349 else
350 die("cannot describe '%s'", oid_to_hex(oid));
351 strbuf_release(&buf);
354 static char const * const name_rev_usage[] = {
355 N_("git name-rev [<options>] <commit>..."),
356 N_("git name-rev [<options>] --all"),
357 N_("git name-rev [<options>] --stdin"),
358 NULL
361 static void name_rev_line(char *p, struct name_ref_data *data)
363 struct strbuf buf = STRBUF_INIT;
364 int forty = 0;
365 char *p_start;
366 for (p_start = p; *p; p++) {
367 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
368 if (!ishex(*p))
369 forty = 0;
370 else if (++forty == GIT_SHA1_HEXSZ &&
371 !ishex(*(p+1))) {
372 struct object_id oid;
373 const char *name = NULL;
374 char c = *(p+1);
375 int p_len = p - p_start + 1;
377 forty = 0;
379 *(p+1) = 0;
380 if (!get_oid(p - (GIT_SHA1_HEXSZ - 1), &oid)) {
381 struct object *o =
382 lookup_object(the_repository,
383 oid.hash);
384 if (o)
385 name = get_rev_name(o, &buf);
387 *(p+1) = c;
389 if (!name)
390 continue;
392 if (data->name_only)
393 printf("%.*s%s", p_len - GIT_SHA1_HEXSZ, p_start, name);
394 else
395 printf("%.*s (%s)", p_len, p_start, name);
396 p_start = p + 1;
400 /* flush */
401 if (p_start != p)
402 fwrite(p_start, p - p_start, 1, stdout);
404 strbuf_release(&buf);
407 int cmd_name_rev(int argc, const char **argv, const char *prefix)
409 struct object_array revs = OBJECT_ARRAY_INIT;
410 int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
411 struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP };
412 struct option opts[] = {
413 OPT_BOOL(0, "name-only", &data.name_only, N_("print only names (no SHA-1)")),
414 OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")),
415 OPT_STRING_LIST(0, "refs", &data.ref_filters, N_("pattern"),
416 N_("only use refs matching <pattern>")),
417 OPT_STRING_LIST(0, "exclude", &data.exclude_filters, N_("pattern"),
418 N_("ignore refs matching <pattern>")),
419 OPT_GROUP(""),
420 OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")),
421 OPT_BOOL(0, "stdin", &transform_stdin, N_("read from stdin")),
422 OPT_BOOL(0, "undefined", &allow_undefined, N_("allow to print `undefined` names (default)")),
423 OPT_BOOL(0, "always", &always,
424 N_("show abbreviated commit object as fallback")),
426 /* A Hidden OPT_BOOL */
427 OPTION_SET_INT, 0, "peel-tag", &peel_tag, NULL,
428 N_("dereference tags in the input (internal use)"),
429 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1,
431 OPT_END(),
434 init_commit_rev_name(&rev_names);
435 git_config(git_default_config, NULL);
436 argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0);
437 if (all + transform_stdin + !!argc > 1) {
438 error("Specify either a list, or --all, not both!");
439 usage_with_options(name_rev_usage, opts);
441 if (all || transform_stdin)
442 cutoff = 0;
444 for (; argc; argc--, argv++) {
445 struct object_id oid;
446 struct object *object;
447 struct commit *commit;
449 if (get_oid(*argv, &oid)) {
450 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
451 *argv);
452 continue;
455 commit = NULL;
456 object = parse_object(the_repository, &oid);
457 if (object) {
458 struct object *peeled = deref_tag(the_repository,
459 object, *argv, 0);
460 if (peeled && peeled->type == OBJ_COMMIT)
461 commit = (struct commit *)peeled;
464 if (!object) {
465 fprintf(stderr, "Could not get object for %s. Skipping.\n",
466 *argv);
467 continue;
470 if (commit) {
471 if (cutoff > commit->date)
472 cutoff = commit->date;
475 if (peel_tag) {
476 if (!commit) {
477 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
478 *argv);
479 continue;
481 object = (struct object *)commit;
483 add_object_array(object, *argv, &revs);
486 if (cutoff)
487 cutoff = cutoff - CUTOFF_DATE_SLOP;
488 for_each_ref(name_ref, &data);
490 if (transform_stdin) {
491 char buffer[2048];
493 while (!feof(stdin)) {
494 char *p = fgets(buffer, sizeof(buffer), stdin);
495 if (!p)
496 break;
497 name_rev_line(p, &data);
499 } else if (all) {
500 int i, max;
502 max = get_max_object_index();
503 for (i = 0; i < max; i++) {
504 struct object *obj = get_indexed_object(i);
505 if (!obj || obj->type != OBJ_COMMIT)
506 continue;
507 show_name(obj, NULL,
508 always, allow_undefined, data.name_only);
510 } else {
511 int i;
512 for (i = 0; i < revs.nr; i++)
513 show_name(revs.objects[i].item, revs.objects[i].name,
514 always, allow_undefined, data.name_only);
517 UNLEAK(revs);
518 return 0;