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
19 typedef struct rev_name
{
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 struct rev_name
*get_commit_rev_name(struct commit
*commit
)
37 struct rev_name
**slot
= commit_rev_name_peek(&rev_names
, commit
);
39 return slot
? *slot
: NULL
;
42 static void set_commit_rev_name(struct commit
*commit
, struct rev_name
*name
)
44 *commit_rev_name_at(&rev_names
, commit
) = name
;
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
,
85 timestamp_t taggerdate
,
86 int generation
, int distance
,
89 struct rev_name
*name
= get_commit_rev_name(commit
);
92 name
= xmalloc(sizeof(*name
));
93 set_commit_rev_name(commit
, name
);
95 } else if (is_better_name(name
, taggerdate
, distance
, from_tag
)) {
97 name
->tip_name
= tip_name
;
98 name
->taggerdate
= taggerdate
;
99 name
->generation
= generation
;
100 name
->distance
= distance
;
101 name
->from_tag
= from_tag
;
108 static void name_rev(struct commit
*start_commit
,
109 const char *tip_name
, timestamp_t taggerdate
,
110 int from_tag
, int deref
)
112 struct prio_queue queue
;
113 struct commit
*commit
;
114 struct commit
**parents_to_queue
= NULL
;
115 size_t parents_to_queue_nr
, parents_to_queue_alloc
= 0;
116 char *to_free
= NULL
;
118 parse_commit(start_commit
);
119 if (start_commit
->date
< cutoff
)
123 tip_name
= to_free
= xstrfmt("%s^0", tip_name
);
125 if (!create_or_update_name(start_commit
, tip_name
, taggerdate
, 0, 0,
131 memset(&queue
, 0, sizeof(queue
)); /* Use the prio_queue as LIFO */
132 prio_queue_put(&queue
, start_commit
);
134 while ((commit
= prio_queue_get(&queue
))) {
135 struct rev_name
*name
= get_commit_rev_name(commit
);
136 struct commit_list
*parents
;
137 int parent_number
= 1;
139 parents_to_queue_nr
= 0;
141 for (parents
= commit
->parents
;
143 parents
= parents
->next
, parent_number
++) {
144 struct commit
*parent
= parents
->item
;
145 const char *new_name
;
146 int generation
, distance
;
148 parse_commit(parent
);
149 if (parent
->date
< cutoff
)
152 if (parent_number
> 1) {
155 strip_suffix(name
->tip_name
, "^0", &len
);
156 if (name
->generation
> 0)
157 new_name
= xstrfmt("%.*s~%d^%d",
163 new_name
= xstrfmt("%.*s^%d", (int)len
,
167 distance
= name
->distance
+ MERGE_TRAVERSAL_WEIGHT
;
169 new_name
= name
->tip_name
;
170 generation
= name
->generation
+ 1;
171 distance
= name
->distance
+ 1;
174 if (create_or_update_name(parent
, new_name
, taggerdate
,
175 generation
, distance
,
177 ALLOC_GROW(parents_to_queue
,
178 parents_to_queue_nr
+ 1,
179 parents_to_queue_alloc
);
180 parents_to_queue
[parents_to_queue_nr
] = parent
;
181 parents_to_queue_nr
++;
185 /* The first parent must come out first from the prio_queue */
186 while (parents_to_queue_nr
)
187 prio_queue_put(&queue
,
188 parents_to_queue
[--parents_to_queue_nr
]);
191 clear_prio_queue(&queue
);
192 free(parents_to_queue
);
195 static int subpath_matches(const char *path
, const char *filter
)
197 const char *subpath
= path
;
200 if (!wildmatch(filter
, subpath
, 0))
201 return subpath
- path
;
202 subpath
= strchr(subpath
, '/');
209 static const char *name_ref_abbrev(const char *refname
, int shorten_unambiguous
)
211 if (shorten_unambiguous
)
212 refname
= shorten_unambiguous_ref(refname
, 0);
213 else if (skip_prefix(refname
, "refs/heads/", &refname
))
214 ; /* refname already advanced */
216 skip_prefix(refname
, "refs/", &refname
);
220 struct name_ref_data
{
223 struct string_list ref_filters
;
224 struct string_list exclude_filters
;
227 static struct tip_table
{
228 struct tip_table_entry
{
229 struct object_id oid
;
237 static void add_to_tip_table(const struct object_id
*oid
, const char *refname
,
238 int shorten_unambiguous
)
240 refname
= name_ref_abbrev(refname
, shorten_unambiguous
);
242 ALLOC_GROW(tip_table
.table
, tip_table
.nr
+ 1, tip_table
.alloc
);
243 oidcpy(&tip_table
.table
[tip_table
.nr
].oid
, oid
);
244 tip_table
.table
[tip_table
.nr
].refname
= xstrdup(refname
);
246 tip_table
.sorted
= 0;
249 static int tipcmp(const void *a_
, const void *b_
)
251 const struct tip_table_entry
*a
= a_
, *b
= b_
;
252 return oidcmp(&a
->oid
, &b
->oid
);
255 static int name_ref(const char *path
, const struct object_id
*oid
, int flags
, void *cb_data
)
257 struct object
*o
= parse_object(the_repository
, oid
);
258 struct name_ref_data
*data
= cb_data
;
259 int can_abbreviate_output
= data
->tags_only
&& data
->name_only
;
261 timestamp_t taggerdate
= TIME_MAX
;
263 if (data
->tags_only
&& !starts_with(path
, "refs/tags/"))
266 if (data
->exclude_filters
.nr
) {
267 struct string_list_item
*item
;
269 for_each_string_list_item(item
, &data
->exclude_filters
) {
270 if (subpath_matches(path
, item
->string
) >= 0)
275 if (data
->ref_filters
.nr
) {
276 struct string_list_item
*item
;
279 /* See if any of the patterns match. */
280 for_each_string_list_item(item
, &data
->ref_filters
) {
282 * Check all patterns even after finding a match, so
283 * that we can see if a match with a subpath exists.
284 * When a user asked for 'refs/tags/v*' and 'v1.*',
285 * both of which match, the user is showing her
286 * willingness to accept a shortened output by having
287 * the 'v1.*' in the acceptable refnames, so we
288 * shouldn't stop when seeing 'refs/tags/v1.4' matches
289 * 'refs/tags/v*'. We should show it as 'v1.4'.
291 switch (subpath_matches(path
, item
->string
)) {
292 case -1: /* did not match */
294 case 0: /* matched fully */
297 default: /* matched subpath */
299 can_abbreviate_output
= 1;
304 /* If none of the patterns matched, stop now */
309 add_to_tip_table(oid
, path
, can_abbreviate_output
);
311 while (o
&& o
->type
== OBJ_TAG
) {
312 struct tag
*t
= (struct tag
*) o
;
314 break; /* broken repository */
315 o
= parse_object(the_repository
, &t
->tagged
->oid
);
317 taggerdate
= t
->date
;
319 if (o
&& o
->type
== OBJ_COMMIT
) {
320 struct commit
*commit
= (struct commit
*)o
;
321 int from_tag
= starts_with(path
, "refs/tags/");
323 if (taggerdate
== TIME_MAX
)
324 taggerdate
= commit
->date
;
325 path
= name_ref_abbrev(path
, can_abbreviate_output
);
326 name_rev(commit
, xstrdup(path
), taggerdate
, from_tag
, deref
);
331 static const unsigned char *nth_tip_table_ent(size_t ix
, void *table_
)
333 struct tip_table_entry
*table
= table_
;
334 return table
[ix
].oid
.hash
;
337 static const char *get_exact_ref_match(const struct object
*o
)
341 if (!tip_table
.table
|| !tip_table
.nr
)
344 if (!tip_table
.sorted
) {
345 QSORT(tip_table
.table
, tip_table
.nr
, tipcmp
);
346 tip_table
.sorted
= 1;
349 found
= sha1_pos(o
->oid
.hash
, tip_table
.table
, tip_table
.nr
,
352 return tip_table
.table
[found
].refname
;
356 /* may return a constant string or use "buf" as scratch space */
357 static const char *get_rev_name(const struct object
*o
, struct strbuf
*buf
)
362 if (o
->type
!= OBJ_COMMIT
)
363 return get_exact_ref_match(o
);
364 c
= (struct commit
*) o
;
365 n
= get_commit_rev_name(c
);
373 strbuf_addstr(buf
, n
->tip_name
);
374 strbuf_strip_suffix(buf
, "^0");
375 strbuf_addf(buf
, "~%d", n
->generation
);
380 static void show_name(const struct object
*obj
,
381 const char *caller_name
,
382 int always
, int allow_undefined
, int name_only
)
385 const struct object_id
*oid
= &obj
->oid
;
386 struct strbuf buf
= STRBUF_INIT
;
389 printf("%s ", caller_name
? caller_name
: oid_to_hex(oid
));
390 name
= get_rev_name(obj
, &buf
);
392 printf("%s\n", name
);
393 else if (allow_undefined
)
394 printf("undefined\n");
396 printf("%s\n", find_unique_abbrev(oid
, DEFAULT_ABBREV
));
398 die("cannot describe '%s'", oid_to_hex(oid
));
399 strbuf_release(&buf
);
402 static char const * const name_rev_usage
[] = {
403 N_("git name-rev [<options>] <commit>..."),
404 N_("git name-rev [<options>] --all"),
405 N_("git name-rev [<options>] --stdin"),
409 static void name_rev_line(char *p
, struct name_ref_data
*data
)
411 struct strbuf buf
= STRBUF_INIT
;
414 const unsigned hexsz
= the_hash_algo
->hexsz
;
416 for (p_start
= p
; *p
; p
++) {
417 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
420 else if (++counter
== hexsz
&&
422 struct object_id oid
;
423 const char *name
= NULL
;
425 int p_len
= p
- p_start
+ 1;
430 if (!get_oid(p
- (hexsz
- 1), &oid
)) {
432 lookup_object(the_repository
, &oid
);
434 name
= get_rev_name(o
, &buf
);
442 printf("%.*s%s", p_len
- hexsz
, p_start
, name
);
444 printf("%.*s (%s)", p_len
, p_start
, name
);
451 fwrite(p_start
, p
- p_start
, 1, stdout
);
453 strbuf_release(&buf
);
456 int cmd_name_rev(int argc
, const char **argv
, const char *prefix
)
458 struct object_array revs
= OBJECT_ARRAY_INIT
;
459 int all
= 0, transform_stdin
= 0, allow_undefined
= 1, always
= 0, peel_tag
= 0;
460 struct name_ref_data data
= { 0, 0, STRING_LIST_INIT_NODUP
, STRING_LIST_INIT_NODUP
};
461 struct option opts
[] = {
462 OPT_BOOL(0, "name-only", &data
.name_only
, N_("print only names (no SHA-1)")),
463 OPT_BOOL(0, "tags", &data
.tags_only
, N_("only use tags to name the commits")),
464 OPT_STRING_LIST(0, "refs", &data
.ref_filters
, N_("pattern"),
465 N_("only use refs matching <pattern>")),
466 OPT_STRING_LIST(0, "exclude", &data
.exclude_filters
, N_("pattern"),
467 N_("ignore refs matching <pattern>")),
469 OPT_BOOL(0, "all", &all
, N_("list all commits reachable from all refs")),
470 OPT_BOOL(0, "stdin", &transform_stdin
, N_("read from stdin")),
471 OPT_BOOL(0, "undefined", &allow_undefined
, N_("allow to print `undefined` names (default)")),
472 OPT_BOOL(0, "always", &always
,
473 N_("show abbreviated commit object as fallback")),
475 /* A Hidden OPT_BOOL */
476 OPTION_SET_INT
, 0, "peel-tag", &peel_tag
, NULL
,
477 N_("dereference tags in the input (internal use)"),
478 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
, NULL
, 1,
483 init_commit_rev_name(&rev_names
);
484 git_config(git_default_config
, NULL
);
485 argc
= parse_options(argc
, argv
, prefix
, opts
, name_rev_usage
, 0);
486 if (all
+ transform_stdin
+ !!argc
> 1) {
487 error("Specify either a list, or --all, not both!");
488 usage_with_options(name_rev_usage
, opts
);
490 if (all
|| transform_stdin
)
493 for (; argc
; argc
--, argv
++) {
494 struct object_id oid
;
495 struct object
*object
;
496 struct commit
*commit
;
498 if (get_oid(*argv
, &oid
)) {
499 fprintf(stderr
, "Could not get sha1 for %s. Skipping.\n",
505 object
= parse_object(the_repository
, &oid
);
507 struct object
*peeled
= deref_tag(the_repository
,
509 if (peeled
&& peeled
->type
== OBJ_COMMIT
)
510 commit
= (struct commit
*)peeled
;
514 fprintf(stderr
, "Could not get object for %s. Skipping.\n",
520 if (cutoff
> commit
->date
)
521 cutoff
= commit
->date
;
526 fprintf(stderr
, "Could not get commit for %s. Skipping.\n",
530 object
= (struct object
*)commit
;
532 add_object_array(object
, *argv
, &revs
);
536 /* check for undeflow */
537 if (cutoff
> TIME_MIN
+ CUTOFF_DATE_SLOP
)
538 cutoff
= cutoff
- CUTOFF_DATE_SLOP
;
542 for_each_ref(name_ref
, &data
);
544 if (transform_stdin
) {
547 while (!feof(stdin
)) {
548 char *p
= fgets(buffer
, sizeof(buffer
), stdin
);
551 name_rev_line(p
, &data
);
556 max
= get_max_object_index();
557 for (i
= 0; i
< max
; i
++) {
558 struct object
*obj
= get_indexed_object(i
);
559 if (!obj
|| obj
->type
!= OBJ_COMMIT
)
562 always
, allow_undefined
, data
.name_only
);
566 for (i
= 0; i
< revs
.nr
; i
++)
567 show_name(revs
.objects
[i
].item
, revs
.objects
[i
].name
,
568 always
, allow_undefined
, data
.name_only
);