3 #include "environment.h"
6 #include "repository.h"
11 #include "object-name.h"
13 #include "parse-options.h"
14 #include "prio-queue.h"
15 #include "hash-lookup.h"
16 #include "commit-slab.h"
17 #include "commit-graph.h"
18 #include "wildmatch.h"
21 * One day. See the 'name a rev shortly after epoch' test in t6120 when
24 #define CUTOFF_DATE_SLOP 86400
28 timestamp_t taggerdate
;
34 define_commit_slab(commit_rev_name
, struct rev_name
);
36 static timestamp_t generation_cutoff
= GENERATION_NUMBER_INFINITY
;
37 static timestamp_t cutoff
= TIME_MAX
;
38 static struct commit_rev_name rev_names
;
40 /* Disable the cutoff checks entirely */
41 static void disable_cutoff(void)
43 generation_cutoff
= 0;
47 /* Cutoff searching any commits older than this one */
48 static void set_commit_cutoff(struct commit
*commit
)
51 if (cutoff
> commit
->date
)
52 cutoff
= commit
->date
;
54 if (generation_cutoff
) {
55 timestamp_t generation
= commit_graph_generation(commit
);
57 if (generation_cutoff
> generation
)
58 generation_cutoff
= generation
;
62 /* adjust the commit date cutoff with a slop to allow for slightly incorrect
63 * commit timestamps in case of clock skew.
65 static void adjust_cutoff_timestamp_for_slop(void)
68 /* check for undeflow */
69 if (cutoff
> TIME_MIN
+ CUTOFF_DATE_SLOP
)
70 cutoff
= cutoff
- CUTOFF_DATE_SLOP
;
76 /* Check if a commit is before the cutoff. Prioritize generation numbers
77 * first, but use the commit timestamp if we lack generation data.
79 static int commit_is_before_cutoff(struct commit
*commit
)
81 if (generation_cutoff
< GENERATION_NUMBER_INFINITY
)
82 return generation_cutoff
&&
83 commit_graph_generation(commit
) < generation_cutoff
;
85 return commit
->date
< cutoff
;
88 /* How many generations are maximally preferred over _one_ merge traversal? */
89 #define MERGE_TRAVERSAL_WEIGHT 65535
91 static int is_valid_rev_name(const struct rev_name
*name
)
93 return name
&& name
->tip_name
;
96 static struct rev_name
*get_commit_rev_name(const struct commit
*commit
)
98 struct rev_name
*name
= commit_rev_name_peek(&rev_names
, commit
);
100 return is_valid_rev_name(name
) ? name
: NULL
;
103 static int effective_distance(int distance
, int generation
)
105 return distance
+ (generation
> 0 ? MERGE_TRAVERSAL_WEIGHT
: 0);
108 static int is_better_name(struct rev_name
*name
,
109 timestamp_t taggerdate
,
114 int name_distance
= effective_distance(name
->distance
, name
->generation
);
115 int new_distance
= effective_distance(distance
, generation
);
117 /* If both are tags, we prefer the nearer one. */
118 if (from_tag
&& name
->from_tag
)
119 return name_distance
> new_distance
;
121 /* Favor a tag over a non-tag. */
122 if (name
->from_tag
!= from_tag
)
126 * We are now looking at two non-tags. Tiebreak to favor
129 if (name_distance
!= new_distance
)
130 return name_distance
> new_distance
;
132 /* ... or tiebreak to favor older date */
133 if (name
->taggerdate
!= taggerdate
)
134 return name
->taggerdate
> taggerdate
;
136 /* keep the current one if we cannot decide */
140 static struct rev_name
*create_or_update_name(struct commit
*commit
,
141 timestamp_t taggerdate
,
142 int generation
, int distance
,
145 struct rev_name
*name
= commit_rev_name_at(&rev_names
, commit
);
147 if (is_valid_rev_name(name
) &&
148 !is_better_name(name
, taggerdate
, generation
, distance
, from_tag
))
151 name
->taggerdate
= taggerdate
;
152 name
->generation
= generation
;
153 name
->distance
= distance
;
154 name
->from_tag
= from_tag
;
159 static char *get_parent_name(const struct rev_name
*name
, int parent_number
)
161 struct strbuf sb
= STRBUF_INIT
;
164 strip_suffix(name
->tip_name
, "^0", &len
);
165 if (name
->generation
> 0) {
166 strbuf_grow(&sb
, len
+
167 1 + decimal_width(name
->generation
) +
168 1 + decimal_width(parent_number
));
169 strbuf_addf(&sb
, "%.*s~%d^%d", (int)len
, name
->tip_name
,
170 name
->generation
, parent_number
);
172 strbuf_grow(&sb
, len
+
173 1 + decimal_width(parent_number
));
174 strbuf_addf(&sb
, "%.*s^%d", (int)len
, name
->tip_name
,
177 return strbuf_detach(&sb
, NULL
);
180 static void name_rev(struct commit
*start_commit
,
181 const char *tip_name
, timestamp_t taggerdate
,
182 int from_tag
, int deref
)
184 struct prio_queue queue
;
185 struct commit
*commit
;
186 struct commit
**parents_to_queue
= NULL
;
187 size_t parents_to_queue_nr
, parents_to_queue_alloc
= 0;
188 struct rev_name
*start_name
;
190 repo_parse_commit(the_repository
, start_commit
);
191 if (commit_is_before_cutoff(start_commit
))
194 start_name
= create_or_update_name(start_commit
, taggerdate
, 0, 0,
199 start_name
->tip_name
= xstrfmt("%s^0", tip_name
);
201 start_name
->tip_name
= xstrdup(tip_name
);
203 memset(&queue
, 0, sizeof(queue
)); /* Use the prio_queue as LIFO */
204 prio_queue_put(&queue
, start_commit
);
206 while ((commit
= prio_queue_get(&queue
))) {
207 struct rev_name
*name
= get_commit_rev_name(commit
);
208 struct commit_list
*parents
;
209 int parent_number
= 1;
211 parents_to_queue_nr
= 0;
213 for (parents
= commit
->parents
;
215 parents
= parents
->next
, parent_number
++) {
216 struct commit
*parent
= parents
->item
;
217 struct rev_name
*parent_name
;
218 int generation
, distance
;
220 repo_parse_commit(the_repository
, parent
);
221 if (commit_is_before_cutoff(parent
))
224 if (parent_number
> 1) {
226 distance
= name
->distance
+ MERGE_TRAVERSAL_WEIGHT
;
228 generation
= name
->generation
+ 1;
229 distance
= name
->distance
+ 1;
232 parent_name
= create_or_update_name(parent
, taggerdate
,
236 if (parent_number
> 1)
237 parent_name
->tip_name
=
238 get_parent_name(name
,
241 parent_name
->tip_name
= name
->tip_name
;
242 ALLOC_GROW(parents_to_queue
,
243 parents_to_queue_nr
+ 1,
244 parents_to_queue_alloc
);
245 parents_to_queue
[parents_to_queue_nr
] = parent
;
246 parents_to_queue_nr
++;
250 /* The first parent must come out first from the prio_queue */
251 while (parents_to_queue_nr
)
252 prio_queue_put(&queue
,
253 parents_to_queue
[--parents_to_queue_nr
]);
256 clear_prio_queue(&queue
);
257 free(parents_to_queue
);
260 static int subpath_matches(const char *path
, const char *filter
)
262 const char *subpath
= path
;
265 if (!wildmatch(filter
, subpath
, 0))
266 return subpath
- path
;
267 subpath
= strchr(subpath
, '/');
274 struct name_ref_data
{
277 struct string_list ref_filters
;
278 struct string_list exclude_filters
;
281 static struct tip_table
{
282 struct tip_table_entry
{
283 struct object_id oid
;
285 struct commit
*commit
;
286 timestamp_t taggerdate
;
287 unsigned int from_tag
:1;
288 unsigned int deref
:1;
295 static void add_to_tip_table(const struct object_id
*oid
, const char *refname
,
296 int shorten_unambiguous
, struct commit
*commit
,
297 timestamp_t taggerdate
, int from_tag
, int deref
)
299 char *short_refname
= NULL
;
301 if (shorten_unambiguous
)
302 short_refname
= shorten_unambiguous_ref(refname
, 0);
303 else if (skip_prefix(refname
, "refs/heads/", &refname
))
304 ; /* refname already advanced */
306 skip_prefix(refname
, "refs/", &refname
);
308 ALLOC_GROW(tip_table
.table
, tip_table
.nr
+ 1, tip_table
.alloc
);
309 oidcpy(&tip_table
.table
[tip_table
.nr
].oid
, oid
);
310 tip_table
.table
[tip_table
.nr
].refname
= short_refname
?
311 short_refname
: xstrdup(refname
);
312 tip_table
.table
[tip_table
.nr
].commit
= commit
;
313 tip_table
.table
[tip_table
.nr
].taggerdate
= taggerdate
;
314 tip_table
.table
[tip_table
.nr
].from_tag
= from_tag
;
315 tip_table
.table
[tip_table
.nr
].deref
= deref
;
317 tip_table
.sorted
= 0;
320 static int tipcmp(const void *a_
, const void *b_
)
322 const struct tip_table_entry
*a
= a_
, *b
= b_
;
323 return oidcmp(&a
->oid
, &b
->oid
);
326 static int cmp_by_tag_and_age(const void *a_
, const void *b_
)
328 const struct tip_table_entry
*a
= a_
, *b
= b_
;
332 cmp
= b
->from_tag
- a
->from_tag
;
336 /* Older is better. */
337 if (a
->taggerdate
< b
->taggerdate
)
339 return a
->taggerdate
!= b
->taggerdate
;
342 static int name_ref(const char *path
, const struct object_id
*oid
,
343 int flags UNUSED
, void *cb_data
)
345 struct object
*o
= parse_object(the_repository
, oid
);
346 struct name_ref_data
*data
= cb_data
;
347 int can_abbreviate_output
= data
->tags_only
&& data
->name_only
;
350 struct commit
*commit
= NULL
;
351 timestamp_t taggerdate
= TIME_MAX
;
353 if (data
->tags_only
&& !starts_with(path
, "refs/tags/"))
356 if (data
->exclude_filters
.nr
) {
357 struct string_list_item
*item
;
359 for_each_string_list_item(item
, &data
->exclude_filters
) {
360 if (subpath_matches(path
, item
->string
) >= 0)
365 if (data
->ref_filters
.nr
) {
366 struct string_list_item
*item
;
369 /* See if any of the patterns match. */
370 for_each_string_list_item(item
, &data
->ref_filters
) {
372 * Check all patterns even after finding a match, so
373 * that we can see if a match with a subpath exists.
374 * When a user asked for 'refs/tags/v*' and 'v1.*',
375 * both of which match, the user is showing her
376 * willingness to accept a shortened output by having
377 * the 'v1.*' in the acceptable refnames, so we
378 * shouldn't stop when seeing 'refs/tags/v1.4' matches
379 * 'refs/tags/v*'. We should show it as 'v1.4'.
381 switch (subpath_matches(path
, item
->string
)) {
382 case -1: /* did not match */
384 case 0: /* matched fully */
387 default: /* matched subpath */
389 can_abbreviate_output
= 1;
394 /* If none of the patterns matched, stop now */
399 while (o
&& o
->type
== OBJ_TAG
) {
400 struct tag
*t
= (struct tag
*) o
;
402 break; /* broken repository */
403 o
= parse_object(the_repository
, &t
->tagged
->oid
);
405 taggerdate
= t
->date
;
407 if (o
&& o
->type
== OBJ_COMMIT
) {
408 commit
= (struct commit
*)o
;
409 from_tag
= starts_with(path
, "refs/tags/");
410 if (taggerdate
== TIME_MAX
)
411 taggerdate
= commit
->date
;
414 add_to_tip_table(oid
, path
, can_abbreviate_output
, commit
, taggerdate
,
419 static void name_tips(void)
424 * Try to set better names first, so that worse ones spread
427 QSORT(tip_table
.table
, tip_table
.nr
, cmp_by_tag_and_age
);
428 for (i
= 0; i
< tip_table
.nr
; i
++) {
429 struct tip_table_entry
*e
= &tip_table
.table
[i
];
431 name_rev(e
->commit
, e
->refname
, e
->taggerdate
,
432 e
->from_tag
, e
->deref
);
437 static const struct object_id
*nth_tip_table_ent(size_t ix
, const void *table_
)
439 const struct tip_table_entry
*table
= table_
;
440 return &table
[ix
].oid
;
443 static const char *get_exact_ref_match(const struct object
*o
)
447 if (!tip_table
.table
|| !tip_table
.nr
)
450 if (!tip_table
.sorted
) {
451 QSORT(tip_table
.table
, tip_table
.nr
, tipcmp
);
452 tip_table
.sorted
= 1;
455 found
= oid_pos(&o
->oid
, tip_table
.table
, tip_table
.nr
,
458 return tip_table
.table
[found
].refname
;
462 /* may return a constant string or use "buf" as scratch space */
463 static const char *get_rev_name(const struct object
*o
, struct strbuf
*buf
)
466 const struct commit
*c
;
468 if (o
->type
!= OBJ_COMMIT
)
469 return get_exact_ref_match(o
);
470 c
= (const struct commit
*) o
;
471 n
= get_commit_rev_name(c
);
479 strbuf_addstr(buf
, n
->tip_name
);
480 strbuf_strip_suffix(buf
, "^0");
481 strbuf_addf(buf
, "~%d", n
->generation
);
486 static void show_name(const struct object
*obj
,
487 const char *caller_name
,
488 int always
, int allow_undefined
, int name_only
)
491 const struct object_id
*oid
= &obj
->oid
;
492 struct strbuf buf
= STRBUF_INIT
;
495 printf("%s ", caller_name
? caller_name
: oid_to_hex(oid
));
496 name
= get_rev_name(obj
, &buf
);
498 printf("%s\n", name
);
499 else if (allow_undefined
)
500 printf("undefined\n");
503 repo_find_unique_abbrev(the_repository
, oid
, DEFAULT_ABBREV
));
505 die("cannot describe '%s'", oid_to_hex(oid
));
506 strbuf_release(&buf
);
509 static char const * const name_rev_usage
[] = {
510 N_("git name-rev [<options>] <commit>..."),
511 N_("git name-rev [<options>] --all"),
512 N_("git name-rev [<options>] --annotate-stdin"),
516 static void name_rev_line(char *p
, struct name_ref_data
*data
)
518 struct strbuf buf
= STRBUF_INIT
;
521 const unsigned hexsz
= the_hash_algo
->hexsz
;
523 for (p_start
= p
; *p
; p
++) {
524 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
527 else if (++counter
== hexsz
&&
529 struct object_id oid
;
530 const char *name
= NULL
;
532 int p_len
= p
- p_start
+ 1;
537 if (!repo_get_oid(the_repository
, p
- (hexsz
- 1), &oid
)) {
539 lookup_object(the_repository
, &oid
);
541 name
= get_rev_name(o
, &buf
);
549 printf("%.*s%s", p_len
- hexsz
, p_start
, name
);
551 printf("%.*s (%s)", p_len
, p_start
, name
);
558 fwrite(p_start
, p
- p_start
, 1, stdout
);
560 strbuf_release(&buf
);
563 int cmd_name_rev(int argc
, const char **argv
, const char *prefix
)
565 struct object_array revs
= OBJECT_ARRAY_INIT
;
566 int all
= 0, annotate_stdin
= 0, transform_stdin
= 0, allow_undefined
= 1, always
= 0, peel_tag
= 0;
567 struct name_ref_data data
= { 0, 0, STRING_LIST_INIT_NODUP
, STRING_LIST_INIT_NODUP
};
568 struct option opts
[] = {
569 OPT_BOOL(0, "name-only", &data
.name_only
, N_("print only ref-based names (no object names)")),
570 OPT_BOOL(0, "tags", &data
.tags_only
, N_("only use tags to name the commits")),
571 OPT_STRING_LIST(0, "refs", &data
.ref_filters
, N_("pattern"),
572 N_("only use refs matching <pattern>")),
573 OPT_STRING_LIST(0, "exclude", &data
.exclude_filters
, N_("pattern"),
574 N_("ignore refs matching <pattern>")),
576 OPT_BOOL(0, "all", &all
, N_("list all commits reachable from all refs")),
580 N_("deprecated: use --annotate-stdin instead"),
582 OPT_BOOL(0, "annotate-stdin", &annotate_stdin
, N_("annotate text from stdin")),
583 OPT_BOOL(0, "undefined", &allow_undefined
, N_("allow to print `undefined` names (default)")),
584 OPT_BOOL(0, "always", &always
,
585 N_("show abbreviated commit object as fallback")),
587 /* A Hidden OPT_BOOL */
588 OPTION_SET_INT
, 0, "peel-tag", &peel_tag
, NULL
,
589 N_("dereference tags in the input (internal use)"),
590 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
, NULL
, 1,
595 init_commit_rev_name(&rev_names
);
596 git_config(git_default_config
, NULL
);
597 argc
= parse_options(argc
, argv
, prefix
, opts
, name_rev_usage
, 0);
599 if (transform_stdin
) {
600 warning("--stdin is deprecated. Please use --annotate-stdin instead, "
601 "which is functionally equivalent.\n"
602 "This option will be removed in a future release.");
606 if (all
+ annotate_stdin
+ !!argc
> 1) {
607 error("Specify either a list, or --all, not both!");
608 usage_with_options(name_rev_usage
, opts
);
610 if (all
|| annotate_stdin
)
613 for (; argc
; argc
--, argv
++) {
614 struct object_id oid
;
615 struct object
*object
;
616 struct commit
*commit
;
618 if (repo_get_oid(the_repository
, *argv
, &oid
)) {
619 fprintf(stderr
, "Could not get sha1 for %s. Skipping.\n",
625 object
= parse_object(the_repository
, &oid
);
627 struct object
*peeled
= deref_tag(the_repository
,
629 if (peeled
&& peeled
->type
== OBJ_COMMIT
)
630 commit
= (struct commit
*)peeled
;
634 fprintf(stderr
, "Could not get object for %s. Skipping.\n",
640 set_commit_cutoff(commit
);
644 fprintf(stderr
, "Could not get commit for %s. Skipping.\n",
648 object
= (struct object
*)commit
;
650 add_object_array(object
, *argv
, &revs
);
653 adjust_cutoff_timestamp_for_slop();
655 for_each_ref(name_ref
, &data
);
658 if (annotate_stdin
) {
659 struct strbuf sb
= STRBUF_INIT
;
661 while (strbuf_getline(&sb
, stdin
) != EOF
) {
662 strbuf_addch(&sb
, '\n');
663 name_rev_line(sb
.buf
, &data
);
669 max
= get_max_object_index();
670 for (i
= 0; i
< max
; i
++) {
671 struct object
*obj
= get_indexed_object(i
);
672 if (!obj
|| obj
->type
!= OBJ_COMMIT
)
675 always
, allow_undefined
, data
.name_only
);
679 for (i
= 0; i
< revs
.nr
; i
++)
680 show_name(revs
.objects
[i
].item
, revs
.objects
[i
].name
,
681 always
, allow_undefined
, data
.name_only
);