3 #include "repository.h"
8 #include "parse-options.h"
9 #include "prio-queue.h"
10 #include "sha1-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 is_better_name(struct rev_name
*name
,
48 timestamp_t taggerdate
,
53 * When comparing names based on tags, prefer names
54 * based on the older tag, even if it is farther away.
56 if (from_tag
&& name
->from_tag
)
57 return (name
->taggerdate
> taggerdate
||
58 (name
->taggerdate
== taggerdate
&&
59 name
->distance
> distance
));
62 * We know that at least one of them is a non-tag at this point.
63 * favor a tag over a non-tag.
65 if (name
->from_tag
!= from_tag
)
69 * We are now looking at two non-tags. Tiebreak to favor
72 if (name
->distance
!= distance
)
73 return name
->distance
> distance
;
75 /* ... or tiebreak to favor older date */
76 if (name
->taggerdate
!= taggerdate
)
77 return name
->taggerdate
> taggerdate
;
79 /* keep the current one if we cannot decide */
83 static struct rev_name
*create_or_update_name(struct commit
*commit
,
84 timestamp_t taggerdate
,
85 int generation
, int distance
,
88 struct rev_name
*name
= commit_rev_name_at(&rev_names
, commit
);
90 if (is_valid_rev_name(name
)) {
91 if (!is_better_name(name
, taggerdate
, distance
, from_tag
))
95 * This string might still be shared with ancestors
96 * (generation > 0). We can release it here regardless,
97 * because the new name that has just won will be better
98 * for them as well, so name_rev() will replace these
99 * stale pointers when it processes the parents.
101 if (!name
->generation
)
102 free(name
->tip_name
);
105 name
->taggerdate
= taggerdate
;
106 name
->generation
= generation
;
107 name
->distance
= distance
;
108 name
->from_tag
= from_tag
;
113 static char *get_parent_name(const struct rev_name
*name
, int parent_number
)
115 struct strbuf sb
= STRBUF_INIT
;
118 strip_suffix(name
->tip_name
, "^0", &len
);
119 if (name
->generation
> 0) {
120 strbuf_grow(&sb
, len
+
121 1 + decimal_width(name
->generation
) +
122 1 + decimal_width(parent_number
));
123 strbuf_addf(&sb
, "%.*s~%d^%d", (int)len
, name
->tip_name
,
124 name
->generation
, parent_number
);
126 strbuf_grow(&sb
, len
+
127 1 + decimal_width(parent_number
));
128 strbuf_addf(&sb
, "%.*s^%d", (int)len
, name
->tip_name
,
131 return strbuf_detach(&sb
, NULL
);
134 static void name_rev(struct commit
*start_commit
,
135 const char *tip_name
, timestamp_t taggerdate
,
136 int from_tag
, int deref
)
138 struct prio_queue queue
;
139 struct commit
*commit
;
140 struct commit
**parents_to_queue
= NULL
;
141 size_t parents_to_queue_nr
, parents_to_queue_alloc
= 0;
142 struct rev_name
*start_name
;
144 parse_commit(start_commit
);
145 if (start_commit
->date
< cutoff
)
148 start_name
= create_or_update_name(start_commit
, taggerdate
, 0, 0,
153 start_name
->tip_name
= xstrfmt("%s^0", tip_name
);
155 start_name
->tip_name
= xstrdup(tip_name
);
157 memset(&queue
, 0, sizeof(queue
)); /* Use the prio_queue as LIFO */
158 prio_queue_put(&queue
, start_commit
);
160 while ((commit
= prio_queue_get(&queue
))) {
161 struct rev_name
*name
= get_commit_rev_name(commit
);
162 struct commit_list
*parents
;
163 int parent_number
= 1;
165 parents_to_queue_nr
= 0;
167 for (parents
= commit
->parents
;
169 parents
= parents
->next
, parent_number
++) {
170 struct commit
*parent
= parents
->item
;
171 struct rev_name
*parent_name
;
172 int generation
, distance
;
174 parse_commit(parent
);
175 if (parent
->date
< cutoff
)
178 if (parent_number
> 1) {
180 distance
= name
->distance
+ MERGE_TRAVERSAL_WEIGHT
;
182 generation
= name
->generation
+ 1;
183 distance
= name
->distance
+ 1;
186 parent_name
= create_or_update_name(parent
, taggerdate
,
190 if (parent_number
> 1)
191 parent_name
->tip_name
=
192 get_parent_name(name
,
195 parent_name
->tip_name
= name
->tip_name
;
196 ALLOC_GROW(parents_to_queue
,
197 parents_to_queue_nr
+ 1,
198 parents_to_queue_alloc
);
199 parents_to_queue
[parents_to_queue_nr
] = parent
;
200 parents_to_queue_nr
++;
204 /* The first parent must come out first from the prio_queue */
205 while (parents_to_queue_nr
)
206 prio_queue_put(&queue
,
207 parents_to_queue
[--parents_to_queue_nr
]);
210 clear_prio_queue(&queue
);
211 free(parents_to_queue
);
214 static int subpath_matches(const char *path
, const char *filter
)
216 const char *subpath
= path
;
219 if (!wildmatch(filter
, subpath
, 0))
220 return subpath
- path
;
221 subpath
= strchr(subpath
, '/');
228 static const char *name_ref_abbrev(const char *refname
, int shorten_unambiguous
)
230 if (shorten_unambiguous
)
231 refname
= shorten_unambiguous_ref(refname
, 0);
232 else if (skip_prefix(refname
, "refs/heads/", &refname
))
233 ; /* refname already advanced */
235 skip_prefix(refname
, "refs/", &refname
);
239 struct name_ref_data
{
242 struct string_list ref_filters
;
243 struct string_list exclude_filters
;
246 static struct tip_table
{
247 struct tip_table_entry
{
248 struct object_id oid
;
250 struct commit
*commit
;
251 timestamp_t taggerdate
;
252 unsigned int from_tag
:1;
253 unsigned int deref
:1;
260 static void add_to_tip_table(const struct object_id
*oid
, const char *refname
,
261 int shorten_unambiguous
, struct commit
*commit
,
262 timestamp_t taggerdate
, int from_tag
, int deref
)
264 refname
= name_ref_abbrev(refname
, shorten_unambiguous
);
266 ALLOC_GROW(tip_table
.table
, tip_table
.nr
+ 1, tip_table
.alloc
);
267 oidcpy(&tip_table
.table
[tip_table
.nr
].oid
, oid
);
268 tip_table
.table
[tip_table
.nr
].refname
= xstrdup(refname
);
269 tip_table
.table
[tip_table
.nr
].commit
= commit
;
270 tip_table
.table
[tip_table
.nr
].taggerdate
= taggerdate
;
271 tip_table
.table
[tip_table
.nr
].from_tag
= from_tag
;
272 tip_table
.table
[tip_table
.nr
].deref
= deref
;
274 tip_table
.sorted
= 0;
277 static int tipcmp(const void *a_
, const void *b_
)
279 const struct tip_table_entry
*a
= a_
, *b
= b_
;
280 return oidcmp(&a
->oid
, &b
->oid
);
283 static int cmp_by_tag_and_age(const void *a_
, const void *b_
)
285 const struct tip_table_entry
*a
= a_
, *b
= b_
;
289 cmp
= b
->from_tag
- a
->from_tag
;
293 /* Older is better. */
294 if (a
->taggerdate
< b
->taggerdate
)
296 return a
->taggerdate
!= b
->taggerdate
;
299 static int name_ref(const char *path
, const struct object_id
*oid
, int flags
, void *cb_data
)
301 struct object
*o
= parse_object(the_repository
, oid
);
302 struct name_ref_data
*data
= cb_data
;
303 int can_abbreviate_output
= data
->tags_only
&& data
->name_only
;
306 struct commit
*commit
= NULL
;
307 timestamp_t taggerdate
= TIME_MAX
;
309 if (data
->tags_only
&& !starts_with(path
, "refs/tags/"))
312 if (data
->exclude_filters
.nr
) {
313 struct string_list_item
*item
;
315 for_each_string_list_item(item
, &data
->exclude_filters
) {
316 if (subpath_matches(path
, item
->string
) >= 0)
321 if (data
->ref_filters
.nr
) {
322 struct string_list_item
*item
;
325 /* See if any of the patterns match. */
326 for_each_string_list_item(item
, &data
->ref_filters
) {
328 * Check all patterns even after finding a match, so
329 * that we can see if a match with a subpath exists.
330 * When a user asked for 'refs/tags/v*' and 'v1.*',
331 * both of which match, the user is showing her
332 * willingness to accept a shortened output by having
333 * the 'v1.*' in the acceptable refnames, so we
334 * shouldn't stop when seeing 'refs/tags/v1.4' matches
335 * 'refs/tags/v*'. We should show it as 'v1.4'.
337 switch (subpath_matches(path
, item
->string
)) {
338 case -1: /* did not match */
340 case 0: /* matched fully */
343 default: /* matched subpath */
345 can_abbreviate_output
= 1;
350 /* If none of the patterns matched, stop now */
355 while (o
&& o
->type
== OBJ_TAG
) {
356 struct tag
*t
= (struct tag
*) o
;
358 break; /* broken repository */
359 o
= parse_object(the_repository
, &t
->tagged
->oid
);
361 taggerdate
= t
->date
;
363 if (o
&& o
->type
== OBJ_COMMIT
) {
364 commit
= (struct commit
*)o
;
365 from_tag
= starts_with(path
, "refs/tags/");
366 if (taggerdate
== TIME_MAX
)
367 taggerdate
= commit
->date
;
370 add_to_tip_table(oid
, path
, can_abbreviate_output
, commit
, taggerdate
,
375 static void name_tips(void)
380 * Try to set better names first, so that worse ones spread
383 QSORT(tip_table
.table
, tip_table
.nr
, cmp_by_tag_and_age
);
384 for (i
= 0; i
< tip_table
.nr
; i
++) {
385 struct tip_table_entry
*e
= &tip_table
.table
[i
];
387 name_rev(e
->commit
, e
->refname
, e
->taggerdate
,
388 e
->from_tag
, e
->deref
);
393 static const unsigned char *nth_tip_table_ent(size_t ix
, void *table_
)
395 struct tip_table_entry
*table
= table_
;
396 return table
[ix
].oid
.hash
;
399 static const char *get_exact_ref_match(const struct object
*o
)
403 if (!tip_table
.table
|| !tip_table
.nr
)
406 if (!tip_table
.sorted
) {
407 QSORT(tip_table
.table
, tip_table
.nr
, tipcmp
);
408 tip_table
.sorted
= 1;
411 found
= sha1_pos(o
->oid
.hash
, tip_table
.table
, tip_table
.nr
,
414 return tip_table
.table
[found
].refname
;
418 /* may return a constant string or use "buf" as scratch space */
419 static const char *get_rev_name(const struct object
*o
, struct strbuf
*buf
)
422 const struct commit
*c
;
424 if (o
->type
!= OBJ_COMMIT
)
425 return get_exact_ref_match(o
);
426 c
= (const struct commit
*) o
;
427 n
= get_commit_rev_name(c
);
435 strbuf_addstr(buf
, n
->tip_name
);
436 strbuf_strip_suffix(buf
, "^0");
437 strbuf_addf(buf
, "~%d", n
->generation
);
442 static void show_name(const struct object
*obj
,
443 const char *caller_name
,
444 int always
, int allow_undefined
, int name_only
)
447 const struct object_id
*oid
= &obj
->oid
;
448 struct strbuf buf
= STRBUF_INIT
;
451 printf("%s ", caller_name
? caller_name
: oid_to_hex(oid
));
452 name
= get_rev_name(obj
, &buf
);
454 printf("%s\n", name
);
455 else if (allow_undefined
)
456 printf("undefined\n");
458 printf("%s\n", find_unique_abbrev(oid
, DEFAULT_ABBREV
));
460 die("cannot describe '%s'", oid_to_hex(oid
));
461 strbuf_release(&buf
);
464 static char const * const name_rev_usage
[] = {
465 N_("git name-rev [<options>] <commit>..."),
466 N_("git name-rev [<options>] --all"),
467 N_("git name-rev [<options>] --stdin"),
471 static void name_rev_line(char *p
, struct name_ref_data
*data
)
473 struct strbuf buf
= STRBUF_INIT
;
476 const unsigned hexsz
= the_hash_algo
->hexsz
;
478 for (p_start
= p
; *p
; p
++) {
479 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
482 else if (++counter
== hexsz
&&
484 struct object_id oid
;
485 const char *name
= NULL
;
487 int p_len
= p
- p_start
+ 1;
492 if (!get_oid(p
- (hexsz
- 1), &oid
)) {
494 lookup_object(the_repository
, &oid
);
496 name
= get_rev_name(o
, &buf
);
504 printf("%.*s%s", p_len
- hexsz
, p_start
, name
);
506 printf("%.*s (%s)", p_len
, p_start
, name
);
513 fwrite(p_start
, p
- p_start
, 1, stdout
);
515 strbuf_release(&buf
);
518 int cmd_name_rev(int argc
, const char **argv
, const char *prefix
)
520 struct object_array revs
= OBJECT_ARRAY_INIT
;
521 int all
= 0, transform_stdin
= 0, allow_undefined
= 1, always
= 0, peel_tag
= 0;
522 struct name_ref_data data
= { 0, 0, STRING_LIST_INIT_NODUP
, STRING_LIST_INIT_NODUP
};
523 struct option opts
[] = {
524 OPT_BOOL(0, "name-only", &data
.name_only
, N_("print only ref-based names (no object names)")),
525 OPT_BOOL(0, "tags", &data
.tags_only
, N_("only use tags to name the commits")),
526 OPT_STRING_LIST(0, "refs", &data
.ref_filters
, N_("pattern"),
527 N_("only use refs matching <pattern>")),
528 OPT_STRING_LIST(0, "exclude", &data
.exclude_filters
, N_("pattern"),
529 N_("ignore refs matching <pattern>")),
531 OPT_BOOL(0, "all", &all
, N_("list all commits reachable from all refs")),
532 OPT_BOOL(0, "stdin", &transform_stdin
, N_("read from stdin")),
533 OPT_BOOL(0, "undefined", &allow_undefined
, N_("allow to print `undefined` names (default)")),
534 OPT_BOOL(0, "always", &always
,
535 N_("show abbreviated commit object as fallback")),
537 /* A Hidden OPT_BOOL */
538 OPTION_SET_INT
, 0, "peel-tag", &peel_tag
, NULL
,
539 N_("dereference tags in the input (internal use)"),
540 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
, NULL
, 1,
545 init_commit_rev_name(&rev_names
);
546 git_config(git_default_config
, NULL
);
547 argc
= parse_options(argc
, argv
, prefix
, opts
, name_rev_usage
, 0);
548 if (all
+ transform_stdin
+ !!argc
> 1) {
549 error("Specify either a list, or --all, not both!");
550 usage_with_options(name_rev_usage
, opts
);
552 if (all
|| transform_stdin
)
555 for (; argc
; argc
--, argv
++) {
556 struct object_id oid
;
557 struct object
*object
;
558 struct commit
*commit
;
560 if (get_oid(*argv
, &oid
)) {
561 fprintf(stderr
, "Could not get sha1 for %s. Skipping.\n",
567 object
= parse_object(the_repository
, &oid
);
569 struct object
*peeled
= deref_tag(the_repository
,
571 if (peeled
&& peeled
->type
== OBJ_COMMIT
)
572 commit
= (struct commit
*)peeled
;
576 fprintf(stderr
, "Could not get object for %s. Skipping.\n",
582 if (cutoff
> commit
->date
)
583 cutoff
= commit
->date
;
588 fprintf(stderr
, "Could not get commit for %s. Skipping.\n",
592 object
= (struct object
*)commit
;
594 add_object_array(object
, *argv
, &revs
);
598 /* check for undeflow */
599 if (cutoff
> TIME_MIN
+ CUTOFF_DATE_SLOP
)
600 cutoff
= cutoff
- CUTOFF_DATE_SLOP
;
604 for_each_ref(name_ref
, &data
);
607 if (transform_stdin
) {
610 while (!feof(stdin
)) {
611 char *p
= fgets(buffer
, sizeof(buffer
), stdin
);
614 name_rev_line(p
, &data
);
619 max
= get_max_object_index();
620 for (i
= 0; i
< max
; i
++) {
621 struct object
*obj
= get_indexed_object(i
);
622 if (!obj
|| obj
->type
!= OBJ_COMMIT
)
625 always
, allow_undefined
, data
.name_only
);
629 for (i
= 0; i
< revs
.nr
; i
++)
630 show_name(revs
.objects
[i
].item
, revs
.objects
[i
].name
,
631 always
, allow_undefined
, data
.name_only
);