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