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