name-rev: extend --refs to accept multiple patterns
[git/debian.git] / builtin / name-rev.c
blob9da027674ca85b5cc0a4c2ceb05f4ad3635a0c76
1 #include "builtin.h"
2 #include "cache.h"
3 #include "commit.h"
4 #include "tag.h"
5 #include "refs.h"
6 #include "parse-options.h"
7 #include "sha1-lookup.h"
9 #define CUTOFF_DATE_SLOP 86400 /* one day */
11 typedef struct rev_name {
12 const char *tip_name;
13 unsigned long taggerdate;
14 int generation;
15 int distance;
16 } rev_name;
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,
26 int deref)
28 struct rev_name *name = (struct rev_name *)commit->util;
29 struct commit_list *parents;
30 int parent_number = 1;
32 parse_commit(commit);
34 if (commit->date < cutoff)
35 return;
37 if (deref) {
38 tip_name = xstrfmt("%s^0", tip_name);
40 if (generation)
41 die("generation: %d, but deref?", generation);
44 if (name == NULL) {
45 name = xmalloc(sizeof(rev_name));
46 commit->util = name;
47 goto copy_data;
48 } else if (name->taggerdate > taggerdate ||
49 (name->taggerdate == taggerdate &&
50 name->distance > distance)) {
51 copy_data:
52 name->tip_name = tip_name;
53 name->taggerdate = taggerdate;
54 name->generation = generation;
55 name->distance = distance;
56 } else
57 return;
59 for (parents = commit->parents;
60 parents;
61 parents = parents->next, parent_number++) {
62 if (parent_number > 1) {
63 size_t len;
64 char *new_name;
66 strip_suffix(tip_name, "^0", &len);
67 if (generation > 0)
68 new_name = xstrfmt("%.*s~%d^%d", (int)len, tip_name,
69 generation, parent_number);
70 else
71 new_name = xstrfmt("%.*s^%d", (int)len, tip_name,
72 parent_number);
74 name_rev(parents->item, new_name, taggerdate, 0,
75 distance + MERGE_TRAVERSAL_WEIGHT, 0);
76 } else {
77 name_rev(parents->item, tip_name, taggerdate,
78 generation + 1, distance + 1, 0);
83 static int subpath_matches(const char *path, const char *filter)
85 const char *subpath = path;
87 while (subpath) {
88 if (!wildmatch(filter, subpath, 0, NULL))
89 return subpath - path;
90 subpath = strchr(subpath, '/');
91 if (subpath)
92 subpath++;
94 return -1;
97 static const char *name_ref_abbrev(const char *refname, int shorten_unambiguous)
99 if (shorten_unambiguous)
100 refname = shorten_unambiguous_ref(refname, 0);
101 else if (starts_with(refname, "refs/heads/"))
102 refname = refname + 11;
103 else if (starts_with(refname, "refs/"))
104 refname = refname + 5;
105 return refname;
108 struct name_ref_data {
109 int tags_only;
110 int name_only;
111 struct string_list ref_filters;
114 static struct tip_table {
115 struct tip_table_entry {
116 unsigned char sha1[20];
117 const char *refname;
118 } *table;
119 int nr;
120 int alloc;
121 int sorted;
122 } tip_table;
124 static void add_to_tip_table(const unsigned char *sha1, const char *refname,
125 int shorten_unambiguous)
127 refname = name_ref_abbrev(refname, shorten_unambiguous);
129 ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc);
130 hashcpy(tip_table.table[tip_table.nr].sha1, sha1);
131 tip_table.table[tip_table.nr].refname = xstrdup(refname);
132 tip_table.nr++;
133 tip_table.sorted = 0;
136 static int tipcmp(const void *a_, const void *b_)
138 const struct tip_table_entry *a = a_, *b = b_;
139 return hashcmp(a->sha1, b->sha1);
142 static int name_ref(const char *path, const struct object_id *oid, int flags, void *cb_data)
144 struct object *o = parse_object(oid->hash);
145 struct name_ref_data *data = cb_data;
146 int can_abbreviate_output = data->tags_only && data->name_only;
147 int deref = 0;
148 unsigned long taggerdate = ULONG_MAX;
150 if (data->tags_only && !starts_with(path, "refs/tags/"))
151 return 0;
153 if (data->ref_filters.nr) {
154 struct string_list_item *item;
155 int matched = 0;
157 /* See if any of the patterns match. */
158 for_each_string_list_item(item, &data->ref_filters) {
160 * Check all patterns even after finding a match, so
161 * that we can see if a match with a subpath exists.
162 * When a user asked for 'refs/tags/v*' and 'v1.*',
163 * both of which match, the user is showing her
164 * willingness to accept a shortened output by having
165 * the 'v1.*' in the acceptable refnames, so we
166 * shouldn't stop when seeing 'refs/tags/v1.4' matches
167 * 'refs/tags/v*'. We should show it as 'v1.4'.
169 switch (subpath_matches(path, item->string)) {
170 case -1: /* did not match */
171 break;
172 case 0: /* matched fully */
173 matched = 1;
174 break;
175 default: /* matched subpath */
176 matched = 1;
177 can_abbreviate_output = 1;
178 break;
182 /* If none of the patterns matched, stop now */
183 if (!matched)
184 return 0;
187 add_to_tip_table(oid->hash, path, can_abbreviate_output);
189 while (o && o->type == OBJ_TAG) {
190 struct tag *t = (struct tag *) o;
191 if (!t->tagged)
192 break; /* broken repository */
193 o = parse_object(t->tagged->oid.hash);
194 deref = 1;
195 taggerdate = t->date;
197 if (o && o->type == OBJ_COMMIT) {
198 struct commit *commit = (struct commit *)o;
200 path = name_ref_abbrev(path, can_abbreviate_output);
201 name_rev(commit, xstrdup(path), taggerdate, 0, 0, deref);
203 return 0;
206 static const unsigned char *nth_tip_table_ent(size_t ix, void *table_)
208 struct tip_table_entry *table = table_;
209 return table[ix].sha1;
212 static const char *get_exact_ref_match(const struct object *o)
214 int found;
216 if (!tip_table.table || !tip_table.nr)
217 return NULL;
219 if (!tip_table.sorted) {
220 QSORT(tip_table.table, tip_table.nr, tipcmp);
221 tip_table.sorted = 1;
224 found = sha1_pos(o->oid.hash, tip_table.table, tip_table.nr,
225 nth_tip_table_ent);
226 if (0 <= found)
227 return tip_table.table[found].refname;
228 return NULL;
231 /* returns a static buffer */
232 static const char *get_rev_name(const struct object *o)
234 static char buffer[1024];
235 struct rev_name *n;
236 struct commit *c;
238 if (o->type != OBJ_COMMIT)
239 return get_exact_ref_match(o);
240 c = (struct commit *) o;
241 n = c->util;
242 if (!n)
243 return NULL;
245 if (!n->generation)
246 return n->tip_name;
247 else {
248 int len = strlen(n->tip_name);
249 if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
250 len -= 2;
251 snprintf(buffer, sizeof(buffer), "%.*s~%d", len, n->tip_name,
252 n->generation);
254 return buffer;
258 static void show_name(const struct object *obj,
259 const char *caller_name,
260 int always, int allow_undefined, int name_only)
262 const char *name;
263 const struct object_id *oid = &obj->oid;
265 if (!name_only)
266 printf("%s ", caller_name ? caller_name : oid_to_hex(oid));
267 name = get_rev_name(obj);
268 if (name)
269 printf("%s\n", name);
270 else if (allow_undefined)
271 printf("undefined\n");
272 else if (always)
273 printf("%s\n", find_unique_abbrev(oid->hash, DEFAULT_ABBREV));
274 else
275 die("cannot describe '%s'", oid_to_hex(oid));
278 static char const * const name_rev_usage[] = {
279 N_("git name-rev [<options>] <commit>..."),
280 N_("git name-rev [<options>] --all"),
281 N_("git name-rev [<options>] --stdin"),
282 NULL
285 static void name_rev_line(char *p, struct name_ref_data *data)
287 int forty = 0;
288 char *p_start;
289 for (p_start = p; *p; p++) {
290 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
291 if (!ishex(*p))
292 forty = 0;
293 else if (++forty == 40 &&
294 !ishex(*(p+1))) {
295 unsigned char sha1[40];
296 const char *name = NULL;
297 char c = *(p+1);
298 int p_len = p - p_start + 1;
300 forty = 0;
302 *(p+1) = 0;
303 if (!get_sha1(p - 39, sha1)) {
304 struct object *o =
305 lookup_object(sha1);
306 if (o)
307 name = get_rev_name(o);
309 *(p+1) = c;
311 if (!name)
312 continue;
314 if (data->name_only)
315 printf("%.*s%s", p_len - 40, p_start, name);
316 else
317 printf("%.*s (%s)", p_len, p_start, name);
318 p_start = p + 1;
322 /* flush */
323 if (p_start != p)
324 fwrite(p_start, p - p_start, 1, stdout);
327 int cmd_name_rev(int argc, const char **argv, const char *prefix)
329 struct object_array revs = OBJECT_ARRAY_INIT;
330 int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
331 struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP };
332 struct option opts[] = {
333 OPT_BOOL(0, "name-only", &data.name_only, N_("print only names (no SHA-1)")),
334 OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")),
335 OPT_STRING_LIST(0, "refs", &data.ref_filters, N_("pattern"),
336 N_("only use refs matching <pattern>")),
337 OPT_GROUP(""),
338 OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")),
339 OPT_BOOL(0, "stdin", &transform_stdin, N_("read from stdin")),
340 OPT_BOOL(0, "undefined", &allow_undefined, N_("allow to print `undefined` names (default)")),
341 OPT_BOOL(0, "always", &always,
342 N_("show abbreviated commit object as fallback")),
344 /* A Hidden OPT_BOOL */
345 OPTION_SET_INT, 0, "peel-tag", &peel_tag, NULL,
346 N_("dereference tags in the input (internal use)"),
347 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1,
349 OPT_END(),
352 git_config(git_default_config, NULL);
353 argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0);
354 if (all + transform_stdin + !!argc > 1) {
355 error("Specify either a list, or --all, not both!");
356 usage_with_options(name_rev_usage, opts);
358 if (all || transform_stdin)
359 cutoff = 0;
361 for (; argc; argc--, argv++) {
362 unsigned char sha1[20];
363 struct object *object;
364 struct commit *commit;
366 if (get_sha1(*argv, sha1)) {
367 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
368 *argv);
369 continue;
372 commit = NULL;
373 object = parse_object(sha1);
374 if (object) {
375 struct object *peeled = deref_tag(object, *argv, 0);
376 if (peeled && peeled->type == OBJ_COMMIT)
377 commit = (struct commit *)peeled;
380 if (!object) {
381 fprintf(stderr, "Could not get object for %s. Skipping.\n",
382 *argv);
383 continue;
386 if (commit) {
387 if (cutoff > commit->date)
388 cutoff = commit->date;
391 if (peel_tag) {
392 if (!commit) {
393 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
394 *argv);
395 continue;
397 object = (struct object *)commit;
399 add_object_array(object, *argv, &revs);
402 if (cutoff)
403 cutoff = cutoff - CUTOFF_DATE_SLOP;
404 for_each_ref(name_ref, &data);
406 if (transform_stdin) {
407 char buffer[2048];
409 while (!feof(stdin)) {
410 char *p = fgets(buffer, sizeof(buffer), stdin);
411 if (!p)
412 break;
413 name_rev_line(p, &data);
415 } else if (all) {
416 int i, max;
418 max = get_max_object_index();
419 for (i = 0; i < max; i++) {
420 struct object *obj = get_indexed_object(i);
421 if (!obj || obj->type != OBJ_COMMIT)
422 continue;
423 show_name(obj, NULL,
424 always, allow_undefined, data.name_only);
426 } else {
427 int i;
428 for (i = 0; i < revs.nr; i++)
429 show_name(revs.objects[i].item, revs.objects[i].name,
430 always, allow_undefined, data.name_only);
433 return 0;