1 #define USE_THE_INDEX_COMPATIBILITY_MACROS
11 #include "parse-options.h"
16 #include "run-command.h"
17 #include "object-store.h"
18 #include "list-objects.h"
19 #include "commit-slab.h"
21 #define MAX_TAGS (FLAG_BITS - 1)
23 define_commit_slab(commit_names
, struct commit_name
*);
25 static const char * const describe_usage
[] = {
26 N_("git describe [<options>] [<commit-ish>...]"),
27 N_("git describe [<options>] --dirty"),
31 static int debug
; /* Display lots of verbose info */
32 static int all
; /* Any valid ref can be used */
33 static int tags
; /* Allow lightweight tags */
34 static int longformat
;
35 static int first_parent
;
36 static int abbrev
= -1; /* unspecified */
37 static int max_candidates
= 10;
38 static struct hashmap names
;
40 static struct string_list patterns
= STRING_LIST_INIT_NODUP
;
41 static struct string_list exclude_patterns
= STRING_LIST_INIT_NODUP
;
43 static const char *suffix
, *dirty
, *broken
;
44 static struct commit_names commit_names
;
46 /* diff-index command arguments to check if working tree is dirty. */
47 static const char *diff_index_args
[] = {
48 "diff-index", "--quiet", "HEAD", "--", NULL
52 struct hashmap_entry entry
;
53 struct object_id peeled
;
55 unsigned prio
:2; /* annotated tag = 2, tag = 1, head = 0 */
56 unsigned name_checked
:1;
62 static const char *prio_names
[] = {
63 N_("head"), N_("lightweight"), N_("annotated"),
66 static int commit_name_neq(const void *unused_cmp_data
,
67 const struct hashmap_entry
*eptr
,
68 const struct hashmap_entry
*entry_or_key
,
71 const struct commit_name
*cn1
, *cn2
;
73 cn1
= container_of(eptr
, const struct commit_name
, entry
);
74 cn2
= container_of(entry_or_key
, const struct commit_name
, entry
);
76 return !oideq(&cn1
->peeled
, peeled
? peeled
: &cn2
->peeled
);
79 static inline struct commit_name
*find_commit_name(const struct object_id
*peeled
)
81 return hashmap_get_entry_from_hash(&names
, oidhash(peeled
), peeled
,
82 struct commit_name
, entry
);
85 static int replace_name(struct commit_name
*e
,
87 const struct object_id
*oid
,
90 if (!e
|| e
->prio
< prio
)
93 if (e
->prio
== 2 && prio
== 2) {
94 /* Multiple annotated tags point to the same commit.
95 * Select one to keep based upon their tagger date.
100 t
= lookup_tag(the_repository
, &e
->oid
);
101 if (!t
|| parse_tag(t
))
106 t
= lookup_tag(the_repository
, oid
);
107 if (!t
|| parse_tag(t
))
111 if (e
->tag
->date
< t
->date
)
118 static void add_to_known_names(const char *path
,
119 const struct object_id
*peeled
,
121 const struct object_id
*oid
)
123 struct commit_name
*e
= find_commit_name(peeled
);
124 struct tag
*tag
= NULL
;
125 if (replace_name(e
, prio
, oid
, &tag
)) {
127 e
= xmalloc(sizeof(struct commit_name
));
128 oidcpy(&e
->peeled
, peeled
);
129 hashmap_entry_init(&e
->entry
, oidhash(peeled
));
130 hashmap_add(&names
, &e
->entry
);
137 oidcpy(&e
->oid
, oid
);
139 e
->path
= xstrdup(path
);
143 static int get_name(const char *path
, const struct object_id
*oid
, int flag
, void *cb_data
)
146 struct object_id peeled
;
147 int is_annotated
, prio
;
148 const char *path_to_match
= NULL
;
150 if (skip_prefix(path
, "refs/tags/", &path_to_match
)) {
153 if ((exclude_patterns
.nr
|| patterns
.nr
) &&
154 !skip_prefix(path
, "refs/heads/", &path_to_match
) &&
155 !skip_prefix(path
, "refs/remotes/", &path_to_match
)) {
156 /* Only accept reference of known type if there are match/exclude patterns */
160 /* Reject anything outside refs/tags/ unless --all */
165 * If we're given exclude patterns, first exclude any tag which match
166 * any of the exclude pattern.
168 if (exclude_patterns
.nr
) {
169 struct string_list_item
*item
;
171 for_each_string_list_item(item
, &exclude_patterns
) {
172 if (!wildmatch(item
->string
, path_to_match
, 0))
178 * If we're given patterns, accept only tags which match at least one
183 struct string_list_item
*item
;
185 for_each_string_list_item(item
, &patterns
) {
186 if (!wildmatch(item
->string
, path_to_match
, 0)) {
196 /* Is it annotated? */
197 if (!peel_iterated_oid(oid
, &peeled
)) {
198 is_annotated
= !oideq(oid
, &peeled
);
200 oidcpy(&peeled
, oid
);
205 * By default, we only use annotated tags, but with --tags
206 * we fall back to lightweight ones (even without --tags,
207 * we still remember lightweight ones, only to give hints
208 * in an error message). --all allows any refs to be used.
217 add_to_known_names(all
? path
+ 5 : path
+ 10, &peeled
, prio
, oid
);
221 struct possible_tag
{
222 struct commit_name
*name
;
225 unsigned flag_within
;
228 static int compare_pt(const void *a_
, const void *b_
)
230 struct possible_tag
*a
= (struct possible_tag
*)a_
;
231 struct possible_tag
*b
= (struct possible_tag
*)b_
;
232 if (a
->depth
!= b
->depth
)
233 return a
->depth
- b
->depth
;
234 if (a
->found_order
!= b
->found_order
)
235 return a
->found_order
- b
->found_order
;
239 static unsigned long finish_depth_computation(
240 struct commit_list
**list
,
241 struct possible_tag
*best
)
243 unsigned long seen_commits
= 0;
245 struct commit
*c
= pop_commit(list
);
246 struct commit_list
*parents
= c
->parents
;
248 if (c
->object
.flags
& best
->flag_within
) {
249 struct commit_list
*a
= *list
;
251 struct commit
*i
= a
->item
;
252 if (!(i
->object
.flags
& best
->flag_within
))
261 struct commit
*p
= parents
->item
;
263 if (!(p
->object
.flags
& SEEN
))
264 commit_list_insert_by_date(p
, list
);
265 p
->object
.flags
|= c
->object
.flags
;
266 parents
= parents
->next
;
272 static void append_name(struct commit_name
*n
, struct strbuf
*dst
)
274 if (n
->prio
== 2 && !n
->tag
) {
275 n
->tag
= lookup_tag(the_repository
, &n
->oid
);
276 if (!n
->tag
|| parse_tag(n
->tag
))
277 die(_("annotated tag %s not available"), n
->path
);
279 if (n
->tag
&& !n
->name_checked
) {
280 if (strcmp(n
->tag
->tag
, all
? n
->path
+ 5 : n
->path
)) {
281 warning(_("tag '%s' is externally known as '%s'"),
282 n
->path
, n
->tag
->tag
);
290 strbuf_addstr(dst
, "tags/");
291 strbuf_addstr(dst
, n
->tag
->tag
);
293 strbuf_addstr(dst
, n
->path
);
297 static void append_suffix(int depth
, const struct object_id
*oid
, struct strbuf
*dst
)
299 strbuf_addf(dst
, "-%d-g%s", depth
, find_unique_abbrev(oid
, abbrev
));
302 static void describe_commit(struct object_id
*oid
, struct strbuf
*dst
)
304 struct commit
*cmit
, *gave_up_on
= NULL
;
305 struct commit_list
*list
;
306 struct commit_name
*n
;
307 struct possible_tag all_matches
[MAX_TAGS
];
308 unsigned int match_cnt
= 0, annotated_cnt
= 0, cur_match
;
309 unsigned long seen_commits
= 0;
310 unsigned int unannotated_cnt
= 0;
312 cmit
= lookup_commit_reference(the_repository
, oid
);
314 n
= find_commit_name(&cmit
->object
.oid
);
315 if (n
&& (tags
|| all
|| n
->prio
== 2)) {
317 * Exact match to an existing ref.
320 if (n
->misnamed
|| longformat
)
321 append_suffix(0, n
->tag
? get_tagged_oid(n
->tag
) : oid
, dst
);
323 strbuf_addstr(dst
, suffix
);
328 die(_("no tag exactly matches '%s'"), oid_to_hex(&cmit
->object
.oid
));
330 fprintf(stderr
, _("No exact match on refs or tags, searching to describe\n"));
333 struct hashmap_iter iter
;
335 struct commit_name
*n
;
337 init_commit_names(&commit_names
);
338 hashmap_for_each_entry(&names
, &iter
, n
,
339 entry
/* member name */) {
340 c
= lookup_commit_reference_gently(the_repository
,
343 *commit_names_at(&commit_names
, c
) = n
;
349 cmit
->object
.flags
= SEEN
;
350 commit_list_insert(cmit
, &list
);
352 struct commit
*c
= pop_commit(&list
);
353 struct commit_list
*parents
= c
->parents
;
354 struct commit_name
**slot
;
357 slot
= commit_names_peek(&commit_names
, c
);
358 n
= slot
? *slot
: NULL
;
360 if (!tags
&& !all
&& n
->prio
< 2) {
362 } else if (match_cnt
< max_candidates
) {
363 struct possible_tag
*t
= &all_matches
[match_cnt
++];
365 t
->depth
= seen_commits
- 1;
366 t
->flag_within
= 1u << match_cnt
;
367 t
->found_order
= match_cnt
;
368 c
->object
.flags
|= t
->flag_within
;
377 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
378 struct possible_tag
*t
= &all_matches
[cur_match
];
379 if (!(c
->object
.flags
& t
->flag_within
))
382 /* Stop if last remaining path already covered by best candidate(s) */
383 if (annotated_cnt
&& !list
) {
384 int best_depth
= INT_MAX
;
385 unsigned best_within
= 0;
386 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
387 struct possible_tag
*t
= &all_matches
[cur_match
];
388 if (t
->depth
< best_depth
) {
389 best_depth
= t
->depth
;
390 best_within
= t
->flag_within
;
391 } else if (t
->depth
== best_depth
) {
392 best_within
|= t
->flag_within
;
395 if ((c
->object
.flags
& best_within
) == best_within
) {
397 fprintf(stderr
, _("finished search at %s\n"),
398 oid_to_hex(&c
->object
.oid
));
403 struct commit
*p
= parents
->item
;
405 if (!(p
->object
.flags
& SEEN
))
406 commit_list_insert_by_date(p
, &list
);
407 p
->object
.flags
|= c
->object
.flags
;
408 parents
= parents
->next
;
416 struct object_id
*cmit_oid
= &cmit
->object
.oid
;
418 strbuf_add_unique_abbrev(dst
, cmit_oid
, abbrev
);
420 strbuf_addstr(dst
, suffix
);
424 die(_("No annotated tags can describe '%s'.\n"
425 "However, there were unannotated tags: try --tags."),
426 oid_to_hex(cmit_oid
));
428 die(_("No tags can describe '%s'.\n"
429 "Try --always, or create some tags."),
430 oid_to_hex(cmit_oid
));
433 QSORT(all_matches
, match_cnt
, compare_pt
);
436 commit_list_insert_by_date(gave_up_on
, &list
);
439 seen_commits
+= finish_depth_computation(&list
, &all_matches
[0]);
440 free_commit_list(list
);
443 static int label_width
= -1;
444 if (label_width
< 0) {
446 for (i
= 0; i
< ARRAY_SIZE(prio_names
); i
++) {
447 w
= strlen(_(prio_names
[i
]));
452 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
453 struct possible_tag
*t
= &all_matches
[cur_match
];
454 fprintf(stderr
, " %-*s %8d %s\n",
455 label_width
, _(prio_names
[t
->name
->prio
]),
456 t
->depth
, t
->name
->path
);
458 fprintf(stderr
, _("traversed %lu commits\n"), seen_commits
);
461 _("more than %i tags found; listed %i most recent\n"
462 "gave up search at %s\n"),
463 max_candidates
, max_candidates
,
464 oid_to_hex(&gave_up_on
->object
.oid
));
468 append_name(all_matches
[0].name
, dst
);
469 if (all_matches
[0].name
->misnamed
|| abbrev
)
470 append_suffix(all_matches
[0].depth
, &cmit
->object
.oid
, dst
);
472 strbuf_addstr(dst
, suffix
);
475 struct process_commit_data
{
476 struct object_id current_commit
;
477 struct object_id looking_for
;
479 struct rev_info
*revs
;
482 static void process_commit(struct commit
*commit
, void *data
)
484 struct process_commit_data
*pcd
= data
;
485 pcd
->current_commit
= commit
->object
.oid
;
488 static void process_object(struct object
*obj
, const char *path
, void *data
)
490 struct process_commit_data
*pcd
= data
;
492 if (oideq(&pcd
->looking_for
, &obj
->oid
) && !pcd
->dst
->len
) {
493 reset_revision_walk();
494 describe_commit(&pcd
->current_commit
, pcd
->dst
);
495 strbuf_addf(pcd
->dst
, ":%s", path
);
496 free_commit_list(pcd
->revs
->commits
);
497 pcd
->revs
->commits
= NULL
;
501 static void describe_blob(struct object_id oid
, struct strbuf
*dst
)
503 struct rev_info revs
;
504 struct strvec args
= STRVEC_INIT
;
505 struct process_commit_data pcd
= { *null_oid(), oid
, dst
, &revs
};
507 strvec_pushl(&args
, "internal: The first arg is not parsed",
508 "--objects", "--in-commit-order", "--reverse", "HEAD",
511 repo_init_revisions(the_repository
, &revs
, NULL
);
512 if (setup_revisions(args
.nr
, args
.v
, &revs
, NULL
) > 1)
513 BUG("setup_revisions could not handle all args?");
515 if (prepare_revision_walk(&revs
))
516 die("revision walk setup failed");
518 traverse_commit_list(&revs
, process_commit
, process_object
, &pcd
);
519 reset_revision_walk();
522 static void describe(const char *arg
, int last_one
)
524 struct object_id oid
;
526 struct strbuf sb
= STRBUF_INIT
;
529 fprintf(stderr
, _("describe %s\n"), arg
);
531 if (get_oid(arg
, &oid
))
532 die(_("Not a valid object name %s"), arg
);
533 cmit
= lookup_commit_reference_gently(the_repository
, &oid
, 1);
536 describe_commit(&oid
, &sb
);
537 else if (oid_object_info(the_repository
, &oid
, NULL
) == OBJ_BLOB
)
538 describe_blob(oid
, &sb
);
540 die(_("%s is neither a commit nor blob"), arg
);
545 clear_commit_marks(cmit
, -1);
550 int cmd_describe(int argc
, const char **argv
, const char *prefix
)
553 struct option options
[] = {
554 OPT_BOOL(0, "contains", &contains
, N_("find the tag that comes after the commit")),
555 OPT_BOOL(0, "debug", &debug
, N_("debug search strategy on stderr")),
556 OPT_BOOL(0, "all", &all
, N_("use any ref")),
557 OPT_BOOL(0, "tags", &tags
, N_("use any tag, even unannotated")),
558 OPT_BOOL(0, "long", &longformat
, N_("always use long format")),
559 OPT_BOOL(0, "first-parent", &first_parent
, N_("only follow first parent")),
560 OPT__ABBREV(&abbrev
),
561 OPT_SET_INT(0, "exact-match", &max_candidates
,
562 N_("only output exact matches"), 0),
563 OPT_INTEGER(0, "candidates", &max_candidates
,
564 N_("consider <n> most recent tags (default: 10)")),
565 OPT_STRING_LIST(0, "match", &patterns
, N_("pattern"),
566 N_("only consider tags matching <pattern>")),
567 OPT_STRING_LIST(0, "exclude", &exclude_patterns
, N_("pattern"),
568 N_("do not consider tags matching <pattern>")),
569 OPT_BOOL(0, "always", &always
,
570 N_("show abbreviated commit object as fallback")),
571 {OPTION_STRING
, 0, "dirty", &dirty
, N_("mark"),
572 N_("append <mark> on dirty working tree (default: \"-dirty\")"),
573 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "-dirty"},
574 {OPTION_STRING
, 0, "broken", &broken
, N_("mark"),
575 N_("append <mark> on broken working tree (default: \"-broken\")"),
576 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "-broken"},
580 git_config(git_default_config
, NULL
);
581 argc
= parse_options(argc
, argv
, prefix
, options
, describe_usage
, 0);
583 abbrev
= DEFAULT_ABBREV
;
585 if (max_candidates
< 0)
587 else if (max_candidates
> MAX_TAGS
)
588 max_candidates
= MAX_TAGS
;
590 save_commit_buffer
= 0;
592 if (longformat
&& abbrev
== 0)
593 die(_("--long is incompatible with --abbrev=0"));
596 struct string_list_item
*item
;
600 strvec_pushl(&args
, "name-rev",
601 "--peel-tag", "--name-only", "--no-undefined",
604 strvec_push(&args
, "--always");
606 strvec_push(&args
, "--tags");
607 for_each_string_list_item(item
, &patterns
)
608 strvec_pushf(&args
, "--refs=refs/tags/%s", item
->string
);
609 for_each_string_list_item(item
, &exclude_patterns
)
610 strvec_pushf(&args
, "--exclude=refs/tags/%s", item
->string
);
613 strvec_pushv(&args
, argv
);
615 strvec_push(&args
, "HEAD");
616 return cmd_name_rev(args
.nr
, args
.v
, prefix
);
619 hashmap_init(&names
, commit_name_neq
, NULL
, 0);
620 for_each_rawref(get_name
, NULL
);
621 if (!hashmap_get_size(&names
) && !always
)
622 die(_("No names found, cannot describe anything."));
626 struct child_process cp
= CHILD_PROCESS_INIT
;
627 strvec_pushv(&cp
.args
, diff_index_args
);
635 switch (run_command(&cp
)) {
643 /* diff-index aborted abnormally */
647 struct lock_file index_lock
= LOCK_INIT
;
648 struct rev_info revs
;
649 struct strvec args
= STRVEC_INIT
;
654 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
,
656 fd
= hold_locked_index(&index_lock
, 0);
658 repo_update_index_if_able(the_repository
, &index_lock
);
660 repo_init_revisions(the_repository
, &revs
, prefix
);
661 strvec_pushv(&args
, diff_index_args
);
662 if (setup_revisions(args
.nr
, args
.v
, &revs
, NULL
) != 1)
663 BUG("malformed internal diff-index command line");
664 result
= run_diff_index(&revs
, 0);
666 if (!diff_result_code(&revs
.diffopt
, result
))
673 die(_("--dirty is incompatible with commit-ishes"));
675 die(_("--broken is incompatible with commit-ishes"));
678 describe(*argv
++, argc
== 0);