]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/name-rev.c
config: don't include config.h by default
[thirdparty/git.git] / builtin / name-rev.c
CommitLineData
d6b64ed0 1#include "builtin.h"
bd321bcc 2#include "cache.h"
b2141fc1 3#include "config.h"
bd321bcc
JS
4#include "commit.h"
5#include "tag.h"
6#include "refs.h"
edefb1a2 7#include "parse-options.h"
b23e0b93 8#include "sha1-lookup.h"
bd321bcc 9
c075aea5
JH
10#define CUTOFF_DATE_SLOP 86400 /* one day */
11
bd321bcc
JS
12typedef struct rev_name {
13 const char *tip_name;
75504248 14 unsigned long taggerdate;
bd321bcc 15 int generation;
ac076c29 16 int distance;
bd321bcc
JS
17} rev_name;
18
19static long cutoff = LONG_MAX;
20
ac076c29
JS
21/* How many generations are maximally preferred over _one_ merge traversal? */
22#define MERGE_TRAVERSAL_WEIGHT 65535
23
bd321bcc 24static void name_rev(struct commit *commit,
75504248
JS
25 const char *tip_name, unsigned long taggerdate,
26 int generation, int distance,
bd321bcc
JS
27 int deref)
28{
d3ff6f55 29 struct rev_name *name = (struct rev_name *)commit->util;
bd321bcc 30 struct commit_list *parents;
f2e6f1c9 31 int parent_number = 1;
bd321bcc 32
0064053b 33 parse_commit(commit);
bd321bcc
JS
34
35 if (commit->date < cutoff)
36 return;
37
38 if (deref) {
b2724c87 39 tip_name = xstrfmt("%s^0", tip_name);
bd321bcc
JS
40
41 if (generation)
42 die("generation: %d, but deref?", generation);
43 }
44
45 if (name == NULL) {
46 name = xmalloc(sizeof(rev_name));
d3ff6f55 47 commit->util = name;
bd321bcc 48 goto copy_data;
75504248
JS
49 } else if (name->taggerdate > taggerdate ||
50 (name->taggerdate == taggerdate &&
51 name->distance > distance)) {
bd321bcc
JS
52copy_data:
53 name->tip_name = tip_name;
75504248 54 name->taggerdate = taggerdate;
bd321bcc 55 name->generation = generation;
ac076c29 56 name->distance = distance;
bd321bcc
JS
57 } else
58 return;
59
60 for (parents = commit->parents;
61 parents;
62 parents = parents->next, parent_number++) {
f2e6f1c9 63 if (parent_number > 1) {
34e02deb 64 size_t len;
75faa45a 65 char *new_name;
bd321bcc 66
34e02deb 67 strip_suffix(tip_name, "^0", &len);
bd321bcc 68 if (generation > 0)
34e02deb 69 new_name = xstrfmt("%.*s~%d^%d", (int)len, tip_name,
75faa45a 70 generation, parent_number);
bd321bcc 71 else
34e02deb 72 new_name = xstrfmt("%.*s^%d", (int)len, tip_name,
75faa45a 73 parent_number);
bd321bcc 74
75504248 75 name_rev(parents->item, new_name, taggerdate, 0,
ac076c29 76 distance + MERGE_TRAVERSAL_WEIGHT, 0);
bd321bcc 77 } else {
75504248
JS
78 name_rev(parents->item, tip_name, taggerdate,
79 generation + 1, distance + 1, 0);
bd321bcc
JS
80 }
81 }
82}
83
98c5c4ad
NK
84static int subpath_matches(const char *path, const char *filter)
85{
86 const char *subpath = path;
87
88 while (subpath) {
eb07894f 89 if (!wildmatch(filter, subpath, 0, NULL))
98c5c4ad
NK
90 return subpath - path;
91 subpath = strchr(subpath, '/');
92 if (subpath)
93 subpath++;
94 }
95 return -1;
96}
97
9608a190
JH
98static const char *name_ref_abbrev(const char *refname, int shorten_unambiguous)
99{
100 if (shorten_unambiguous)
101 refname = shorten_unambiguous_ref(refname, 0);
59556548 102 else if (starts_with(refname, "refs/heads/"))
9608a190 103 refname = refname + 11;
59556548 104 else if (starts_with(refname, "refs/"))
9608a190
JH
105 refname = refname + 5;
106 return refname;
107}
108
2afc29aa
JS
109struct name_ref_data {
110 int tags_only;
23615708 111 int name_only;
290be667 112 struct string_list ref_filters;
96415b49 113 struct string_list exclude_filters;
2afc29aa
JS
114};
115
b23e0b93
JH
116static struct tip_table {
117 struct tip_table_entry {
118 unsigned char sha1[20];
119 const char *refname;
120 } *table;
121 int nr;
122 int alloc;
123 int sorted;
124} tip_table;
125
126static void add_to_tip_table(const unsigned char *sha1, const char *refname,
127 int shorten_unambiguous)
128{
129 refname = name_ref_abbrev(refname, shorten_unambiguous);
130
131 ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc);
132 hashcpy(tip_table.table[tip_table.nr].sha1, sha1);
133 tip_table.table[tip_table.nr].refname = xstrdup(refname);
134 tip_table.nr++;
135 tip_table.sorted = 0;
136}
137
138static int tipcmp(const void *a_, const void *b_)
139{
140 const struct tip_table_entry *a = a_, *b = b_;
141 return hashcmp(a->sha1, b->sha1);
142}
143
73868486 144static int name_ref(const char *path, const struct object_id *oid, int flags, void *cb_data)
bd321bcc 145{
73868486 146 struct object *o = parse_object(oid->hash);
2afc29aa 147 struct name_ref_data *data = cb_data;
98c5c4ad 148 int can_abbreviate_output = data->tags_only && data->name_only;
bd321bcc 149 int deref = 0;
75504248 150 unsigned long taggerdate = ULONG_MAX;
bd321bcc 151
59556548 152 if (data->tags_only && !starts_with(path, "refs/tags/"))
2afc29aa
JS
153 return 0;
154
96415b49
JK
155 if (data->exclude_filters.nr) {
156 struct string_list_item *item;
157
158 for_each_string_list_item(item, &data->exclude_filters) {
159 if (subpath_matches(path, item->string) >= 0)
160 return 0;
161 }
162 }
163
290be667
JK
164 if (data->ref_filters.nr) {
165 struct string_list_item *item;
166 int matched = 0;
167
168 /* See if any of the patterns match. */
169 for_each_string_list_item(item, &data->ref_filters) {
170 /*
171 * Check all patterns even after finding a match, so
172 * that we can see if a match with a subpath exists.
173 * When a user asked for 'refs/tags/v*' and 'v1.*',
174 * both of which match, the user is showing her
175 * willingness to accept a shortened output by having
176 * the 'v1.*' in the acceptable refnames, so we
177 * shouldn't stop when seeing 'refs/tags/v1.4' matches
178 * 'refs/tags/v*'. We should show it as 'v1.4'.
179 */
180 switch (subpath_matches(path, item->string)) {
181 case -1: /* did not match */
182 break;
183 case 0: /* matched fully */
184 matched = 1;
185 break;
186 default: /* matched subpath */
187 matched = 1;
188 can_abbreviate_output = 1;
189 break;
190 }
98c5c4ad 191 }
290be667
JK
192
193 /* If none of the patterns matched, stop now */
194 if (!matched)
195 return 0;
98c5c4ad 196 }
bd321bcc 197
73868486 198 add_to_tip_table(oid->hash, path, can_abbreviate_output);
b23e0b93 199
1974632c 200 while (o && o->type == OBJ_TAG) {
bd321bcc
JS
201 struct tag *t = (struct tag *) o;
202 if (!t->tagged)
203 break; /* broken repository */
ed1c9977 204 o = parse_object(t->tagged->oid.hash);
bd321bcc 205 deref = 1;
75504248 206 taggerdate = t->date;
bd321bcc 207 }
1974632c 208 if (o && o->type == OBJ_COMMIT) {
bd321bcc 209 struct commit *commit = (struct commit *)o;
bd321bcc 210
9608a190 211 path = name_ref_abbrev(path, can_abbreviate_output);
75504248 212 name_rev(commit, xstrdup(path), taggerdate, 0, 0, deref);
bd321bcc
JS
213 }
214 return 0;
215}
216
b23e0b93
JH
217static const unsigned char *nth_tip_table_ent(size_t ix, void *table_)
218{
219 struct tip_table_entry *table = table_;
220 return table[ix].sha1;
221}
222
223static const char *get_exact_ref_match(const struct object *o)
224{
225 int found;
226
227 if (!tip_table.table || !tip_table.nr)
228 return NULL;
229
230 if (!tip_table.sorted) {
9ed0d8d6 231 QSORT(tip_table.table, tip_table.nr, tipcmp);
b23e0b93
JH
232 tip_table.sorted = 1;
233 }
234
ed1c9977 235 found = sha1_pos(o->oid.hash, tip_table.table, tip_table.nr,
b23e0b93
JH
236 nth_tip_table_ent);
237 if (0 <= found)
238 return tip_table.table[found].refname;
239 return NULL;
240}
241
903fc7da
JK
242/* may return a constant string or use "buf" as scratch space */
243static const char *get_rev_name(const struct object *o, struct strbuf *buf)
bd321bcc 244{
d3ff6f55
LT
245 struct rev_name *n;
246 struct commit *c;
247
1974632c 248 if (o->type != OBJ_COMMIT)
b23e0b93 249 return get_exact_ref_match(o);
d3ff6f55
LT
250 c = (struct commit *) o;
251 n = c->util;
bd321bcc 252 if (!n)
a2cf9f44 253 return NULL;
bd321bcc
JS
254
255 if (!n->generation)
256 return n->tip_name;
59d3f541
JS
257 else {
258 int len = strlen(n->tip_name);
259 if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
260 len -= 2;
903fc7da
JK
261 strbuf_reset(buf);
262 strbuf_addf(buf, "%.*s~%d", len, n->tip_name, n->generation);
263 return buf->buf;
59d3f541 264 }
bd321bcc 265}
1f1e895f 266
da2478db
JH
267static void show_name(const struct object *obj,
268 const char *caller_name,
269 int always, int allow_undefined, int name_only)
270{
271 const char *name;
f2fd0760 272 const struct object_id *oid = &obj->oid;
903fc7da 273 struct strbuf buf = STRBUF_INIT;
da2478db
JH
274
275 if (!name_only)
f2fd0760 276 printf("%s ", caller_name ? caller_name : oid_to_hex(oid));
903fc7da 277 name = get_rev_name(obj, &buf);
da2478db
JH
278 if (name)
279 printf("%s\n", name);
280 else if (allow_undefined)
281 printf("undefined\n");
282 else if (always)
f2fd0760 283 printf("%s\n", find_unique_abbrev(oid->hash, DEFAULT_ABBREV));
da2478db 284 else
f2fd0760 285 die("cannot describe '%s'", oid_to_hex(oid));
903fc7da 286 strbuf_release(&buf);
da2478db
JH
287}
288
edefb1a2 289static char const * const name_rev_usage[] = {
9c9b4f2f
AH
290 N_("git name-rev [<options>] <commit>..."),
291 N_("git name-rev [<options>] --all"),
292 N_("git name-rev [<options>] --stdin"),
edefb1a2
PH
293 NULL
294};
295
e8b55fab
JH
296static void name_rev_line(char *p, struct name_ref_data *data)
297{
903fc7da 298 struct strbuf buf = STRBUF_INIT;
e8b55fab
JH
299 int forty = 0;
300 char *p_start;
301 for (p_start = p; *p; p++) {
302#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
303 if (!ishex(*p))
304 forty = 0;
305 else if (++forty == 40 &&
306 !ishex(*(p+1))) {
307 unsigned char sha1[40];
308 const char *name = NULL;
309 char c = *(p+1);
7c5b1675 310 int p_len = p - p_start + 1;
e8b55fab
JH
311
312 forty = 0;
313
314 *(p+1) = 0;
315 if (!get_sha1(p - 39, sha1)) {
316 struct object *o =
317 lookup_object(sha1);
318 if (o)
903fc7da 319 name = get_rev_name(o, &buf);
e8b55fab
JH
320 }
321 *(p+1) = c;
322
323 if (!name)
324 continue;
325
7c5b1675
RS
326 if (data->name_only)
327 printf("%.*s%s", p_len - 40, p_start, name);
328 else
329 printf("%.*s (%s)", p_len, p_start, name);
e8b55fab
JH
330 p_start = p + 1;
331 }
332 }
333
334 /* flush */
335 if (p_start != p)
336 fwrite(p_start, p - p_start, 1, stdout);
903fc7da
JK
337
338 strbuf_release(&buf);
e8b55fab
JH
339}
340
d6b64ed0 341int cmd_name_rev(int argc, const char **argv, const char *prefix)
bd321bcc 342{
3cd47459 343 struct object_array revs = OBJECT_ARRAY_INIT;
adfc1857 344 int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
96415b49 345 struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP };
edefb1a2 346 struct option opts[] = {
d5d09d47
SB
347 OPT_BOOL(0, "name-only", &data.name_only, N_("print only names (no SHA-1)")),
348 OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")),
290be667 349 OPT_STRING_LIST(0, "refs", &data.ref_filters, N_("pattern"),
422cad0a 350 N_("only use refs matching <pattern>")),
96415b49
JK
351 OPT_STRING_LIST(0, "exclude", &data.exclude_filters, N_("pattern"),
352 N_("ignore refs matching <pattern>")),
edefb1a2 353 OPT_GROUP(""),
d5d09d47
SB
354 OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")),
355 OPT_BOOL(0, "stdin", &transform_stdin, N_("read from stdin")),
356 OPT_BOOL(0, "undefined", &allow_undefined, N_("allow to print `undefined` names (default)")),
357 OPT_BOOL(0, "always", &always,
422cad0a 358 N_("show abbreviated commit object as fallback")),
adfc1857
JH
359 {
360 /* A Hidden OPT_BOOL */
361 OPTION_SET_INT, 0, "peel-tag", &peel_tag, NULL,
362 N_("dereference tags in the input (internal use)"),
363 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1,
364 },
edefb1a2
PH
365 OPT_END(),
366 };
bd321bcc 367
ef90d6d4 368 git_config(git_default_config, NULL);
37782920 369 argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0);
05efb7b7 370 if (all + transform_stdin + !!argc > 1) {
edefb1a2
PH
371 error("Specify either a list, or --all, not both!");
372 usage_with_options(name_rev_usage, opts);
373 }
374 if (all || transform_stdin)
375 cutoff = 0;
bd321bcc 376
edefb1a2 377 for (; argc; argc--, argv++) {
bd321bcc 378 unsigned char sha1[20];
118aa4ac 379 struct object *object;
bd321bcc
JS
380 struct commit *commit;
381
bd321bcc
JS
382 if (get_sha1(*argv, sha1)) {
383 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
384 *argv);
385 continue;
386 }
387
118aa4ac
JH
388 commit = NULL;
389 object = parse_object(sha1);
390 if (object) {
391 struct object *peeled = deref_tag(object, *argv, 0);
392 if (peeled && peeled->type == OBJ_COMMIT)
393 commit = (struct commit *)peeled;
394 }
395
396 if (!object) {
397 fprintf(stderr, "Could not get object for %s. Skipping.\n",
bd321bcc
JS
398 *argv);
399 continue;
400 }
401
118aa4ac
JH
402 if (commit) {
403 if (cutoff > commit->date)
404 cutoff = commit->date;
405 }
adfc1857
JH
406
407 if (peel_tag) {
408 if (!commit) {
409 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
410 *argv);
411 continue;
412 }
413 object = (struct object *)commit;
414 }
118aa4ac 415 add_object_array(object, *argv, &revs);
bd321bcc
JS
416 }
417
c075aea5
JH
418 if (cutoff)
419 cutoff = cutoff - CUTOFF_DATE_SLOP;
73868486 420 for_each_ref(name_ref, &data);
bd321bcc
JS
421
422 if (transform_stdin) {
423 char buffer[2048];
bd321bcc
JS
424
425 while (!feof(stdin)) {
e8b55fab 426 char *p = fgets(buffer, sizeof(buffer), stdin);
bd321bcc
JS
427 if (!p)
428 break;
e8b55fab 429 name_rev_line(p, &data);
bd321bcc
JS
430 }
431 } else if (all) {
fc046a75 432 int i, max;
bd321bcc 433
fc046a75 434 max = get_max_object_index();
a83123de
BS
435 for (i = 0; i < max; i++) {
436 struct object *obj = get_indexed_object(i);
e8b14d7e 437 if (!obj || obj->type != OBJ_COMMIT)
a83123de
BS
438 continue;
439 show_name(obj, NULL,
da2478db 440 always, allow_undefined, data.name_only);
a83123de 441 }
1f1e895f
LT
442 } else {
443 int i;
da2478db
JH
444 for (i = 0; i < revs.nr; i++)
445 show_name(revs.objects[i].item, revs.objects[i].name,
446 always, allow_undefined, data.name_only);
1f1e895f 447 }
bd321bcc
JS
448
449 return 0;
450}