1 #define USE_THE_INDEX_VARIABLE
4 #include "environment.h"
14 #include "object-name.h"
15 #include "parse-options.h"
21 #include "run-command.h"
22 #include "object-store.h"
23 #include "list-objects.h"
24 #include "commit-slab.h"
26 #define MAX_TAGS (FLAG_BITS - 1)
28 define_commit_slab(commit_names
, struct commit_name
*);
30 static const char * const describe_usage
[] = {
31 N_("git describe [--all] [--tags] [--contains] [--abbrev=<n>] [<commit-ish>...]"),
32 N_("git describe [--all] [--tags] [--contains] [--abbrev=<n>] --dirty[=<mark>]"),
33 N_("git describe <blob>"),
37 static int debug
; /* Display lots of verbose info */
38 static int all
; /* Any valid ref can be used */
39 static int tags
; /* Allow lightweight tags */
40 static int longformat
;
41 static int first_parent
;
42 static int abbrev
= -1; /* unspecified */
43 static int max_candidates
= 10;
44 static struct hashmap names
;
46 static struct string_list patterns
= STRING_LIST_INIT_NODUP
;
47 static struct string_list exclude_patterns
= STRING_LIST_INIT_NODUP
;
49 static const char *suffix
, *dirty
, *broken
;
50 static struct commit_names commit_names
;
52 /* diff-index command arguments to check if working tree is dirty. */
53 static const char *diff_index_args
[] = {
54 "diff-index", "--quiet", "HEAD", "--", NULL
58 struct hashmap_entry entry
;
59 struct object_id peeled
;
61 unsigned prio
:2; /* annotated tag = 2, tag = 1, head = 0 */
62 unsigned name_checked
:1;
68 static const char *prio_names
[] = {
69 N_("head"), N_("lightweight"), N_("annotated"),
72 static int commit_name_neq(const void *cmp_data UNUSED
,
73 const struct hashmap_entry
*eptr
,
74 const struct hashmap_entry
*entry_or_key
,
77 const struct commit_name
*cn1
, *cn2
;
79 cn1
= container_of(eptr
, const struct commit_name
, entry
);
80 cn2
= container_of(entry_or_key
, const struct commit_name
, entry
);
82 return !oideq(&cn1
->peeled
, peeled
? peeled
: &cn2
->peeled
);
85 static inline struct commit_name
*find_commit_name(const struct object_id
*peeled
)
87 return hashmap_get_entry_from_hash(&names
, oidhash(peeled
), peeled
,
88 struct commit_name
, entry
);
91 static int replace_name(struct commit_name
*e
,
93 const struct object_id
*oid
,
96 if (!e
|| e
->prio
< prio
)
99 if (e
->prio
== 2 && prio
== 2) {
100 /* Multiple annotated tags point to the same commit.
101 * Select one to keep based upon their tagger date.
106 t
= lookup_tag(the_repository
, &e
->oid
);
107 if (!t
|| parse_tag(t
))
112 t
= lookup_tag(the_repository
, oid
);
113 if (!t
|| parse_tag(t
))
117 if (e
->tag
->date
< t
->date
)
124 static void add_to_known_names(const char *path
,
125 const struct object_id
*peeled
,
127 const struct object_id
*oid
)
129 struct commit_name
*e
= find_commit_name(peeled
);
130 struct tag
*tag
= NULL
;
131 if (replace_name(e
, prio
, oid
, &tag
)) {
133 e
= xmalloc(sizeof(struct commit_name
));
134 oidcpy(&e
->peeled
, peeled
);
135 hashmap_entry_init(&e
->entry
, oidhash(peeled
));
136 hashmap_add(&names
, &e
->entry
);
143 oidcpy(&e
->oid
, oid
);
145 e
->path
= xstrdup(path
);
149 static int get_name(const char *path
, const struct object_id
*oid
,
150 int flag UNUSED
, void *cb_data UNUSED
)
153 struct object_id peeled
;
154 int is_annotated
, prio
;
155 const char *path_to_match
= NULL
;
157 if (skip_prefix(path
, "refs/tags/", &path_to_match
)) {
160 if ((exclude_patterns
.nr
|| patterns
.nr
) &&
161 !skip_prefix(path
, "refs/heads/", &path_to_match
) &&
162 !skip_prefix(path
, "refs/remotes/", &path_to_match
)) {
163 /* Only accept reference of known type if there are match/exclude patterns */
167 /* Reject anything outside refs/tags/ unless --all */
172 * If we're given exclude patterns, first exclude any tag which match
173 * any of the exclude pattern.
175 if (exclude_patterns
.nr
) {
176 struct string_list_item
*item
;
178 for_each_string_list_item(item
, &exclude_patterns
) {
179 if (!wildmatch(item
->string
, path_to_match
, 0))
185 * If we're given patterns, accept only tags which match at least one
190 struct string_list_item
*item
;
192 for_each_string_list_item(item
, &patterns
) {
193 if (!wildmatch(item
->string
, path_to_match
, 0)) {
203 /* Is it annotated? */
204 if (!peel_iterated_oid(oid
, &peeled
)) {
205 is_annotated
= !oideq(oid
, &peeled
);
207 oidcpy(&peeled
, oid
);
212 * By default, we only use annotated tags, but with --tags
213 * we fall back to lightweight ones (even without --tags,
214 * we still remember lightweight ones, only to give hints
215 * in an error message). --all allows any refs to be used.
224 add_to_known_names(all
? path
+ 5 : path
+ 10, &peeled
, prio
, oid
);
228 struct possible_tag
{
229 struct commit_name
*name
;
232 unsigned flag_within
;
235 static int compare_pt(const void *a_
, const void *b_
)
237 struct possible_tag
*a
= (struct possible_tag
*)a_
;
238 struct possible_tag
*b
= (struct possible_tag
*)b_
;
239 if (a
->depth
!= b
->depth
)
240 return a
->depth
- b
->depth
;
241 if (a
->found_order
!= b
->found_order
)
242 return a
->found_order
- b
->found_order
;
246 static unsigned long finish_depth_computation(
247 struct commit_list
**list
,
248 struct possible_tag
*best
)
250 unsigned long seen_commits
= 0;
252 struct commit
*c
= pop_commit(list
);
253 struct commit_list
*parents
= c
->parents
;
255 if (c
->object
.flags
& best
->flag_within
) {
256 struct commit_list
*a
= *list
;
258 struct commit
*i
= a
->item
;
259 if (!(i
->object
.flags
& best
->flag_within
))
268 struct commit
*p
= parents
->item
;
269 repo_parse_commit(the_repository
, p
);
270 if (!(p
->object
.flags
& SEEN
))
271 commit_list_insert_by_date(p
, list
);
272 p
->object
.flags
|= c
->object
.flags
;
273 parents
= parents
->next
;
279 static void append_name(struct commit_name
*n
, struct strbuf
*dst
)
281 if (n
->prio
== 2 && !n
->tag
) {
282 n
->tag
= lookup_tag(the_repository
, &n
->oid
);
283 if (!n
->tag
|| parse_tag(n
->tag
))
284 die(_("annotated tag %s not available"), n
->path
);
286 if (n
->tag
&& !n
->name_checked
) {
287 if (strcmp(n
->tag
->tag
, all
? n
->path
+ 5 : n
->path
)) {
288 warning(_("tag '%s' is externally known as '%s'"),
289 n
->path
, n
->tag
->tag
);
297 strbuf_addstr(dst
, "tags/");
298 strbuf_addstr(dst
, n
->tag
->tag
);
300 strbuf_addstr(dst
, n
->path
);
304 static void append_suffix(int depth
, const struct object_id
*oid
, struct strbuf
*dst
)
306 strbuf_addf(dst
, "-%d-g%s", depth
,
307 repo_find_unique_abbrev(the_repository
, oid
, abbrev
));
310 static void describe_commit(struct object_id
*oid
, struct strbuf
*dst
)
312 struct commit
*cmit
, *gave_up_on
= NULL
;
313 struct commit_list
*list
;
314 struct commit_name
*n
;
315 struct possible_tag all_matches
[MAX_TAGS
];
316 unsigned int match_cnt
= 0, annotated_cnt
= 0, cur_match
;
317 unsigned long seen_commits
= 0;
318 unsigned int unannotated_cnt
= 0;
320 cmit
= lookup_commit_reference(the_repository
, oid
);
322 n
= find_commit_name(&cmit
->object
.oid
);
323 if (n
&& (tags
|| all
|| n
->prio
== 2)) {
325 * Exact match to an existing ref.
328 if (n
->misnamed
|| longformat
)
329 append_suffix(0, n
->tag
? get_tagged_oid(n
->tag
) : oid
, dst
);
331 strbuf_addstr(dst
, suffix
);
336 die(_("no tag exactly matches '%s'"), oid_to_hex(&cmit
->object
.oid
));
338 fprintf(stderr
, _("No exact match on refs or tags, searching to describe\n"));
341 struct hashmap_iter iter
;
343 struct commit_name
*n
;
345 init_commit_names(&commit_names
);
346 hashmap_for_each_entry(&names
, &iter
, n
,
347 entry
/* member name */) {
348 c
= lookup_commit_reference_gently(the_repository
,
351 *commit_names_at(&commit_names
, c
) = n
;
357 cmit
->object
.flags
= SEEN
;
358 commit_list_insert(cmit
, &list
);
360 struct commit
*c
= pop_commit(&list
);
361 struct commit_list
*parents
= c
->parents
;
362 struct commit_name
**slot
;
365 slot
= commit_names_peek(&commit_names
, c
);
366 n
= slot
? *slot
: NULL
;
368 if (!tags
&& !all
&& n
->prio
< 2) {
370 } else if (match_cnt
< max_candidates
) {
371 struct possible_tag
*t
= &all_matches
[match_cnt
++];
373 t
->depth
= seen_commits
- 1;
374 t
->flag_within
= 1u << match_cnt
;
375 t
->found_order
= match_cnt
;
376 c
->object
.flags
|= t
->flag_within
;
385 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
386 struct possible_tag
*t
= &all_matches
[cur_match
];
387 if (!(c
->object
.flags
& t
->flag_within
))
390 /* Stop if last remaining path already covered by best candidate(s) */
391 if (annotated_cnt
&& !list
) {
392 int best_depth
= INT_MAX
;
393 unsigned best_within
= 0;
394 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
395 struct possible_tag
*t
= &all_matches
[cur_match
];
396 if (t
->depth
< best_depth
) {
397 best_depth
= t
->depth
;
398 best_within
= t
->flag_within
;
399 } else if (t
->depth
== best_depth
) {
400 best_within
|= t
->flag_within
;
403 if ((c
->object
.flags
& best_within
) == best_within
) {
405 fprintf(stderr
, _("finished search at %s\n"),
406 oid_to_hex(&c
->object
.oid
));
411 struct commit
*p
= parents
->item
;
412 repo_parse_commit(the_repository
, p
);
413 if (!(p
->object
.flags
& SEEN
))
414 commit_list_insert_by_date(p
, &list
);
415 p
->object
.flags
|= c
->object
.flags
;
416 parents
= parents
->next
;
424 struct object_id
*cmit_oid
= &cmit
->object
.oid
;
426 strbuf_add_unique_abbrev(dst
, cmit_oid
, abbrev
);
428 strbuf_addstr(dst
, suffix
);
432 die(_("No annotated tags can describe '%s'.\n"
433 "However, there were unannotated tags: try --tags."),
434 oid_to_hex(cmit_oid
));
436 die(_("No tags can describe '%s'.\n"
437 "Try --always, or create some tags."),
438 oid_to_hex(cmit_oid
));
441 QSORT(all_matches
, match_cnt
, compare_pt
);
444 commit_list_insert_by_date(gave_up_on
, &list
);
447 seen_commits
+= finish_depth_computation(&list
, &all_matches
[0]);
448 free_commit_list(list
);
451 static int label_width
= -1;
452 if (label_width
< 0) {
454 for (i
= 0; i
< ARRAY_SIZE(prio_names
); i
++) {
455 w
= strlen(_(prio_names
[i
]));
460 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
461 struct possible_tag
*t
= &all_matches
[cur_match
];
462 fprintf(stderr
, " %-*s %8d %s\n",
463 label_width
, _(prio_names
[t
->name
->prio
]),
464 t
->depth
, t
->name
->path
);
466 fprintf(stderr
, _("traversed %lu commits\n"), seen_commits
);
469 _("more than %i tags found; listed %i most recent\n"
470 "gave up search at %s\n"),
471 max_candidates
, max_candidates
,
472 oid_to_hex(&gave_up_on
->object
.oid
));
476 append_name(all_matches
[0].name
, dst
);
477 if (all_matches
[0].name
->misnamed
|| abbrev
)
478 append_suffix(all_matches
[0].depth
, &cmit
->object
.oid
, dst
);
480 strbuf_addstr(dst
, suffix
);
483 struct process_commit_data
{
484 struct object_id current_commit
;
485 struct object_id looking_for
;
487 struct rev_info
*revs
;
490 static void process_commit(struct commit
*commit
, void *data
)
492 struct process_commit_data
*pcd
= data
;
493 pcd
->current_commit
= commit
->object
.oid
;
496 static void process_object(struct object
*obj
, const char *path
, void *data
)
498 struct process_commit_data
*pcd
= data
;
500 if (oideq(&pcd
->looking_for
, &obj
->oid
) && !pcd
->dst
->len
) {
501 reset_revision_walk();
502 describe_commit(&pcd
->current_commit
, pcd
->dst
);
503 strbuf_addf(pcd
->dst
, ":%s", path
);
504 free_commit_list(pcd
->revs
->commits
);
505 pcd
->revs
->commits
= NULL
;
509 static void describe_blob(struct object_id oid
, struct strbuf
*dst
)
511 struct rev_info revs
;
512 struct strvec args
= STRVEC_INIT
;
513 struct process_commit_data pcd
= { *null_oid(), oid
, dst
, &revs
};
515 strvec_pushl(&args
, "internal: The first arg is not parsed",
516 "--objects", "--in-commit-order", "--reverse", "HEAD",
519 repo_init_revisions(the_repository
, &revs
, NULL
);
520 if (setup_revisions(args
.nr
, args
.v
, &revs
, NULL
) > 1)
521 BUG("setup_revisions could not handle all args?");
523 if (prepare_revision_walk(&revs
))
524 die("revision walk setup failed");
526 traverse_commit_list(&revs
, process_commit
, process_object
, &pcd
);
527 reset_revision_walk();
528 release_revisions(&revs
);
531 static void describe(const char *arg
, int last_one
)
533 struct object_id oid
;
535 struct strbuf sb
= STRBUF_INIT
;
538 fprintf(stderr
, _("describe %s\n"), arg
);
540 if (repo_get_oid(the_repository
, arg
, &oid
))
541 die(_("Not a valid object name %s"), arg
);
542 cmit
= lookup_commit_reference_gently(the_repository
, &oid
, 1);
545 describe_commit(&oid
, &sb
);
546 else if (oid_object_info(the_repository
, &oid
, NULL
) == OBJ_BLOB
)
547 describe_blob(oid
, &sb
);
549 die(_("%s is neither a commit nor blob"), arg
);
554 clear_commit_marks(cmit
, -1);
559 int cmd_describe(int argc
, const char **argv
, const char *prefix
)
562 struct option options
[] = {
563 OPT_BOOL(0, "contains", &contains
, N_("find the tag that comes after the commit")),
564 OPT_BOOL(0, "debug", &debug
, N_("debug search strategy on stderr")),
565 OPT_BOOL(0, "all", &all
, N_("use any ref")),
566 OPT_BOOL(0, "tags", &tags
, N_("use any tag, even unannotated")),
567 OPT_BOOL(0, "long", &longformat
, N_("always use long format")),
568 OPT_BOOL(0, "first-parent", &first_parent
, N_("only follow first parent")),
569 OPT__ABBREV(&abbrev
),
570 OPT_SET_INT(0, "exact-match", &max_candidates
,
571 N_("only output exact matches"), 0),
572 OPT_INTEGER(0, "candidates", &max_candidates
,
573 N_("consider <n> most recent tags (default: 10)")),
574 OPT_STRING_LIST(0, "match", &patterns
, N_("pattern"),
575 N_("only consider tags matching <pattern>")),
576 OPT_STRING_LIST(0, "exclude", &exclude_patterns
, N_("pattern"),
577 N_("do not consider tags matching <pattern>")),
578 OPT_BOOL(0, "always", &always
,
579 N_("show abbreviated commit object as fallback")),
580 {OPTION_STRING
, 0, "dirty", &dirty
, N_("mark"),
581 N_("append <mark> on dirty working tree (default: \"-dirty\")"),
582 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "-dirty"},
583 {OPTION_STRING
, 0, "broken", &broken
, N_("mark"),
584 N_("append <mark> on broken working tree (default: \"-broken\")"),
585 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "-broken"},
589 git_config(git_default_config
, NULL
);
590 argc
= parse_options(argc
, argv
, prefix
, options
, describe_usage
, 0);
592 abbrev
= DEFAULT_ABBREV
;
594 if (max_candidates
< 0)
596 else if (max_candidates
> MAX_TAGS
)
597 max_candidates
= MAX_TAGS
;
599 save_commit_buffer
= 0;
601 if (longformat
&& abbrev
== 0)
602 die(_("options '%s' and '%s' cannot be used together"), "--long", "--abbrev=0");
605 struct string_list_item
*item
;
609 strvec_pushl(&args
, "name-rev",
610 "--peel-tag", "--name-only", "--no-undefined",
613 strvec_push(&args
, "--always");
615 strvec_push(&args
, "--tags");
616 for_each_string_list_item(item
, &patterns
)
617 strvec_pushf(&args
, "--refs=refs/tags/%s", item
->string
);
618 for_each_string_list_item(item
, &exclude_patterns
)
619 strvec_pushf(&args
, "--exclude=refs/tags/%s", item
->string
);
622 strvec_pushv(&args
, argv
);
624 strvec_push(&args
, "HEAD");
625 return cmd_name_rev(args
.nr
, args
.v
, prefix
);
628 hashmap_init(&names
, commit_name_neq
, NULL
, 0);
629 for_each_rawref(get_name
, NULL
);
630 if (!hashmap_get_size(&names
) && !always
)
631 die(_("No names found, cannot describe anything."));
635 struct child_process cp
= CHILD_PROCESS_INIT
;
636 strvec_pushv(&cp
.args
, diff_index_args
);
644 switch (run_command(&cp
)) {
652 /* diff-index aborted abnormally */
656 struct lock_file index_lock
= LOCK_INIT
;
657 struct rev_info revs
;
658 struct strvec args
= STRVEC_INIT
;
662 prepare_repo_settings(the_repository
);
663 the_repository
->settings
.command_requires_full_index
= 0;
664 repo_read_index(the_repository
);
665 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
,
667 fd
= repo_hold_locked_index(the_repository
,
670 repo_update_index_if_able(the_repository
, &index_lock
);
672 repo_init_revisions(the_repository
, &revs
, prefix
);
673 strvec_pushv(&args
, diff_index_args
);
674 if (setup_revisions(args
.nr
, args
.v
, &revs
, NULL
) != 1)
675 BUG("malformed internal diff-index command line");
676 result
= run_diff_index(&revs
, 0);
678 if (!diff_result_code(&revs
.diffopt
, result
))
682 release_revisions(&revs
);
686 die(_("option '%s' and commit-ishes cannot be used together"), "--dirty");
688 die(_("option '%s' and commit-ishes cannot be used together"), "--broken");
691 describe(*argv
++, argc
== 0);