Merge branch 'yk/filter-branch-non-committish-refs'
[git] / builtin / name-rev.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "commit.h"
5 #include "tag.h"
6 #include "refs.h"
7 #include "parse-options.h"
8 #include "sha1-lookup.h"
9
10 #define CUTOFF_DATE_SLOP 86400 /* one day */
11
12 typedef struct rev_name {
13         const char *tip_name;
14         timestamp_t taggerdate;
15         int generation;
16         int distance;
17         int from_tag;
18 } rev_name;
19
20 static timestamp_t cutoff = TIME_MAX;
21
22 /* How many generations are maximally preferred over _one_ merge traversal? */
23 #define MERGE_TRAVERSAL_WEIGHT 65535
24
25 static int is_better_name(struct rev_name *name,
26                           const char *tip_name,
27                           timestamp_t taggerdate,
28                           int generation,
29                           int distance,
30                           int from_tag)
31 {
32         /*
33          * When comparing names based on tags, prefer names
34          * based on the older tag, even if it is farther away.
35          */
36         if (from_tag && name->from_tag)
37                 return (name->taggerdate > taggerdate ||
38                         (name->taggerdate == taggerdate &&
39                          name->distance > distance));
40
41         /*
42          * We know that at least one of them is a non-tag at this point.
43          * favor a tag over a non-tag.
44          */
45         if (name->from_tag != from_tag)
46                 return from_tag;
47
48         /*
49          * We are now looking at two non-tags.  Tiebreak to favor
50          * shorter hops.
51          */
52         if (name->distance != distance)
53                 return name->distance > distance;
54
55         /* ... or tiebreak to favor older date */
56         if (name->taggerdate != taggerdate)
57                 return name->taggerdate > taggerdate;
58
59         /* keep the current one if we cannot decide */
60         return 0;
61 }
62
63 static void name_rev(struct commit *commit,
64                 const char *tip_name, timestamp_t taggerdate,
65                 int generation, int distance, int from_tag,
66                 int deref)
67 {
68         struct rev_name *name = (struct rev_name *)commit->util;
69         struct commit_list *parents;
70         int parent_number = 1;
71         char *to_free = NULL;
72
73         parse_commit(commit);
74
75         if (commit->date < cutoff)
76                 return;
77
78         if (deref) {
79                 tip_name = to_free = xstrfmt("%s^0", tip_name);
80
81                 if (generation)
82                         die("generation: %d, but deref?", generation);
83         }
84
85         if (name == NULL) {
86                 name = xmalloc(sizeof(rev_name));
87                 commit->util = name;
88                 goto copy_data;
89         } else if (is_better_name(name, tip_name, taggerdate,
90                                   generation, distance, from_tag)) {
91 copy_data:
92                 name->tip_name = tip_name;
93                 name->taggerdate = taggerdate;
94                 name->generation = generation;
95                 name->distance = distance;
96                 name->from_tag = from_tag;
97         } else {
98                 free(to_free);
99                 return;
100         }
101
102         for (parents = commit->parents;
103                         parents;
104                         parents = parents->next, parent_number++) {
105                 if (parent_number > 1) {
106                         size_t len;
107                         char *new_name;
108
109                         strip_suffix(tip_name, "^0", &len);
110                         if (generation > 0)
111                                 new_name = xstrfmt("%.*s~%d^%d", (int)len, tip_name,
112                                                    generation, parent_number);
113                         else
114                                 new_name = xstrfmt("%.*s^%d", (int)len, tip_name,
115                                                    parent_number);
116
117                         name_rev(parents->item, new_name, taggerdate, 0,
118                                  distance + MERGE_TRAVERSAL_WEIGHT,
119                                  from_tag, 0);
120                 } else {
121                         name_rev(parents->item, tip_name, taggerdate,
122                                  generation + 1, distance + 1,
123                                  from_tag, 0);
124                 }
125         }
126 }
127
128 static int subpath_matches(const char *path, const char *filter)
129 {
130         const char *subpath = path;
131
132         while (subpath) {
133                 if (!wildmatch(filter, subpath, 0))
134                         return subpath - path;
135                 subpath = strchr(subpath, '/');
136                 if (subpath)
137                         subpath++;
138         }
139         return -1;
140 }
141
142 static const char *name_ref_abbrev(const char *refname, int shorten_unambiguous)
143 {
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;
150         return refname;
151 }
152
153 struct name_ref_data {
154         int tags_only;
155         int name_only;
156         struct string_list ref_filters;
157         struct string_list exclude_filters;
158 };
159
160 static struct tip_table {
161         struct tip_table_entry {
162                 struct object_id oid;
163                 const char *refname;
164         } *table;
165         int nr;
166         int alloc;
167         int sorted;
168 } tip_table;
169
170 static void add_to_tip_table(const struct object_id *oid, const char *refname,
171                              int shorten_unambiguous)
172 {
173         refname = name_ref_abbrev(refname, shorten_unambiguous);
174
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);
178         tip_table.nr++;
179         tip_table.sorted = 0;
180 }
181
182 static int tipcmp(const void *a_, const void *b_)
183 {
184         const struct tip_table_entry *a = a_, *b = b_;
185         return oidcmp(&a->oid, &b->oid);
186 }
187
188 static int name_ref(const char *path, const struct object_id *oid, int flags, void *cb_data)
189 {
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;
193         int deref = 0;
194         timestamp_t taggerdate = TIME_MAX;
195
196         if (data->tags_only && !starts_with(path, "refs/tags/"))
197                 return 0;
198
199         if (data->exclude_filters.nr) {
200                 struct string_list_item *item;
201
202                 for_each_string_list_item(item, &data->exclude_filters) {
203                         if (subpath_matches(path, item->string) >= 0)
204                                 return 0;
205                 }
206         }
207
208         if (data->ref_filters.nr) {
209                 struct string_list_item *item;
210                 int matched = 0;
211
212                 /* See if any of the patterns match. */
213                 for_each_string_list_item(item, &data->ref_filters) {
214                         /*
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'.
223                          */
224                         switch (subpath_matches(path, item->string)) {
225                         case -1: /* did not match */
226                                 break;
227                         case 0: /* matched fully */
228                                 matched = 1;
229                                 break;
230                         default: /* matched subpath */
231                                 matched = 1;
232                                 can_abbreviate_output = 1;
233                                 break;
234                         }
235                 }
236
237                 /* If none of the patterns matched, stop now */
238                 if (!matched)
239                         return 0;
240         }
241
242         add_to_tip_table(oid, path, can_abbreviate_output);
243
244         while (o && o->type == OBJ_TAG) {
245                 struct tag *t = (struct tag *) o;
246                 if (!t->tagged)
247                         break; /* broken repository */
248                 o = parse_object(&t->tagged->oid);
249                 deref = 1;
250                 taggerdate = t->date;
251         }
252         if (o && o->type == OBJ_COMMIT) {
253                 struct commit *commit = (struct commit *)o;
254                 int from_tag = starts_with(path, "refs/tags/");
255
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,
260                          from_tag, deref);
261         }
262         return 0;
263 }
264
265 static const unsigned char *nth_tip_table_ent(size_t ix, void *table_)
266 {
267         struct tip_table_entry *table = table_;
268         return table[ix].oid.hash;
269 }
270
271 static const char *get_exact_ref_match(const struct object *o)
272 {
273         int found;
274
275         if (!tip_table.table || !tip_table.nr)
276                 return NULL;
277
278         if (!tip_table.sorted) {
279                 QSORT(tip_table.table, tip_table.nr, tipcmp);
280                 tip_table.sorted = 1;
281         }
282
283         found = sha1_pos(o->oid.hash, tip_table.table, tip_table.nr,
284                          nth_tip_table_ent);
285         if (0 <= found)
286                 return tip_table.table[found].refname;
287         return NULL;
288 }
289
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)
292 {
293         struct rev_name *n;
294         struct commit *c;
295
296         if (o->type != OBJ_COMMIT)
297                 return get_exact_ref_match(o);
298         c = (struct commit *) o;
299         n = c->util;
300         if (!n)
301                 return NULL;
302
303         if (!n->generation)
304                 return n->tip_name;
305         else {
306                 int len = strlen(n->tip_name);
307                 if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
308                         len -= 2;
309                 strbuf_reset(buf);
310                 strbuf_addf(buf, "%.*s~%d", len, n->tip_name, n->generation);
311                 return buf->buf;
312         }
313 }
314
315 static void show_name(const struct object *obj,
316                       const char *caller_name,
317                       int always, int allow_undefined, int name_only)
318 {
319         const char *name;
320         const struct object_id *oid = &obj->oid;
321         struct strbuf buf = STRBUF_INIT;
322
323         if (!name_only)
324                 printf("%s ", caller_name ? caller_name : oid_to_hex(oid));
325         name = get_rev_name(obj, &buf);
326         if (name)
327                 printf("%s\n", name);
328         else if (allow_undefined)
329                 printf("undefined\n");
330         else if (always)
331                 printf("%s\n", find_unique_abbrev(oid, DEFAULT_ABBREV));
332         else
333                 die("cannot describe '%s'", oid_to_hex(oid));
334         strbuf_release(&buf);
335 }
336
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"),
341         NULL
342 };
343
344 static void name_rev_line(char *p, struct name_ref_data *data)
345 {
346         struct strbuf buf = STRBUF_INIT;
347         int forty = 0;
348         char *p_start;
349         for (p_start = p; *p; p++) {
350 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
351                 if (!ishex(*p))
352                         forty = 0;
353                 else if (++forty == GIT_SHA1_HEXSZ &&
354                          !ishex(*(p+1))) {
355                         struct object_id oid;
356                         const char *name = NULL;
357                         char c = *(p+1);
358                         int p_len = p - p_start + 1;
359
360                         forty = 0;
361
362                         *(p+1) = 0;
363                         if (!get_oid(p - (GIT_SHA1_HEXSZ - 1), &oid)) {
364                                 struct object *o =
365                                         lookup_object(oid.hash);
366                                 if (o)
367                                         name = get_rev_name(o, &buf);
368                         }
369                         *(p+1) = c;
370
371                         if (!name)
372                                 continue;
373
374                         if (data->name_only)
375                                 printf("%.*s%s", p_len - GIT_SHA1_HEXSZ, p_start, name);
376                         else
377                                 printf("%.*s (%s)", p_len, p_start, name);
378                         p_start = p + 1;
379                 }
380         }
381
382         /* flush */
383         if (p_start != p)
384                 fwrite(p_start, p - p_start, 1, stdout);
385
386         strbuf_release(&buf);
387 }
388
389 int cmd_name_rev(int argc, const char **argv, const char *prefix)
390 {
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>")),
401                 OPT_GROUP(""),
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")),
407                 {
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,
412                 },
413                 OPT_END(),
414         };
415
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);
421         }
422         if (all || transform_stdin)
423                 cutoff = 0;
424
425         for (; argc; argc--, argv++) {
426                 struct object_id oid;
427                 struct object *object;
428                 struct commit *commit;
429
430                 if (get_oid(*argv, &oid)) {
431                         fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
432                                         *argv);
433                         continue;
434                 }
435
436                 commit = NULL;
437                 object = parse_object(&oid);
438                 if (object) {
439                         struct object *peeled = deref_tag(object, *argv, 0);
440                         if (peeled && peeled->type == OBJ_COMMIT)
441                                 commit = (struct commit *)peeled;
442                 }
443
444                 if (!object) {
445                         fprintf(stderr, "Could not get object for %s. Skipping.\n",
446                                         *argv);
447                         continue;
448                 }
449
450                 if (commit) {
451                         if (cutoff > commit->date)
452                                 cutoff = commit->date;
453                 }
454
455                 if (peel_tag) {
456                         if (!commit) {
457                                 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
458                                         *argv);
459                                 continue;
460                         }
461                         object = (struct object *)commit;
462                 }
463                 add_object_array(object, *argv, &revs);
464         }
465
466         if (cutoff)
467                 cutoff = cutoff - CUTOFF_DATE_SLOP;
468         for_each_ref(name_ref, &data);
469
470         if (transform_stdin) {
471                 char buffer[2048];
472
473                 while (!feof(stdin)) {
474                         char *p = fgets(buffer, sizeof(buffer), stdin);
475                         if (!p)
476                                 break;
477                         name_rev_line(p, &data);
478                 }
479         } else if (all) {
480                 int i, max;
481
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)
486                                 continue;
487                         show_name(obj, NULL,
488                                   always, allow_undefined, data.name_only);
489                 }
490         } else {
491                 int i;
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);
495         }
496
497         UNLEAK(revs);
498         return 0;
499 }