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