2 #include "environment.h"
5 #include "repository.h"
10 #include "object-name.h"
12 #include "parse-options.h"
13 #include "prio-queue.h"
14 #include "hash-lookup.h"
15 #include "commit-slab.h"
16 #include "commit-graph.h"
17 #include "wildmatch.h"
20 * One day. See the 'name a rev shortly after epoch' test in t6120 when
23 #define CUTOFF_DATE_SLOP 86400
27 timestamp_t taggerdate
;
33 define_commit_slab(commit_rev_name
, struct rev_name
);
35 static timestamp_t generation_cutoff
= GENERATION_NUMBER_INFINITY
;
36 static timestamp_t cutoff
= TIME_MAX
;
37 static struct commit_rev_name rev_names
;
39 /* Disable the cutoff checks entirely */
40 static void disable_cutoff(void)
42 generation_cutoff
= 0;
46 /* Cutoff searching any commits older than this one */
47 static void set_commit_cutoff(struct commit
*commit
)
50 if (cutoff
> commit
->date
)
51 cutoff
= commit
->date
;
53 if (generation_cutoff
) {
54 timestamp_t generation
= commit_graph_generation(commit
);
56 if (generation_cutoff
> generation
)
57 generation_cutoff
= generation
;
61 /* adjust the commit date cutoff with a slop to allow for slightly incorrect
62 * commit timestamps in case of clock skew.
64 static void adjust_cutoff_timestamp_for_slop(void)
67 /* check for undeflow */
68 if (cutoff
> TIME_MIN
+ CUTOFF_DATE_SLOP
)
69 cutoff
= cutoff
- CUTOFF_DATE_SLOP
;
75 /* Check if a commit is before the cutoff. Prioritize generation numbers
76 * first, but use the commit timestamp if we lack generation data.
78 static int commit_is_before_cutoff(struct commit
*commit
)
80 if (generation_cutoff
< GENERATION_NUMBER_INFINITY
)
81 return generation_cutoff
&&
82 commit_graph_generation(commit
) < generation_cutoff
;
84 return commit
->date
< cutoff
;
87 /* How many generations are maximally preferred over _one_ merge traversal? */
88 #define MERGE_TRAVERSAL_WEIGHT 65535
90 static int is_valid_rev_name(const struct rev_name
*name
)
92 return name
&& name
->tip_name
;
95 static struct rev_name
*get_commit_rev_name(const struct commit
*commit
)
97 struct rev_name
*name
= commit_rev_name_peek(&rev_names
, commit
);
99 return is_valid_rev_name(name
) ? name
: NULL
;
102 static int effective_distance(int distance
, int generation
)
104 return distance
+ (generation
> 0 ? MERGE_TRAVERSAL_WEIGHT
: 0);
107 static int is_better_name(struct rev_name
*name
,
108 timestamp_t taggerdate
,
113 int name_distance
= effective_distance(name
->distance
, name
->generation
);
114 int new_distance
= effective_distance(distance
, generation
);
116 /* If both are tags, we prefer the nearer one. */
117 if (from_tag
&& name
->from_tag
)
118 return name_distance
> new_distance
;
120 /* Favor a tag over a non-tag. */
121 if (name
->from_tag
!= from_tag
)
125 * We are now looking at two non-tags. Tiebreak to favor
128 if (name_distance
!= new_distance
)
129 return name_distance
> new_distance
;
131 /* ... or tiebreak to favor older date */
132 if (name
->taggerdate
!= taggerdate
)
133 return name
->taggerdate
> taggerdate
;
135 /* keep the current one if we cannot decide */
139 static struct rev_name
*create_or_update_name(struct commit
*commit
,
140 timestamp_t taggerdate
,
141 int generation
, int distance
,
144 struct rev_name
*name
= commit_rev_name_at(&rev_names
, commit
);
146 if (is_valid_rev_name(name
) &&
147 !is_better_name(name
, taggerdate
, generation
, distance
, from_tag
))
150 name
->taggerdate
= taggerdate
;
151 name
->generation
= generation
;
152 name
->distance
= distance
;
153 name
->from_tag
= from_tag
;
158 static char *get_parent_name(const struct rev_name
*name
, int parent_number
)
160 struct strbuf sb
= STRBUF_INIT
;
163 strip_suffix(name
->tip_name
, "^0", &len
);
164 if (name
->generation
> 0) {
165 strbuf_grow(&sb
, len
+
166 1 + decimal_width(name
->generation
) +
167 1 + decimal_width(parent_number
));
168 strbuf_addf(&sb
, "%.*s~%d^%d", (int)len
, name
->tip_name
,
169 name
->generation
, parent_number
);
171 strbuf_grow(&sb
, len
+
172 1 + decimal_width(parent_number
));
173 strbuf_addf(&sb
, "%.*s^%d", (int)len
, name
->tip_name
,
176 return strbuf_detach(&sb
, NULL
);
179 static void name_rev(struct commit
*start_commit
,
180 const char *tip_name
, timestamp_t taggerdate
,
181 int from_tag
, int deref
)
183 struct prio_queue queue
;
184 struct commit
*commit
;
185 struct commit
**parents_to_queue
= NULL
;
186 size_t parents_to_queue_nr
, parents_to_queue_alloc
= 0;
187 struct rev_name
*start_name
;
189 repo_parse_commit(the_repository
, start_commit
);
190 if (commit_is_before_cutoff(start_commit
))
193 start_name
= create_or_update_name(start_commit
, taggerdate
, 0, 0,
198 start_name
->tip_name
= xstrfmt("%s^0", tip_name
);
200 start_name
->tip_name
= xstrdup(tip_name
);
202 memset(&queue
, 0, sizeof(queue
)); /* Use the prio_queue as LIFO */
203 prio_queue_put(&queue
, start_commit
);
205 while ((commit
= prio_queue_get(&queue
))) {
206 struct rev_name
*name
= get_commit_rev_name(commit
);
207 struct commit_list
*parents
;
208 int parent_number
= 1;
210 parents_to_queue_nr
= 0;
212 for (parents
= commit
->parents
;
214 parents
= parents
->next
, parent_number
++) {
215 struct commit
*parent
= parents
->item
;
216 struct rev_name
*parent_name
;
217 int generation
, distance
;
219 repo_parse_commit(the_repository
, parent
);
220 if (commit_is_before_cutoff(parent
))
223 if (parent_number
> 1) {
225 distance
= name
->distance
+ MERGE_TRAVERSAL_WEIGHT
;
227 generation
= name
->generation
+ 1;
228 distance
= name
->distance
+ 1;
231 parent_name
= create_or_update_name(parent
, taggerdate
,
235 if (parent_number
> 1)
236 parent_name
->tip_name
=
237 get_parent_name(name
,
240 parent_name
->tip_name
= name
->tip_name
;
241 ALLOC_GROW(parents_to_queue
,
242 parents_to_queue_nr
+ 1,
243 parents_to_queue_alloc
);
244 parents_to_queue
[parents_to_queue_nr
] = parent
;
245 parents_to_queue_nr
++;
249 /* The first parent must come out first from the prio_queue */
250 while (parents_to_queue_nr
)
251 prio_queue_put(&queue
,
252 parents_to_queue
[--parents_to_queue_nr
]);
255 clear_prio_queue(&queue
);
256 free(parents_to_queue
);
259 static int subpath_matches(const char *path
, const char *filter
)
261 const char *subpath
= path
;
264 if (!wildmatch(filter
, subpath
, 0))
265 return subpath
- path
;
266 subpath
= strchr(subpath
, '/');
273 struct name_ref_data
{
276 struct string_list ref_filters
;
277 struct string_list exclude_filters
;
280 static struct tip_table
{
281 struct tip_table_entry
{
282 struct object_id oid
;
284 struct commit
*commit
;
285 timestamp_t taggerdate
;
286 unsigned int from_tag
:1;
287 unsigned int deref
:1;
294 static void add_to_tip_table(const struct object_id
*oid
, const char *refname
,
295 int shorten_unambiguous
, struct commit
*commit
,
296 timestamp_t taggerdate
, int from_tag
, int deref
)
298 char *short_refname
= NULL
;
300 if (shorten_unambiguous
)
301 short_refname
= shorten_unambiguous_ref(refname
, 0);
302 else if (skip_prefix(refname
, "refs/heads/", &refname
))
303 ; /* refname already advanced */
305 skip_prefix(refname
, "refs/", &refname
);
307 ALLOC_GROW(tip_table
.table
, tip_table
.nr
+ 1, tip_table
.alloc
);
308 oidcpy(&tip_table
.table
[tip_table
.nr
].oid
, oid
);
309 tip_table
.table
[tip_table
.nr
].refname
= short_refname
?
310 short_refname
: xstrdup(refname
);
311 tip_table
.table
[tip_table
.nr
].commit
= commit
;
312 tip_table
.table
[tip_table
.nr
].taggerdate
= taggerdate
;
313 tip_table
.table
[tip_table
.nr
].from_tag
= from_tag
;
314 tip_table
.table
[tip_table
.nr
].deref
= deref
;
316 tip_table
.sorted
= 0;
319 static int tipcmp(const void *a_
, const void *b_
)
321 const struct tip_table_entry
*a
= a_
, *b
= b_
;
322 return oidcmp(&a
->oid
, &b
->oid
);
325 static int cmp_by_tag_and_age(const void *a_
, const void *b_
)
327 const struct tip_table_entry
*a
= a_
, *b
= b_
;
331 cmp
= b
->from_tag
- a
->from_tag
;
335 /* Older is better. */
336 if (a
->taggerdate
< b
->taggerdate
)
338 return a
->taggerdate
!= b
->taggerdate
;
341 static int name_ref(const char *path
, const struct object_id
*oid
,
342 int flags UNUSED
, void *cb_data
)
344 struct object
*o
= parse_object(the_repository
, oid
);
345 struct name_ref_data
*data
= cb_data
;
346 int can_abbreviate_output
= data
->tags_only
&& data
->name_only
;
349 struct commit
*commit
= NULL
;
350 timestamp_t taggerdate
= TIME_MAX
;
352 if (data
->tags_only
&& !starts_with(path
, "refs/tags/"))
355 if (data
->exclude_filters
.nr
) {
356 struct string_list_item
*item
;
358 for_each_string_list_item(item
, &data
->exclude_filters
) {
359 if (subpath_matches(path
, item
->string
) >= 0)
364 if (data
->ref_filters
.nr
) {
365 struct string_list_item
*item
;
368 /* See if any of the patterns match. */
369 for_each_string_list_item(item
, &data
->ref_filters
) {
371 * Check all patterns even after finding a match, so
372 * that we can see if a match with a subpath exists.
373 * When a user asked for 'refs/tags/v*' and 'v1.*',
374 * both of which match, the user is showing her
375 * willingness to accept a shortened output by having
376 * the 'v1.*' in the acceptable refnames, so we
377 * shouldn't stop when seeing 'refs/tags/v1.4' matches
378 * 'refs/tags/v*'. We should show it as 'v1.4'.
380 switch (subpath_matches(path
, item
->string
)) {
381 case -1: /* did not match */
383 case 0: /* matched fully */
386 default: /* matched subpath */
388 can_abbreviate_output
= 1;
393 /* If none of the patterns matched, stop now */
398 while (o
&& o
->type
== OBJ_TAG
) {
399 struct tag
*t
= (struct tag
*) o
;
401 break; /* broken repository */
402 o
= parse_object(the_repository
, &t
->tagged
->oid
);
404 taggerdate
= t
->date
;
406 if (o
&& o
->type
== OBJ_COMMIT
) {
407 commit
= (struct commit
*)o
;
408 from_tag
= starts_with(path
, "refs/tags/");
409 if (taggerdate
== TIME_MAX
)
410 taggerdate
= commit
->date
;
413 add_to_tip_table(oid
, path
, can_abbreviate_output
, commit
, taggerdate
,
418 static void name_tips(void)
423 * Try to set better names first, so that worse ones spread
426 QSORT(tip_table
.table
, tip_table
.nr
, cmp_by_tag_and_age
);
427 for (i
= 0; i
< tip_table
.nr
; i
++) {
428 struct tip_table_entry
*e
= &tip_table
.table
[i
];
430 name_rev(e
->commit
, e
->refname
, e
->taggerdate
,
431 e
->from_tag
, e
->deref
);
436 static const struct object_id
*nth_tip_table_ent(size_t ix
, const void *table_
)
438 const struct tip_table_entry
*table
= table_
;
439 return &table
[ix
].oid
;
442 static const char *get_exact_ref_match(const struct object
*o
)
446 if (!tip_table
.table
|| !tip_table
.nr
)
449 if (!tip_table
.sorted
) {
450 QSORT(tip_table
.table
, tip_table
.nr
, tipcmp
);
451 tip_table
.sorted
= 1;
454 found
= oid_pos(&o
->oid
, tip_table
.table
, tip_table
.nr
,
457 return tip_table
.table
[found
].refname
;
461 /* may return a constant string or use "buf" as scratch space */
462 static const char *get_rev_name(const struct object
*o
, struct strbuf
*buf
)
465 const struct commit
*c
;
467 if (o
->type
!= OBJ_COMMIT
)
468 return get_exact_ref_match(o
);
469 c
= (const struct commit
*) o
;
470 n
= get_commit_rev_name(c
);
478 strbuf_addstr(buf
, n
->tip_name
);
479 strbuf_strip_suffix(buf
, "^0");
480 strbuf_addf(buf
, "~%d", n
->generation
);
485 static void show_name(const struct object
*obj
,
486 const char *caller_name
,
487 int always
, int allow_undefined
, int name_only
)
490 const struct object_id
*oid
= &obj
->oid
;
491 struct strbuf buf
= STRBUF_INIT
;
494 printf("%s ", caller_name
? caller_name
: oid_to_hex(oid
));
495 name
= get_rev_name(obj
, &buf
);
497 printf("%s\n", name
);
498 else if (allow_undefined
)
499 printf("undefined\n");
502 repo_find_unique_abbrev(the_repository
, oid
, DEFAULT_ABBREV
));
504 die("cannot describe '%s'", oid_to_hex(oid
));
505 strbuf_release(&buf
);
508 static char const * const name_rev_usage
[] = {
509 N_("git name-rev [<options>] <commit>..."),
510 N_("git name-rev [<options>] --all"),
511 N_("git name-rev [<options>] --annotate-stdin"),
515 static void name_rev_line(char *p
, struct name_ref_data
*data
)
517 struct strbuf buf
= STRBUF_INIT
;
520 const unsigned hexsz
= the_hash_algo
->hexsz
;
522 for (p_start
= p
; *p
; p
++) {
523 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
526 else if (++counter
== hexsz
&&
528 struct object_id oid
;
529 const char *name
= NULL
;
531 int p_len
= p
- p_start
+ 1;
536 if (!repo_get_oid(the_repository
, p
- (hexsz
- 1), &oid
)) {
538 lookup_object(the_repository
, &oid
);
540 name
= get_rev_name(o
, &buf
);
548 printf("%.*s%s", p_len
- hexsz
, p_start
, name
);
550 printf("%.*s (%s)", p_len
, p_start
, name
);
557 fwrite(p_start
, p
- p_start
, 1, stdout
);
559 strbuf_release(&buf
);
562 int cmd_name_rev(int argc
, const char **argv
, const char *prefix
)
564 struct object_array revs
= OBJECT_ARRAY_INIT
;
565 int all
= 0, annotate_stdin
= 0, transform_stdin
= 0, allow_undefined
= 1, always
= 0, peel_tag
= 0;
566 struct name_ref_data data
= { 0, 0, STRING_LIST_INIT_NODUP
, STRING_LIST_INIT_NODUP
};
567 struct option opts
[] = {
568 OPT_BOOL(0, "name-only", &data
.name_only
, N_("print only ref-based names (no object names)")),
569 OPT_BOOL(0, "tags", &data
.tags_only
, N_("only use tags to name the commits")),
570 OPT_STRING_LIST(0, "refs", &data
.ref_filters
, N_("pattern"),
571 N_("only use refs matching <pattern>")),
572 OPT_STRING_LIST(0, "exclude", &data
.exclude_filters
, N_("pattern"),
573 N_("ignore refs matching <pattern>")),
575 OPT_BOOL(0, "all", &all
, N_("list all commits reachable from all refs")),
579 N_("deprecated: use --annotate-stdin instead"),
581 OPT_BOOL(0, "annotate-stdin", &annotate_stdin
, N_("annotate text from stdin")),
582 OPT_BOOL(0, "undefined", &allow_undefined
, N_("allow to print `undefined` names (default)")),
583 OPT_BOOL(0, "always", &always
,
584 N_("show abbreviated commit object as fallback")),
586 /* A Hidden OPT_BOOL */
587 OPTION_SET_INT
, 0, "peel-tag", &peel_tag
, NULL
,
588 N_("dereference tags in the input (internal use)"),
589 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
, NULL
, 1,
594 init_commit_rev_name(&rev_names
);
595 git_config(git_default_config
, NULL
);
596 argc
= parse_options(argc
, argv
, prefix
, opts
, name_rev_usage
, 0);
598 if (transform_stdin
) {
599 warning("--stdin is deprecated. Please use --annotate-stdin instead, "
600 "which is functionally equivalent.\n"
601 "This option will be removed in a future release.");
605 if (all
+ annotate_stdin
+ !!argc
> 1) {
606 error("Specify either a list, or --all, not both!");
607 usage_with_options(name_rev_usage
, opts
);
609 if (all
|| annotate_stdin
)
612 for (; argc
; argc
--, argv
++) {
613 struct object_id oid
;
614 struct object
*object
;
615 struct commit
*commit
;
617 if (repo_get_oid(the_repository
, *argv
, &oid
)) {
618 fprintf(stderr
, "Could not get sha1 for %s. Skipping.\n",
624 object
= parse_object(the_repository
, &oid
);
626 struct object
*peeled
= deref_tag(the_repository
,
628 if (peeled
&& peeled
->type
== OBJ_COMMIT
)
629 commit
= (struct commit
*)peeled
;
633 fprintf(stderr
, "Could not get object for %s. Skipping.\n",
639 set_commit_cutoff(commit
);
643 fprintf(stderr
, "Could not get commit for %s. Skipping.\n",
647 object
= (struct object
*)commit
;
649 add_object_array(object
, *argv
, &revs
);
652 adjust_cutoff_timestamp_for_slop();
654 for_each_ref(name_ref
, &data
);
657 if (annotate_stdin
) {
658 struct strbuf sb
= STRBUF_INIT
;
660 while (strbuf_getline(&sb
, stdin
) != EOF
) {
661 strbuf_addch(&sb
, '\n');
662 name_rev_line(sb
.buf
, &data
);
668 max
= get_max_object_index();
669 for (i
= 0; i
< max
; i
++) {
670 struct object
*obj
= get_indexed_object(i
);
671 if (!obj
|| obj
->type
!= OBJ_COMMIT
)
674 always
, allow_undefined
, data
.name_only
);
678 for (i
= 0; i
< revs
.nr
; i
++)
679 show_name(revs
.objects
[i
].item
, revs
.objects
[i
].name
,
680 always
, allow_undefined
, data
.name_only
);