3 #include "repository.h"
8 #include "parse-options.h"
9 #include "prio-queue.h"
10 #include "hash-lookup.h"
11 #include "commit-slab.h"
14 * One day. See the 'name a rev shortly after epoch' test in t6120 when
17 #define CUTOFF_DATE_SLOP 86400
21 timestamp_t taggerdate
;
27 define_commit_slab(commit_rev_name
, struct rev_name
);
29 static timestamp_t cutoff
= TIME_MAX
;
30 static struct commit_rev_name rev_names
;
32 /* How many generations are maximally preferred over _one_ merge traversal? */
33 #define MERGE_TRAVERSAL_WEIGHT 65535
35 static int is_valid_rev_name(const struct rev_name
*name
)
37 return name
&& (name
->generation
|| name
->tip_name
);
40 static struct rev_name
*get_commit_rev_name(const struct commit
*commit
)
42 struct rev_name
*name
= commit_rev_name_peek(&rev_names
, commit
);
44 return is_valid_rev_name(name
) ? name
: NULL
;
47 static int effective_distance(int distance
, int generation
)
49 return distance
+ (generation
> 0 ? MERGE_TRAVERSAL_WEIGHT
: 0);
52 static int is_better_name(struct rev_name
*name
,
53 timestamp_t taggerdate
,
58 int name_distance
= effective_distance(name
->distance
, name
->generation
);
59 int new_distance
= effective_distance(distance
, generation
);
62 * When comparing names based on tags, prefer names
63 * based on the older tag, even if it is farther away.
65 if (from_tag
&& name
->from_tag
)
66 return (name
->taggerdate
> taggerdate
||
67 (name
->taggerdate
== taggerdate
&&
68 name_distance
> new_distance
));
71 * We know that at least one of them is a non-tag at this point.
72 * favor a tag over a non-tag.
74 if (name
->from_tag
!= from_tag
)
78 * We are now looking at two non-tags. Tiebreak to favor
81 if (name_distance
!= new_distance
)
82 return name_distance
> new_distance
;
84 /* ... or tiebreak to favor older date */
85 if (name
->taggerdate
!= taggerdate
)
86 return name
->taggerdate
> taggerdate
;
88 /* keep the current one if we cannot decide */
92 static struct rev_name
*create_or_update_name(struct commit
*commit
,
93 timestamp_t taggerdate
,
94 int generation
, int distance
,
97 struct rev_name
*name
= commit_rev_name_at(&rev_names
, commit
);
99 if (is_valid_rev_name(name
)) {
100 if (!is_better_name(name
, taggerdate
, generation
, distance
, from_tag
))
104 * This string might still be shared with ancestors
105 * (generation > 0). We can release it here regardless,
106 * because the new name that has just won will be better
107 * for them as well, so name_rev() will replace these
108 * stale pointers when it processes the parents.
110 if (!name
->generation
)
111 free(name
->tip_name
);
114 name
->taggerdate
= taggerdate
;
115 name
->generation
= generation
;
116 name
->distance
= distance
;
117 name
->from_tag
= from_tag
;
122 static char *get_parent_name(const struct rev_name
*name
, int parent_number
)
124 struct strbuf sb
= STRBUF_INIT
;
127 strip_suffix(name
->tip_name
, "^0", &len
);
128 if (name
->generation
> 0) {
129 strbuf_grow(&sb
, len
+
130 1 + decimal_width(name
->generation
) +
131 1 + decimal_width(parent_number
));
132 strbuf_addf(&sb
, "%.*s~%d^%d", (int)len
, name
->tip_name
,
133 name
->generation
, parent_number
);
135 strbuf_grow(&sb
, len
+
136 1 + decimal_width(parent_number
));
137 strbuf_addf(&sb
, "%.*s^%d", (int)len
, name
->tip_name
,
140 return strbuf_detach(&sb
, NULL
);
143 static void name_rev(struct commit
*start_commit
,
144 const char *tip_name
, timestamp_t taggerdate
,
145 int from_tag
, int deref
)
147 struct prio_queue queue
;
148 struct commit
*commit
;
149 struct commit
**parents_to_queue
= NULL
;
150 size_t parents_to_queue_nr
, parents_to_queue_alloc
= 0;
151 struct rev_name
*start_name
;
153 parse_commit(start_commit
);
154 if (start_commit
->date
< cutoff
)
157 start_name
= create_or_update_name(start_commit
, taggerdate
, 0, 0,
162 start_name
->tip_name
= xstrfmt("%s^0", tip_name
);
164 start_name
->tip_name
= xstrdup(tip_name
);
166 memset(&queue
, 0, sizeof(queue
)); /* Use the prio_queue as LIFO */
167 prio_queue_put(&queue
, start_commit
);
169 while ((commit
= prio_queue_get(&queue
))) {
170 struct rev_name
*name
= get_commit_rev_name(commit
);
171 struct commit_list
*parents
;
172 int parent_number
= 1;
174 parents_to_queue_nr
= 0;
176 for (parents
= commit
->parents
;
178 parents
= parents
->next
, parent_number
++) {
179 struct commit
*parent
= parents
->item
;
180 struct rev_name
*parent_name
;
181 int generation
, distance
;
183 parse_commit(parent
);
184 if (parent
->date
< cutoff
)
187 if (parent_number
> 1) {
189 distance
= name
->distance
+ MERGE_TRAVERSAL_WEIGHT
;
191 generation
= name
->generation
+ 1;
192 distance
= name
->distance
+ 1;
195 parent_name
= create_or_update_name(parent
, taggerdate
,
199 if (parent_number
> 1)
200 parent_name
->tip_name
=
201 get_parent_name(name
,
204 parent_name
->tip_name
= name
->tip_name
;
205 ALLOC_GROW(parents_to_queue
,
206 parents_to_queue_nr
+ 1,
207 parents_to_queue_alloc
);
208 parents_to_queue
[parents_to_queue_nr
] = parent
;
209 parents_to_queue_nr
++;
213 /* The first parent must come out first from the prio_queue */
214 while (parents_to_queue_nr
)
215 prio_queue_put(&queue
,
216 parents_to_queue
[--parents_to_queue_nr
]);
219 clear_prio_queue(&queue
);
220 free(parents_to_queue
);
223 static int subpath_matches(const char *path
, const char *filter
)
225 const char *subpath
= path
;
228 if (!wildmatch(filter
, subpath
, 0))
229 return subpath
- path
;
230 subpath
= strchr(subpath
, '/');
237 static const char *name_ref_abbrev(const char *refname
, int shorten_unambiguous
)
239 if (shorten_unambiguous
)
240 refname
= shorten_unambiguous_ref(refname
, 0);
241 else if (skip_prefix(refname
, "refs/heads/", &refname
))
242 ; /* refname already advanced */
244 skip_prefix(refname
, "refs/", &refname
);
248 struct name_ref_data
{
251 struct string_list ref_filters
;
252 struct string_list exclude_filters
;
255 static struct tip_table
{
256 struct tip_table_entry
{
257 struct object_id oid
;
259 struct commit
*commit
;
260 timestamp_t taggerdate
;
261 unsigned int from_tag
:1;
262 unsigned int deref
:1;
269 static void add_to_tip_table(const struct object_id
*oid
, const char *refname
,
270 int shorten_unambiguous
, struct commit
*commit
,
271 timestamp_t taggerdate
, int from_tag
, int deref
)
273 refname
= name_ref_abbrev(refname
, shorten_unambiguous
);
275 ALLOC_GROW(tip_table
.table
, tip_table
.nr
+ 1, tip_table
.alloc
);
276 oidcpy(&tip_table
.table
[tip_table
.nr
].oid
, oid
);
277 tip_table
.table
[tip_table
.nr
].refname
= xstrdup(refname
);
278 tip_table
.table
[tip_table
.nr
].commit
= commit
;
279 tip_table
.table
[tip_table
.nr
].taggerdate
= taggerdate
;
280 tip_table
.table
[tip_table
.nr
].from_tag
= from_tag
;
281 tip_table
.table
[tip_table
.nr
].deref
= deref
;
283 tip_table
.sorted
= 0;
286 static int tipcmp(const void *a_
, const void *b_
)
288 const struct tip_table_entry
*a
= a_
, *b
= b_
;
289 return oidcmp(&a
->oid
, &b
->oid
);
292 static int cmp_by_tag_and_age(const void *a_
, const void *b_
)
294 const struct tip_table_entry
*a
= a_
, *b
= b_
;
298 cmp
= b
->from_tag
- a
->from_tag
;
302 /* Older is better. */
303 if (a
->taggerdate
< b
->taggerdate
)
305 return a
->taggerdate
!= b
->taggerdate
;
308 static int name_ref(const char *path
, const struct object_id
*oid
, int flags
, void *cb_data
)
310 struct object
*o
= parse_object(the_repository
, oid
);
311 struct name_ref_data
*data
= cb_data
;
312 int can_abbreviate_output
= data
->tags_only
&& data
->name_only
;
315 struct commit
*commit
= NULL
;
316 timestamp_t taggerdate
= TIME_MAX
;
318 if (data
->tags_only
&& !starts_with(path
, "refs/tags/"))
321 if (data
->exclude_filters
.nr
) {
322 struct string_list_item
*item
;
324 for_each_string_list_item(item
, &data
->exclude_filters
) {
325 if (subpath_matches(path
, item
->string
) >= 0)
330 if (data
->ref_filters
.nr
) {
331 struct string_list_item
*item
;
334 /* See if any of the patterns match. */
335 for_each_string_list_item(item
, &data
->ref_filters
) {
337 * Check all patterns even after finding a match, so
338 * that we can see if a match with a subpath exists.
339 * When a user asked for 'refs/tags/v*' and 'v1.*',
340 * both of which match, the user is showing her
341 * willingness to accept a shortened output by having
342 * the 'v1.*' in the acceptable refnames, so we
343 * shouldn't stop when seeing 'refs/tags/v1.4' matches
344 * 'refs/tags/v*'. We should show it as 'v1.4'.
346 switch (subpath_matches(path
, item
->string
)) {
347 case -1: /* did not match */
349 case 0: /* matched fully */
352 default: /* matched subpath */
354 can_abbreviate_output
= 1;
359 /* If none of the patterns matched, stop now */
364 while (o
&& o
->type
== OBJ_TAG
) {
365 struct tag
*t
= (struct tag
*) o
;
367 break; /* broken repository */
368 o
= parse_object(the_repository
, &t
->tagged
->oid
);
370 taggerdate
= t
->date
;
372 if (o
&& o
->type
== OBJ_COMMIT
) {
373 commit
= (struct commit
*)o
;
374 from_tag
= starts_with(path
, "refs/tags/");
375 if (taggerdate
== TIME_MAX
)
376 taggerdate
= commit
->date
;
379 add_to_tip_table(oid
, path
, can_abbreviate_output
, commit
, taggerdate
,
384 static void name_tips(void)
389 * Try to set better names first, so that worse ones spread
392 QSORT(tip_table
.table
, tip_table
.nr
, cmp_by_tag_and_age
);
393 for (i
= 0; i
< tip_table
.nr
; i
++) {
394 struct tip_table_entry
*e
= &tip_table
.table
[i
];
396 name_rev(e
->commit
, e
->refname
, e
->taggerdate
,
397 e
->from_tag
, e
->deref
);
402 static const struct object_id
*nth_tip_table_ent(size_t ix
, const void *table_
)
404 const struct tip_table_entry
*table
= table_
;
405 return &table
[ix
].oid
;
408 static const char *get_exact_ref_match(const struct object
*o
)
412 if (!tip_table
.table
|| !tip_table
.nr
)
415 if (!tip_table
.sorted
) {
416 QSORT(tip_table
.table
, tip_table
.nr
, tipcmp
);
417 tip_table
.sorted
= 1;
420 found
= oid_pos(&o
->oid
, tip_table
.table
, tip_table
.nr
,
423 return tip_table
.table
[found
].refname
;
427 /* may return a constant string or use "buf" as scratch space */
428 static const char *get_rev_name(const struct object
*o
, struct strbuf
*buf
)
431 const struct commit
*c
;
433 if (o
->type
!= OBJ_COMMIT
)
434 return get_exact_ref_match(o
);
435 c
= (const struct commit
*) o
;
436 n
= get_commit_rev_name(c
);
444 strbuf_addstr(buf
, n
->tip_name
);
445 strbuf_strip_suffix(buf
, "^0");
446 strbuf_addf(buf
, "~%d", n
->generation
);
451 static void show_name(const struct object
*obj
,
452 const char *caller_name
,
453 int always
, int allow_undefined
, int name_only
)
456 const struct object_id
*oid
= &obj
->oid
;
457 struct strbuf buf
= STRBUF_INIT
;
460 printf("%s ", caller_name
? caller_name
: oid_to_hex(oid
));
461 name
= get_rev_name(obj
, &buf
);
463 printf("%s\n", name
);
464 else if (allow_undefined
)
465 printf("undefined\n");
467 printf("%s\n", find_unique_abbrev(oid
, DEFAULT_ABBREV
));
469 die("cannot describe '%s'", oid_to_hex(oid
));
470 strbuf_release(&buf
);
473 static char const * const name_rev_usage
[] = {
474 N_("git name-rev [<options>] <commit>..."),
475 N_("git name-rev [<options>] --all"),
476 N_("git name-rev [<options>] --stdin"),
480 static void name_rev_line(char *p
, struct name_ref_data
*data
)
482 struct strbuf buf
= STRBUF_INIT
;
485 const unsigned hexsz
= the_hash_algo
->hexsz
;
487 for (p_start
= p
; *p
; p
++) {
488 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
491 else if (++counter
== hexsz
&&
493 struct object_id oid
;
494 const char *name
= NULL
;
496 int p_len
= p
- p_start
+ 1;
501 if (!get_oid(p
- (hexsz
- 1), &oid
)) {
503 lookup_object(the_repository
, &oid
);
505 name
= get_rev_name(o
, &buf
);
513 printf("%.*s%s", p_len
- hexsz
, p_start
, name
);
515 printf("%.*s (%s)", p_len
, p_start
, name
);
522 fwrite(p_start
, p
- p_start
, 1, stdout
);
524 strbuf_release(&buf
);
527 int cmd_name_rev(int argc
, const char **argv
, const char *prefix
)
529 struct object_array revs
= OBJECT_ARRAY_INIT
;
530 int all
= 0, annotate_stdin
= 0, transform_stdin
= 0, allow_undefined
= 1, always
= 0, peel_tag
= 0;
531 struct name_ref_data data
= { 0, 0, STRING_LIST_INIT_NODUP
, STRING_LIST_INIT_NODUP
};
532 struct option opts
[] = {
533 OPT_BOOL(0, "name-only", &data
.name_only
, N_("print only ref-based names (no object names)")),
534 OPT_BOOL(0, "tags", &data
.tags_only
, N_("only use tags to name the commits")),
535 OPT_STRING_LIST(0, "refs", &data
.ref_filters
, N_("pattern"),
536 N_("only use refs matching <pattern>")),
537 OPT_STRING_LIST(0, "exclude", &data
.exclude_filters
, N_("pattern"),
538 N_("ignore refs matching <pattern>")),
540 OPT_BOOL(0, "all", &all
, N_("list all commits reachable from all refs")),
541 OPT_BOOL(0, "stdin", &transform_stdin
, N_("deprecated: use annotate-stdin instead")),
542 OPT_BOOL(0, "annotate-stdin", &annotate_stdin
, N_("annotate text from stdin")),
543 OPT_BOOL(0, "undefined", &allow_undefined
, N_("allow to print `undefined` names (default)")),
544 OPT_BOOL(0, "always", &always
,
545 N_("show abbreviated commit object as fallback")),
547 /* A Hidden OPT_BOOL */
548 OPTION_SET_INT
, 0, "peel-tag", &peel_tag
, NULL
,
549 N_("dereference tags in the input (internal use)"),
550 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
, NULL
, 1,
555 init_commit_rev_name(&rev_names
);
556 git_config(git_default_config
, NULL
);
557 argc
= parse_options(argc
, argv
, prefix
, opts
, name_rev_usage
, 0);
559 if (transform_stdin
) {
560 warning("--stdin is deprecated. Please use --annotate-stdin instead, "
561 "which is functionally equivalent.\n"
562 "This option will be removed in a future release.");
566 if (all
+ annotate_stdin
+ !!argc
> 1) {
567 error("Specify either a list, or --all, not both!");
568 usage_with_options(name_rev_usage
, opts
);
570 if (all
|| annotate_stdin
)
573 for (; argc
; argc
--, argv
++) {
574 struct object_id oid
;
575 struct object
*object
;
576 struct commit
*commit
;
578 if (get_oid(*argv
, &oid
)) {
579 fprintf(stderr
, "Could not get sha1 for %s. Skipping.\n",
585 object
= parse_object(the_repository
, &oid
);
587 struct object
*peeled
= deref_tag(the_repository
,
589 if (peeled
&& peeled
->type
== OBJ_COMMIT
)
590 commit
= (struct commit
*)peeled
;
594 fprintf(stderr
, "Could not get object for %s. Skipping.\n",
600 if (cutoff
> commit
->date
)
601 cutoff
= commit
->date
;
606 fprintf(stderr
, "Could not get commit for %s. Skipping.\n",
610 object
= (struct object
*)commit
;
612 add_object_array(object
, *argv
, &revs
);
616 /* check for undeflow */
617 if (cutoff
> TIME_MIN
+ CUTOFF_DATE_SLOP
)
618 cutoff
= cutoff
- CUTOFF_DATE_SLOP
;
622 for_each_ref(name_ref
, &data
);
625 if (annotate_stdin
) {
626 struct strbuf sb
= STRBUF_INIT
;
628 while (strbuf_getline(&sb
, stdin
) != EOF
) {
629 strbuf_addch(&sb
, '\n');
630 name_rev_line(sb
.buf
, &data
);
636 max
= get_max_object_index();
637 for (i
= 0; i
< max
; i
++) {
638 struct object
*obj
= get_indexed_object(i
);
639 if (!obj
|| obj
->type
!= OBJ_COMMIT
)
642 always
, allow_undefined
, data
.name_only
);
646 for (i
= 0; i
< revs
.nr
; i
++)
647 show_name(revs
.objects
[i
].item
, revs
.objects
[i
].name
,
648 always
, allow_undefined
, data
.name_only
);