]>
Commit | Line | Data |
---|---|---|
d6b64ed0 | 1 | #include "builtin.h" |
32a8f510 | 2 | #include "environment.h" |
f394e093 | 3 | #include "gettext.h" |
41771fa4 | 4 | #include "hex.h" |
109cd76d | 5 | #include "repository.h" |
b2141fc1 | 6 | #include "config.h" |
bd321bcc JS |
7 | #include "commit.h" |
8 | #include "tag.h" | |
9 | #include "refs.h" | |
dabab1d6 | 10 | #include "object-name.h" |
ca4eed70 | 11 | #include "pager.h" |
edefb1a2 | 12 | #include "parse-options.h" |
49f7a2fd | 13 | #include "prio-queue.h" |
bc626927 | 14 | #include "hash-lookup.h" |
8fd79a73 | 15 | #include "commit-slab.h" |
2e8ea40f | 16 | #include "commit-graph.h" |
dd77d587 | 17 | #include "wildmatch.h" |
f39addd0 | 18 | #include "mem-pool.h" |
bd321bcc | 19 | |
2e09c012 SG |
20 | /* |
21 | * One day. See the 'name a rev shortly after epoch' test in t6120 when | |
22 | * changing this value | |
23 | */ | |
24 | #define CUTOFF_DATE_SLOP 86400 | |
c075aea5 | 25 | |
71620ca8 | 26 | struct rev_name { |
45a14f57 | 27 | const char *tip_name; |
dddbad72 | 28 | timestamp_t taggerdate; |
bd321bcc | 29 | int generation; |
ac076c29 | 30 | int distance; |
ef1e7406 | 31 | int from_tag; |
71620ca8 | 32 | }; |
bd321bcc | 33 | |
f13ca7ce | 34 | define_commit_slab(commit_rev_name, struct rev_name); |
8fd79a73 | 35 | |
2e8ea40f | 36 | static timestamp_t generation_cutoff = GENERATION_NUMBER_INFINITY; |
5589e87f | 37 | static timestamp_t cutoff = TIME_MAX; |
8fd79a73 | 38 | static struct commit_rev_name rev_names; |
bd321bcc | 39 | |
2e8ea40f JK |
40 | /* Disable the cutoff checks entirely */ |
41 | static void disable_cutoff(void) | |
42 | { | |
43 | generation_cutoff = 0; | |
44 | cutoff = 0; | |
45 | } | |
46 | ||
47 | /* Cutoff searching any commits older than this one */ | |
48 | static void set_commit_cutoff(struct commit *commit) | |
49 | { | |
50 | ||
51 | if (cutoff > commit->date) | |
52 | cutoff = commit->date; | |
53 | ||
54 | if (generation_cutoff) { | |
55 | timestamp_t generation = commit_graph_generation(commit); | |
56 | ||
57 | if (generation_cutoff > generation) | |
58 | generation_cutoff = generation; | |
59 | } | |
60 | } | |
61 | ||
62 | /* adjust the commit date cutoff with a slop to allow for slightly incorrect | |
63 | * commit timestamps in case of clock skew. | |
64 | */ | |
65 | static void adjust_cutoff_timestamp_for_slop(void) | |
66 | { | |
67 | if (cutoff) { | |
68 | /* check for undeflow */ | |
69 | if (cutoff > TIME_MIN + CUTOFF_DATE_SLOP) | |
70 | cutoff = cutoff - CUTOFF_DATE_SLOP; | |
71 | else | |
72 | cutoff = TIME_MIN; | |
73 | } | |
74 | } | |
75 | ||
76 | /* Check if a commit is before the cutoff. Prioritize generation numbers | |
77 | * first, but use the commit timestamp if we lack generation data. | |
78 | */ | |
79 | static int commit_is_before_cutoff(struct commit *commit) | |
80 | { | |
81 | if (generation_cutoff < GENERATION_NUMBER_INFINITY) | |
82 | return generation_cutoff && | |
83 | commit_graph_generation(commit) < generation_cutoff; | |
84 | ||
85 | return commit->date < cutoff; | |
86 | } | |
87 | ||
ac076c29 JS |
88 | /* How many generations are maximally preferred over _one_ merge traversal? */ |
89 | #define MERGE_TRAVERSAL_WEIGHT 65535 | |
90 | ||
f13ca7ce RS |
91 | static int is_valid_rev_name(const struct rev_name *name) |
92 | { | |
45a14f57 | 93 | return name && name->tip_name; |
f13ca7ce RS |
94 | } |
95 | ||
36d2419c | 96 | static struct rev_name *get_commit_rev_name(const struct commit *commit) |
8fd79a73 | 97 | { |
f13ca7ce | 98 | struct rev_name *name = commit_rev_name_peek(&rev_names, commit); |
8fd79a73 | 99 | |
f13ca7ce | 100 | return is_valid_rev_name(name) ? name : NULL; |
8fd79a73 NTND |
101 | } |
102 | ||
3656f842 EN |
103 | static int effective_distance(int distance, int generation) |
104 | { | |
105 | return distance + (generation > 0 ? MERGE_TRAVERSAL_WEIGHT : 0); | |
106 | } | |
107 | ||
0041bf65 | 108 | static int is_better_name(struct rev_name *name, |
78089b71 | 109 | timestamp_t taggerdate, |
3656f842 | 110 | int generation, |
ef1e7406 JH |
111 | int distance, |
112 | int from_tag) | |
0041bf65 | 113 | { |
3656f842 EN |
114 | int name_distance = effective_distance(name->distance, name->generation); |
115 | int new_distance = effective_distance(distance, generation); | |
116 | ||
b2182a87 | 117 | /* If both are tags, we prefer the nearer one. */ |
ef1e7406 | 118 | if (from_tag && name->from_tag) |
b2182a87 | 119 | return name_distance > new_distance; |
ef1e7406 | 120 | |
b2182a87 | 121 | /* Favor a tag over a non-tag. */ |
ef1e7406 JH |
122 | if (name->from_tag != from_tag) |
123 | return from_tag; | |
124 | ||
125 | /* | |
126 | * We are now looking at two non-tags. Tiebreak to favor | |
127 | * shorter hops. | |
128 | */ | |
3656f842 EN |
129 | if (name_distance != new_distance) |
130 | return name_distance > new_distance; | |
ef1e7406 JH |
131 | |
132 | /* ... or tiebreak to favor older date */ | |
133 | if (name->taggerdate != taggerdate) | |
134 | return name->taggerdate > taggerdate; | |
135 | ||
136 | /* keep the current one if we cannot decide */ | |
137 | return 0; | |
0041bf65 JH |
138 | } |
139 | ||
766f9e39 | 140 | static struct rev_name *create_or_update_name(struct commit *commit, |
766f9e39 SG |
141 | timestamp_t taggerdate, |
142 | int generation, int distance, | |
143 | int from_tag) | |
bd321bcc | 144 | { |
f13ca7ce | 145 | struct rev_name *name = commit_rev_name_at(&rev_names, commit); |
bd321bcc | 146 | |
45a14f57 RS |
147 | if (is_valid_rev_name(name) && |
148 | !is_better_name(name, taggerdate, generation, distance, from_tag)) | |
149 | return NULL; | |
3e2feb0d | 150 | |
3e2feb0d MÅ |
151 | name->taggerdate = taggerdate; |
152 | name->generation = generation; | |
153 | name->distance = distance; | |
154 | name->from_tag = from_tag; | |
155 | ||
156 | return name; | |
766f9e39 SG |
157 | } |
158 | ||
f39addd0 RS |
159 | static char *get_parent_name(const struct rev_name *name, int parent_number, |
160 | struct mem_pool *string_pool) | |
ddc42ec7 RS |
161 | { |
162 | size_t len; | |
163 | ||
164 | strip_suffix(name->tip_name, "^0", &len); | |
1c56fc20 | 165 | if (name->generation > 0) { |
f39addd0 RS |
166 | return mem_pool_strfmt(string_pool, "%.*s~%d^%d", |
167 | (int)len, name->tip_name, | |
168 | name->generation, parent_number); | |
1c56fc20 | 169 | } else { |
f39addd0 RS |
170 | return mem_pool_strfmt(string_pool, "%.*s^%d", |
171 | (int)len, name->tip_name, parent_number); | |
1c56fc20 | 172 | } |
ddc42ec7 RS |
173 | } |
174 | ||
49f7a2fd | 175 | static void name_rev(struct commit *start_commit, |
dddbad72 | 176 | const char *tip_name, timestamp_t taggerdate, |
f39addd0 | 177 | int from_tag, int deref, struct mem_pool *string_pool) |
bd321bcc | 178 | { |
49f7a2fd SG |
179 | struct prio_queue queue; |
180 | struct commit *commit; | |
181 | struct commit **parents_to_queue = NULL; | |
182 | size_t parents_to_queue_nr, parents_to_queue_alloc = 0; | |
977dc191 | 183 | struct rev_name *start_name; |
2866fd28 | 184 | |
ecb5091f | 185 | repo_parse_commit(the_repository, start_commit); |
2e8ea40f | 186 | if (commit_is_before_cutoff(start_commit)) |
2866fd28 SG |
187 | return; |
188 | ||
977dc191 RS |
189 | start_name = create_or_update_name(start_commit, taggerdate, 0, 0, |
190 | from_tag); | |
191 | if (!start_name) | |
192 | return; | |
2866fd28 | 193 | if (deref) |
f39addd0 RS |
194 | start_name->tip_name = mem_pool_strfmt(string_pool, "%s^0", |
195 | tip_name); | |
15a4205d | 196 | else |
f39addd0 | 197 | start_name->tip_name = mem_pool_strdup(string_pool, tip_name); |
bd321bcc | 198 | |
49f7a2fd SG |
199 | memset(&queue, 0, sizeof(queue)); /* Use the prio_queue as LIFO */ |
200 | prio_queue_put(&queue, start_commit); | |
201 | ||
202 | while ((commit = prio_queue_get(&queue))) { | |
203 | struct rev_name *name = get_commit_rev_name(commit); | |
204 | struct commit_list *parents; | |
205 | int parent_number = 1; | |
206 | ||
207 | parents_to_queue_nr = 0; | |
208 | ||
209 | for (parents = commit->parents; | |
210 | parents; | |
211 | parents = parents->next, parent_number++) { | |
212 | struct commit *parent = parents->item; | |
977dc191 | 213 | struct rev_name *parent_name; |
49f7a2fd SG |
214 | int generation, distance; |
215 | ||
ecb5091f | 216 | repo_parse_commit(the_repository, parent); |
2e8ea40f | 217 | if (commit_is_before_cutoff(parent)) |
49f7a2fd SG |
218 | continue; |
219 | ||
220 | if (parent_number > 1) { | |
49f7a2fd SG |
221 | generation = 0; |
222 | distance = name->distance + MERGE_TRAVERSAL_WEIGHT; | |
223 | } else { | |
49f7a2fd SG |
224 | generation = name->generation + 1; |
225 | distance = name->distance + 1; | |
226 | } | |
227 | ||
977dc191 RS |
228 | parent_name = create_or_update_name(parent, taggerdate, |
229 | generation, | |
230 | distance, from_tag); | |
231 | if (parent_name) { | |
232 | if (parent_number > 1) | |
233 | parent_name->tip_name = | |
234 | get_parent_name(name, | |
f39addd0 RS |
235 | parent_number, |
236 | string_pool); | |
977dc191 RS |
237 | else |
238 | parent_name->tip_name = name->tip_name; | |
49f7a2fd SG |
239 | ALLOC_GROW(parents_to_queue, |
240 | parents_to_queue_nr + 1, | |
241 | parents_to_queue_alloc); | |
242 | parents_to_queue[parents_to_queue_nr] = parent; | |
243 | parents_to_queue_nr++; | |
244 | } | |
bd321bcc | 245 | } |
3a521503 | 246 | |
49f7a2fd SG |
247 | /* The first parent must come out first from the prio_queue */ |
248 | while (parents_to_queue_nr) | |
249 | prio_queue_put(&queue, | |
250 | parents_to_queue[--parents_to_queue_nr]); | |
bd321bcc | 251 | } |
49f7a2fd SG |
252 | |
253 | clear_prio_queue(&queue); | |
254 | free(parents_to_queue); | |
bd321bcc JS |
255 | } |
256 | ||
98c5c4ad NK |
257 | static int subpath_matches(const char *path, const char *filter) |
258 | { | |
259 | const char *subpath = path; | |
260 | ||
261 | while (subpath) { | |
55d34269 | 262 | if (!wildmatch(filter, subpath, 0)) |
98c5c4ad NK |
263 | return subpath - path; |
264 | subpath = strchr(subpath, '/'); | |
265 | if (subpath) | |
266 | subpath++; | |
267 | } | |
268 | return -1; | |
269 | } | |
270 | ||
2afc29aa JS |
271 | struct name_ref_data { |
272 | int tags_only; | |
23615708 | 273 | int name_only; |
290be667 | 274 | struct string_list ref_filters; |
96415b49 | 275 | struct string_list exclude_filters; |
2afc29aa JS |
276 | }; |
277 | ||
b23e0b93 JH |
278 | static struct tip_table { |
279 | struct tip_table_entry { | |
511dca80 | 280 | struct object_id oid; |
b23e0b93 | 281 | const char *refname; |
079f9709 RS |
282 | struct commit *commit; |
283 | timestamp_t taggerdate; | |
284 | unsigned int from_tag:1; | |
285 | unsigned int deref:1; | |
b23e0b93 JH |
286 | } *table; |
287 | int nr; | |
288 | int alloc; | |
289 | int sorted; | |
290 | } tip_table; | |
291 | ||
511dca80 | 292 | static void add_to_tip_table(const struct object_id *oid, const char *refname, |
079f9709 RS |
293 | int shorten_unambiguous, struct commit *commit, |
294 | timestamp_t taggerdate, int from_tag, int deref) | |
b23e0b93 | 295 | { |
486620ae ÆAB |
296 | char *short_refname = NULL; |
297 | ||
298 | if (shorten_unambiguous) | |
299 | short_refname = shorten_unambiguous_ref(refname, 0); | |
300 | else if (skip_prefix(refname, "refs/heads/", &refname)) | |
301 | ; /* refname already advanced */ | |
302 | else | |
303 | skip_prefix(refname, "refs/", &refname); | |
b23e0b93 JH |
304 | |
305 | ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc); | |
511dca80 | 306 | oidcpy(&tip_table.table[tip_table.nr].oid, oid); |
486620ae ÆAB |
307 | tip_table.table[tip_table.nr].refname = short_refname ? |
308 | short_refname : xstrdup(refname); | |
079f9709 RS |
309 | tip_table.table[tip_table.nr].commit = commit; |
310 | tip_table.table[tip_table.nr].taggerdate = taggerdate; | |
311 | tip_table.table[tip_table.nr].from_tag = from_tag; | |
312 | tip_table.table[tip_table.nr].deref = deref; | |
b23e0b93 JH |
313 | tip_table.nr++; |
314 | tip_table.sorted = 0; | |
315 | } | |
316 | ||
317 | static int tipcmp(const void *a_, const void *b_) | |
318 | { | |
319 | const struct tip_table_entry *a = a_, *b = b_; | |
511dca80 | 320 | return oidcmp(&a->oid, &b->oid); |
b23e0b93 JH |
321 | } |
322 | ||
079f9709 RS |
323 | static int cmp_by_tag_and_age(const void *a_, const void *b_) |
324 | { | |
325 | const struct tip_table_entry *a = a_, *b = b_; | |
326 | int cmp; | |
327 | ||
328 | /* Prefer tags. */ | |
329 | cmp = b->from_tag - a->from_tag; | |
330 | if (cmp) | |
331 | return cmp; | |
332 | ||
333 | /* Older is better. */ | |
334 | if (a->taggerdate < b->taggerdate) | |
335 | return -1; | |
336 | return a->taggerdate != b->taggerdate; | |
337 | } | |
338 | ||
63e14ee2 | 339 | static int name_ref(const char *path, const struct object_id *oid, |
5cf88fd8 | 340 | int flags UNUSED, void *cb_data) |
bd321bcc | 341 | { |
109cd76d | 342 | struct object *o = parse_object(the_repository, oid); |
2afc29aa | 343 | struct name_ref_data *data = cb_data; |
98c5c4ad | 344 | int can_abbreviate_output = data->tags_only && data->name_only; |
bd321bcc | 345 | int deref = 0; |
079f9709 RS |
346 | int from_tag = 0; |
347 | struct commit *commit = NULL; | |
dddbad72 | 348 | timestamp_t taggerdate = TIME_MAX; |
bd321bcc | 349 | |
59556548 | 350 | if (data->tags_only && !starts_with(path, "refs/tags/")) |
2afc29aa JS |
351 | return 0; |
352 | ||
96415b49 JK |
353 | if (data->exclude_filters.nr) { |
354 | struct string_list_item *item; | |
355 | ||
356 | for_each_string_list_item(item, &data->exclude_filters) { | |
357 | if (subpath_matches(path, item->string) >= 0) | |
358 | return 0; | |
359 | } | |
360 | } | |
361 | ||
290be667 JK |
362 | if (data->ref_filters.nr) { |
363 | struct string_list_item *item; | |
364 | int matched = 0; | |
365 | ||
366 | /* See if any of the patterns match. */ | |
367 | for_each_string_list_item(item, &data->ref_filters) { | |
368 | /* | |
369 | * Check all patterns even after finding a match, so | |
370 | * that we can see if a match with a subpath exists. | |
371 | * When a user asked for 'refs/tags/v*' and 'v1.*', | |
372 | * both of which match, the user is showing her | |
373 | * willingness to accept a shortened output by having | |
374 | * the 'v1.*' in the acceptable refnames, so we | |
375 | * shouldn't stop when seeing 'refs/tags/v1.4' matches | |
376 | * 'refs/tags/v*'. We should show it as 'v1.4'. | |
377 | */ | |
378 | switch (subpath_matches(path, item->string)) { | |
379 | case -1: /* did not match */ | |
380 | break; | |
381 | case 0: /* matched fully */ | |
382 | matched = 1; | |
383 | break; | |
384 | default: /* matched subpath */ | |
385 | matched = 1; | |
386 | can_abbreviate_output = 1; | |
387 | break; | |
388 | } | |
98c5c4ad | 389 | } |
290be667 JK |
390 | |
391 | /* If none of the patterns matched, stop now */ | |
392 | if (!matched) | |
393 | return 0; | |
98c5c4ad | 394 | } |
bd321bcc | 395 | |
1974632c | 396 | while (o && o->type == OBJ_TAG) { |
bd321bcc JS |
397 | struct tag *t = (struct tag *) o; |
398 | if (!t->tagged) | |
399 | break; /* broken repository */ | |
109cd76d | 400 | o = parse_object(the_repository, &t->tagged->oid); |
bd321bcc | 401 | deref = 1; |
75504248 | 402 | taggerdate = t->date; |
bd321bcc | 403 | } |
1974632c | 404 | if (o && o->type == OBJ_COMMIT) { |
079f9709 RS |
405 | commit = (struct commit *)o; |
406 | from_tag = starts_with(path, "refs/tags/"); | |
5554451d | 407 | if (taggerdate == TIME_MAX) |
e0c4da6f | 408 | taggerdate = commit->date; |
bd321bcc | 409 | } |
079f9709 RS |
410 | |
411 | add_to_tip_table(oid, path, can_abbreviate_output, commit, taggerdate, | |
412 | from_tag, deref); | |
bd321bcc JS |
413 | return 0; |
414 | } | |
415 | ||
f39addd0 | 416 | static void name_tips(struct mem_pool *string_pool) |
079f9709 RS |
417 | { |
418 | int i; | |
419 | ||
420 | /* | |
421 | * Try to set better names first, so that worse ones spread | |
422 | * less. | |
423 | */ | |
424 | QSORT(tip_table.table, tip_table.nr, cmp_by_tag_and_age); | |
425 | for (i = 0; i < tip_table.nr; i++) { | |
426 | struct tip_table_entry *e = &tip_table.table[i]; | |
427 | if (e->commit) { | |
428 | name_rev(e->commit, e->refname, e->taggerdate, | |
f39addd0 | 429 | e->from_tag, e->deref, string_pool); |
079f9709 RS |
430 | } |
431 | } | |
432 | } | |
433 | ||
8380dcd7 | 434 | static const struct object_id *nth_tip_table_ent(size_t ix, const void *table_) |
b23e0b93 | 435 | { |
8380dcd7 | 436 | const struct tip_table_entry *table = table_; |
45ee13b9 | 437 | return &table[ix].oid; |
b23e0b93 JH |
438 | } |
439 | ||
440 | static const char *get_exact_ref_match(const struct object *o) | |
441 | { | |
442 | int found; | |
443 | ||
444 | if (!tip_table.table || !tip_table.nr) | |
445 | return NULL; | |
446 | ||
447 | if (!tip_table.sorted) { | |
9ed0d8d6 | 448 | QSORT(tip_table.table, tip_table.nr, tipcmp); |
b23e0b93 JH |
449 | tip_table.sorted = 1; |
450 | } | |
451 | ||
45ee13b9 JK |
452 | found = oid_pos(&o->oid, tip_table.table, tip_table.nr, |
453 | nth_tip_table_ent); | |
b23e0b93 JH |
454 | if (0 <= found) |
455 | return tip_table.table[found].refname; | |
456 | return NULL; | |
457 | } | |
458 | ||
903fc7da JK |
459 | /* may return a constant string or use "buf" as scratch space */ |
460 | static const char *get_rev_name(const struct object *o, struct strbuf *buf) | |
bd321bcc | 461 | { |
d3ff6f55 | 462 | struct rev_name *n; |
36d2419c | 463 | const struct commit *c; |
d3ff6f55 | 464 | |
1974632c | 465 | if (o->type != OBJ_COMMIT) |
b23e0b93 | 466 | return get_exact_ref_match(o); |
36d2419c | 467 | c = (const struct commit *) o; |
8fd79a73 | 468 | n = get_commit_rev_name(c); |
bd321bcc | 469 | if (!n) |
a2cf9f44 | 470 | return NULL; |
bd321bcc JS |
471 | |
472 | if (!n->generation) | |
473 | return n->tip_name; | |
59d3f541 | 474 | else { |
903fc7da | 475 | strbuf_reset(buf); |
c3794d4c RS |
476 | strbuf_addstr(buf, n->tip_name); |
477 | strbuf_strip_suffix(buf, "^0"); | |
478 | strbuf_addf(buf, "~%d", n->generation); | |
903fc7da | 479 | return buf->buf; |
59d3f541 | 480 | } |
bd321bcc | 481 | } |
1f1e895f | 482 | |
da2478db JH |
483 | static void show_name(const struct object *obj, |
484 | const char *caller_name, | |
485 | int always, int allow_undefined, int name_only) | |
486 | { | |
487 | const char *name; | |
f2fd0760 | 488 | const struct object_id *oid = &obj->oid; |
903fc7da | 489 | struct strbuf buf = STRBUF_INIT; |
da2478db JH |
490 | |
491 | if (!name_only) | |
f2fd0760 | 492 | printf("%s ", caller_name ? caller_name : oid_to_hex(oid)); |
903fc7da | 493 | name = get_rev_name(obj, &buf); |
da2478db JH |
494 | if (name) |
495 | printf("%s\n", name); | |
496 | else if (allow_undefined) | |
497 | printf("undefined\n"); | |
498 | else if (always) | |
d850b7a5 ÆAB |
499 | printf("%s\n", |
500 | repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV)); | |
da2478db | 501 | else |
f2fd0760 | 502 | die("cannot describe '%s'", oid_to_hex(oid)); |
903fc7da | 503 | strbuf_release(&buf); |
da2478db JH |
504 | } |
505 | ||
edefb1a2 | 506 | static char const * const name_rev_usage[] = { |
9c9b4f2f AH |
507 | N_("git name-rev [<options>] <commit>..."), |
508 | N_("git name-rev [<options>] --all"), | |
d271892f | 509 | N_("git name-rev [<options>] --annotate-stdin"), |
edefb1a2 PH |
510 | NULL |
511 | }; | |
512 | ||
e8b55fab JH |
513 | static void name_rev_line(char *p, struct name_ref_data *data) |
514 | { | |
903fc7da | 515 | struct strbuf buf = STRBUF_INIT; |
1c4675dc | 516 | int counter = 0; |
e8b55fab | 517 | char *p_start; |
1c4675dc | 518 | const unsigned hexsz = the_hash_algo->hexsz; |
519 | ||
e8b55fab JH |
520 | for (p_start = p; *p; p++) { |
521 | #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f')) | |
522 | if (!ishex(*p)) | |
1c4675dc | 523 | counter = 0; |
524 | else if (++counter == hexsz && | |
e8b55fab | 525 | !ishex(*(p+1))) { |
511dca80 | 526 | struct object_id oid; |
e8b55fab JH |
527 | const char *name = NULL; |
528 | char c = *(p+1); | |
7c5b1675 | 529 | int p_len = p - p_start + 1; |
e8b55fab | 530 | |
1c4675dc | 531 | counter = 0; |
e8b55fab JH |
532 | |
533 | *(p+1) = 0; | |
d850b7a5 | 534 | if (!repo_get_oid(the_repository, p - (hexsz - 1), &oid)) { |
e8b55fab | 535 | struct object *o = |
d0229abd | 536 | lookup_object(the_repository, &oid); |
e8b55fab | 537 | if (o) |
903fc7da | 538 | name = get_rev_name(o, &buf); |
e8b55fab JH |
539 | } |
540 | *(p+1) = c; | |
541 | ||
542 | if (!name) | |
543 | continue; | |
544 | ||
7c5b1675 | 545 | if (data->name_only) |
1c4675dc | 546 | printf("%.*s%s", p_len - hexsz, p_start, name); |
7c5b1675 RS |
547 | else |
548 | printf("%.*s (%s)", p_len, p_start, name); | |
e8b55fab JH |
549 | p_start = p + 1; |
550 | } | |
551 | } | |
552 | ||
553 | /* flush */ | |
554 | if (p_start != p) | |
555 | fwrite(p_start, p - p_start, 1, stdout); | |
903fc7da JK |
556 | |
557 | strbuf_release(&buf); | |
e8b55fab JH |
558 | } |
559 | ||
d6b64ed0 | 560 | int cmd_name_rev(int argc, const char **argv, const char *prefix) |
bd321bcc | 561 | { |
f39addd0 | 562 | struct mem_pool string_pool; |
3cd47459 | 563 | struct object_array revs = OBJECT_ARRAY_INIT; |
34ae3b70 | 564 | int all = 0, annotate_stdin = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0; |
96415b49 | 565 | struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP }; |
edefb1a2 | 566 | struct option opts[] = { |
4279000d | 567 | OPT_BOOL(0, "name-only", &data.name_only, N_("print only ref-based names (no object names)")), |
d5d09d47 | 568 | OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")), |
290be667 | 569 | OPT_STRING_LIST(0, "refs", &data.ref_filters, N_("pattern"), |
422cad0a | 570 | N_("only use refs matching <pattern>")), |
96415b49 JK |
571 | OPT_STRING_LIST(0, "exclude", &data.exclude_filters, N_("pattern"), |
572 | N_("ignore refs matching <pattern>")), | |
edefb1a2 | 573 | OPT_GROUP(""), |
d5d09d47 | 574 | OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")), |
9019d7dc JC |
575 | OPT_BOOL_F(0, |
576 | "stdin", | |
577 | &transform_stdin, | |
578 | N_("deprecated: use --annotate-stdin instead"), | |
579 | PARSE_OPT_HIDDEN), | |
34ae3b70 | 580 | OPT_BOOL(0, "annotate-stdin", &annotate_stdin, N_("annotate text from stdin")), |
d5d09d47 SB |
581 | OPT_BOOL(0, "undefined", &allow_undefined, N_("allow to print `undefined` names (default)")), |
582 | OPT_BOOL(0, "always", &always, | |
422cad0a | 583 | N_("show abbreviated commit object as fallback")), |
078c4253 RS |
584 | OPT_HIDDEN_BOOL(0, "peel-tag", &peel_tag, |
585 | N_("dereference tags in the input (internal use)")), | |
edefb1a2 PH |
586 | OPT_END(), |
587 | }; | |
bd321bcc | 588 | |
f39addd0 | 589 | mem_pool_init(&string_pool, 0); |
8fd79a73 | 590 | init_commit_rev_name(&rev_names); |
ef90d6d4 | 591 | git_config(git_default_config, NULL); |
37782920 | 592 | argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0); |
34ae3b70 JC |
593 | |
594 | if (transform_stdin) { | |
595 | warning("--stdin is deprecated. Please use --annotate-stdin instead, " | |
596 | "which is functionally equivalent.\n" | |
597 | "This option will be removed in a future release."); | |
598 | annotate_stdin = 1; | |
599 | } | |
600 | ||
601 | if (all + annotate_stdin + !!argc > 1) { | |
edefb1a2 PH |
602 | error("Specify either a list, or --all, not both!"); |
603 | usage_with_options(name_rev_usage, opts); | |
604 | } | |
34ae3b70 | 605 | if (all || annotate_stdin) |
2e8ea40f | 606 | disable_cutoff(); |
bd321bcc | 607 | |
edefb1a2 | 608 | for (; argc; argc--, argv++) { |
511dca80 | 609 | struct object_id oid; |
118aa4ac | 610 | struct object *object; |
bd321bcc JS |
611 | struct commit *commit; |
612 | ||
d850b7a5 | 613 | if (repo_get_oid(the_repository, *argv, &oid)) { |
bd321bcc JS |
614 | fprintf(stderr, "Could not get sha1 for %s. Skipping.\n", |
615 | *argv); | |
616 | continue; | |
617 | } | |
618 | ||
118aa4ac | 619 | commit = NULL; |
109cd76d | 620 | object = parse_object(the_repository, &oid); |
118aa4ac | 621 | if (object) { |
a74093da SB |
622 | struct object *peeled = deref_tag(the_repository, |
623 | object, *argv, 0); | |
118aa4ac JH |
624 | if (peeled && peeled->type == OBJ_COMMIT) |
625 | commit = (struct commit *)peeled; | |
626 | } | |
627 | ||
628 | if (!object) { | |
629 | fprintf(stderr, "Could not get object for %s. Skipping.\n", | |
bd321bcc JS |
630 | *argv); |
631 | continue; | |
632 | } | |
633 | ||
2e8ea40f JK |
634 | if (commit) |
635 | set_commit_cutoff(commit); | |
adfc1857 JH |
636 | |
637 | if (peel_tag) { | |
638 | if (!commit) { | |
639 | fprintf(stderr, "Could not get commit for %s. Skipping.\n", | |
640 | *argv); | |
641 | continue; | |
642 | } | |
643 | object = (struct object *)commit; | |
644 | } | |
118aa4ac | 645 | add_object_array(object, *argv, &revs); |
bd321bcc JS |
646 | } |
647 | ||
2e8ea40f JK |
648 | adjust_cutoff_timestamp_for_slop(); |
649 | ||
73868486 | 650 | for_each_ref(name_ref, &data); |
f39addd0 | 651 | name_tips(&string_pool); |
bd321bcc | 652 | |
34ae3b70 | 653 | if (annotate_stdin) { |
a2585719 | 654 | struct strbuf sb = STRBUF_INIT; |
bd321bcc | 655 | |
a2585719 JC |
656 | while (strbuf_getline(&sb, stdin) != EOF) { |
657 | strbuf_addch(&sb, '\n'); | |
658 | name_rev_line(sb.buf, &data); | |
bd321bcc | 659 | } |
a2585719 | 660 | strbuf_release(&sb); |
bd321bcc | 661 | } else if (all) { |
fc046a75 | 662 | int i, max; |
bd321bcc | 663 | |
fc046a75 | 664 | max = get_max_object_index(); |
a83123de BS |
665 | for (i = 0; i < max; i++) { |
666 | struct object *obj = get_indexed_object(i); | |
e8b14d7e | 667 | if (!obj || obj->type != OBJ_COMMIT) |
a83123de BS |
668 | continue; |
669 | show_name(obj, NULL, | |
da2478db | 670 | always, allow_undefined, data.name_only); |
a83123de | 671 | } |
1f1e895f LT |
672 | } else { |
673 | int i; | |
da2478db JH |
674 | for (i = 0; i < revs.nr; i++) |
675 | show_name(revs.objects[i].item, revs.objects[i].name, | |
676 | always, allow_undefined, data.name_only); | |
1f1e895f | 677 | } |
bd321bcc | 678 | |
f39addd0 | 679 | UNLEAK(string_pool); |
886e1084 | 680 | UNLEAK(revs); |
bd321bcc JS |
681 | return 0; |
682 | } |