1 #define USE_THE_INDEX_VARIABLE
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 [--all] [--tags] [--contains] [--abbrev=<n>] [<commit-ish>...]"),
27 N_("git describe [--all] [--tags] [--contains] [--abbrev=<n>] --dirty[=<mark>]"),
28 N_("git describe <blob>"),
32 static int debug
; /* Display lots of verbose info */
33 static int all
; /* Any valid ref can be used */
34 static int tags
; /* Allow lightweight tags */
35 static int longformat
;
36 static int first_parent
;
37 static int abbrev
= -1; /* unspecified */
38 static int max_candidates
= 10;
39 static struct hashmap names
;
41 static struct string_list patterns
= STRING_LIST_INIT_NODUP
;
42 static struct string_list exclude_patterns
= STRING_LIST_INIT_NODUP
;
44 static const char *suffix
, *dirty
, *broken
;
45 static struct commit_names commit_names
;
47 /* diff-index command arguments to check if working tree is dirty. */
48 static const char *diff_index_args
[] = {
49 "diff-index", "--quiet", "HEAD", "--", NULL
53 struct hashmap_entry entry
;
54 struct object_id peeled
;
56 unsigned prio
:2; /* annotated tag = 2, tag = 1, head = 0 */
57 unsigned name_checked
:1;
63 static const char *prio_names
[] = {
64 N_("head"), N_("lightweight"), N_("annotated"),
67 static int commit_name_neq(const void *cmp_data UNUSED
,
68 const struct hashmap_entry
*eptr
,
69 const struct hashmap_entry
*entry_or_key
,
72 const struct commit_name
*cn1
, *cn2
;
74 cn1
= container_of(eptr
, const struct commit_name
, entry
);
75 cn2
= container_of(entry_or_key
, const struct commit_name
, entry
);
77 return !oideq(&cn1
->peeled
, peeled
? peeled
: &cn2
->peeled
);
80 static inline struct commit_name
*find_commit_name(const struct object_id
*peeled
)
82 return hashmap_get_entry_from_hash(&names
, oidhash(peeled
), peeled
,
83 struct commit_name
, entry
);
86 static int replace_name(struct commit_name
*e
,
88 const struct object_id
*oid
,
91 if (!e
|| e
->prio
< prio
)
94 if (e
->prio
== 2 && prio
== 2) {
95 /* Multiple annotated tags point to the same commit.
96 * Select one to keep based upon their tagger date.
101 t
= lookup_tag(the_repository
, &e
->oid
);
102 if (!t
|| parse_tag(t
))
107 t
= lookup_tag(the_repository
, oid
);
108 if (!t
|| parse_tag(t
))
112 if (e
->tag
->date
< t
->date
)
119 static void add_to_known_names(const char *path
,
120 const struct object_id
*peeled
,
122 const struct object_id
*oid
)
124 struct commit_name
*e
= find_commit_name(peeled
);
125 struct tag
*tag
= NULL
;
126 if (replace_name(e
, prio
, oid
, &tag
)) {
128 e
= xmalloc(sizeof(struct commit_name
));
129 oidcpy(&e
->peeled
, peeled
);
130 hashmap_entry_init(&e
->entry
, oidhash(peeled
));
131 hashmap_add(&names
, &e
->entry
);
138 oidcpy(&e
->oid
, oid
);
140 e
->path
= xstrdup(path
);
144 static int get_name(const char *path
, const struct object_id
*oid
,
145 int flag UNUSED
, void *cb_data UNUSED
)
148 struct object_id peeled
;
149 int is_annotated
, prio
;
150 const char *path_to_match
= NULL
;
152 if (skip_prefix(path
, "refs/tags/", &path_to_match
)) {
155 if ((exclude_patterns
.nr
|| patterns
.nr
) &&
156 !skip_prefix(path
, "refs/heads/", &path_to_match
) &&
157 !skip_prefix(path
, "refs/remotes/", &path_to_match
)) {
158 /* Only accept reference of known type if there are match/exclude patterns */
162 /* Reject anything outside refs/tags/ unless --all */
167 * If we're given exclude patterns, first exclude any tag which match
168 * any of the exclude pattern.
170 if (exclude_patterns
.nr
) {
171 struct string_list_item
*item
;
173 for_each_string_list_item(item
, &exclude_patterns
) {
174 if (!wildmatch(item
->string
, path_to_match
, 0))
180 * If we're given patterns, accept only tags which match at least one
185 struct string_list_item
*item
;
187 for_each_string_list_item(item
, &patterns
) {
188 if (!wildmatch(item
->string
, path_to_match
, 0)) {
198 /* Is it annotated? */
199 if (!peel_iterated_oid(oid
, &peeled
)) {
200 is_annotated
= !oideq(oid
, &peeled
);
202 oidcpy(&peeled
, oid
);
207 * By default, we only use annotated tags, but with --tags
208 * we fall back to lightweight ones (even without --tags,
209 * we still remember lightweight ones, only to give hints
210 * in an error message). --all allows any refs to be used.
219 add_to_known_names(all
? path
+ 5 : path
+ 10, &peeled
, prio
, oid
);
223 struct possible_tag
{
224 struct commit_name
*name
;
227 unsigned flag_within
;
230 static int compare_pt(const void *a_
, const void *b_
)
232 struct possible_tag
*a
= (struct possible_tag
*)a_
;
233 struct possible_tag
*b
= (struct possible_tag
*)b_
;
234 if (a
->depth
!= b
->depth
)
235 return a
->depth
- b
->depth
;
236 if (a
->found_order
!= b
->found_order
)
237 return a
->found_order
- b
->found_order
;
241 static unsigned long finish_depth_computation(
242 struct commit_list
**list
,
243 struct possible_tag
*best
)
245 unsigned long seen_commits
= 0;
247 struct commit
*c
= pop_commit(list
);
248 struct commit_list
*parents
= c
->parents
;
250 if (c
->object
.flags
& best
->flag_within
) {
251 struct commit_list
*a
= *list
;
253 struct commit
*i
= a
->item
;
254 if (!(i
->object
.flags
& best
->flag_within
))
263 struct commit
*p
= parents
->item
;
265 if (!(p
->object
.flags
& SEEN
))
266 commit_list_insert_by_date(p
, list
);
267 p
->object
.flags
|= c
->object
.flags
;
268 parents
= parents
->next
;
274 static void append_name(struct commit_name
*n
, struct strbuf
*dst
)
276 if (n
->prio
== 2 && !n
->tag
) {
277 n
->tag
= lookup_tag(the_repository
, &n
->oid
);
278 if (!n
->tag
|| parse_tag(n
->tag
))
279 die(_("annotated tag %s not available"), n
->path
);
281 if (n
->tag
&& !n
->name_checked
) {
282 if (strcmp(n
->tag
->tag
, all
? n
->path
+ 5 : n
->path
)) {
283 warning(_("tag '%s' is externally known as '%s'"),
284 n
->path
, n
->tag
->tag
);
292 strbuf_addstr(dst
, "tags/");
293 strbuf_addstr(dst
, n
->tag
->tag
);
295 strbuf_addstr(dst
, n
->path
);
299 static void append_suffix(int depth
, const struct object_id
*oid
, struct strbuf
*dst
)
301 strbuf_addf(dst
, "-%d-g%s", depth
, find_unique_abbrev(oid
, abbrev
));
304 static void describe_commit(struct object_id
*oid
, struct strbuf
*dst
)
306 struct commit
*cmit
, *gave_up_on
= NULL
;
307 struct commit_list
*list
;
308 struct commit_name
*n
;
309 struct possible_tag all_matches
[MAX_TAGS
];
310 unsigned int match_cnt
= 0, annotated_cnt
= 0, cur_match
;
311 unsigned long seen_commits
= 0;
312 unsigned int unannotated_cnt
= 0;
314 cmit
= lookup_commit_reference(the_repository
, oid
);
316 n
= find_commit_name(&cmit
->object
.oid
);
317 if (n
&& (tags
|| all
|| n
->prio
== 2)) {
319 * Exact match to an existing ref.
322 if (n
->misnamed
|| longformat
)
323 append_suffix(0, n
->tag
? get_tagged_oid(n
->tag
) : oid
, dst
);
325 strbuf_addstr(dst
, suffix
);
330 die(_("no tag exactly matches '%s'"), oid_to_hex(&cmit
->object
.oid
));
332 fprintf(stderr
, _("No exact match on refs or tags, searching to describe\n"));
335 struct hashmap_iter iter
;
337 struct commit_name
*n
;
339 init_commit_names(&commit_names
);
340 hashmap_for_each_entry(&names
, &iter
, n
,
341 entry
/* member name */) {
342 c
= lookup_commit_reference_gently(the_repository
,
345 *commit_names_at(&commit_names
, c
) = n
;
351 cmit
->object
.flags
= SEEN
;
352 commit_list_insert(cmit
, &list
);
354 struct commit
*c
= pop_commit(&list
);
355 struct commit_list
*parents
= c
->parents
;
356 struct commit_name
**slot
;
359 slot
= commit_names_peek(&commit_names
, c
);
360 n
= slot
? *slot
: NULL
;
362 if (!tags
&& !all
&& n
->prio
< 2) {
364 } else if (match_cnt
< max_candidates
) {
365 struct possible_tag
*t
= &all_matches
[match_cnt
++];
367 t
->depth
= seen_commits
- 1;
368 t
->flag_within
= 1u << match_cnt
;
369 t
->found_order
= match_cnt
;
370 c
->object
.flags
|= t
->flag_within
;
379 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
380 struct possible_tag
*t
= &all_matches
[cur_match
];
381 if (!(c
->object
.flags
& t
->flag_within
))
384 /* Stop if last remaining path already covered by best candidate(s) */
385 if (annotated_cnt
&& !list
) {
386 int best_depth
= INT_MAX
;
387 unsigned best_within
= 0;
388 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
389 struct possible_tag
*t
= &all_matches
[cur_match
];
390 if (t
->depth
< best_depth
) {
391 best_depth
= t
->depth
;
392 best_within
= t
->flag_within
;
393 } else if (t
->depth
== best_depth
) {
394 best_within
|= t
->flag_within
;
397 if ((c
->object
.flags
& best_within
) == best_within
) {
399 fprintf(stderr
, _("finished search at %s\n"),
400 oid_to_hex(&c
->object
.oid
));
405 struct commit
*p
= parents
->item
;
407 if (!(p
->object
.flags
& SEEN
))
408 commit_list_insert_by_date(p
, &list
);
409 p
->object
.flags
|= c
->object
.flags
;
410 parents
= parents
->next
;
418 struct object_id
*cmit_oid
= &cmit
->object
.oid
;
420 strbuf_add_unique_abbrev(dst
, cmit_oid
, abbrev
);
422 strbuf_addstr(dst
, suffix
);
426 die(_("No annotated tags can describe '%s'.\n"
427 "However, there were unannotated tags: try --tags."),
428 oid_to_hex(cmit_oid
));
430 die(_("No tags can describe '%s'.\n"
431 "Try --always, or create some tags."),
432 oid_to_hex(cmit_oid
));
435 QSORT(all_matches
, match_cnt
, compare_pt
);
438 commit_list_insert_by_date(gave_up_on
, &list
);
441 seen_commits
+= finish_depth_computation(&list
, &all_matches
[0]);
442 free_commit_list(list
);
445 static int label_width
= -1;
446 if (label_width
< 0) {
448 for (i
= 0; i
< ARRAY_SIZE(prio_names
); i
++) {
449 w
= strlen(_(prio_names
[i
]));
454 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
455 struct possible_tag
*t
= &all_matches
[cur_match
];
456 fprintf(stderr
, " %-*s %8d %s\n",
457 label_width
, _(prio_names
[t
->name
->prio
]),
458 t
->depth
, t
->name
->path
);
460 fprintf(stderr
, _("traversed %lu commits\n"), seen_commits
);
463 _("more than %i tags found; listed %i most recent\n"
464 "gave up search at %s\n"),
465 max_candidates
, max_candidates
,
466 oid_to_hex(&gave_up_on
->object
.oid
));
470 append_name(all_matches
[0].name
, dst
);
471 if (all_matches
[0].name
->misnamed
|| abbrev
)
472 append_suffix(all_matches
[0].depth
, &cmit
->object
.oid
, dst
);
474 strbuf_addstr(dst
, suffix
);
477 struct process_commit_data
{
478 struct object_id current_commit
;
479 struct object_id looking_for
;
481 struct rev_info
*revs
;
484 static void process_commit(struct commit
*commit
, void *data
)
486 struct process_commit_data
*pcd
= data
;
487 pcd
->current_commit
= commit
->object
.oid
;
490 static void process_object(struct object
*obj
, const char *path
, void *data
)
492 struct process_commit_data
*pcd
= data
;
494 if (oideq(&pcd
->looking_for
, &obj
->oid
) && !pcd
->dst
->len
) {
495 reset_revision_walk();
496 describe_commit(&pcd
->current_commit
, pcd
->dst
);
497 strbuf_addf(pcd
->dst
, ":%s", path
);
498 free_commit_list(pcd
->revs
->commits
);
499 pcd
->revs
->commits
= NULL
;
503 static void describe_blob(struct object_id oid
, struct strbuf
*dst
)
505 struct rev_info revs
;
506 struct strvec args
= STRVEC_INIT
;
507 struct process_commit_data pcd
= { *null_oid(), oid
, dst
, &revs
};
509 strvec_pushl(&args
, "internal: The first arg is not parsed",
510 "--objects", "--in-commit-order", "--reverse", "HEAD",
513 repo_init_revisions(the_repository
, &revs
, NULL
);
514 if (setup_revisions(args
.nr
, args
.v
, &revs
, NULL
) > 1)
515 BUG("setup_revisions could not handle all args?");
517 if (prepare_revision_walk(&revs
))
518 die("revision walk setup failed");
520 traverse_commit_list(&revs
, process_commit
, process_object
, &pcd
);
521 reset_revision_walk();
522 release_revisions(&revs
);
525 static void describe(const char *arg
, int last_one
)
527 struct object_id oid
;
529 struct strbuf sb
= STRBUF_INIT
;
532 fprintf(stderr
, _("describe %s\n"), arg
);
534 if (get_oid(arg
, &oid
))
535 die(_("Not a valid object name %s"), arg
);
536 cmit
= lookup_commit_reference_gently(the_repository
, &oid
, 1);
539 describe_commit(&oid
, &sb
);
540 else if (oid_object_info(the_repository
, &oid
, NULL
) == OBJ_BLOB
)
541 describe_blob(oid
, &sb
);
543 die(_("%s is neither a commit nor blob"), arg
);
548 clear_commit_marks(cmit
, -1);
553 int cmd_describe(int argc
, const char **argv
, const char *prefix
)
556 struct option options
[] = {
557 OPT_BOOL(0, "contains", &contains
, N_("find the tag that comes after the commit")),
558 OPT_BOOL(0, "debug", &debug
, N_("debug search strategy on stderr")),
559 OPT_BOOL(0, "all", &all
, N_("use any ref")),
560 OPT_BOOL(0, "tags", &tags
, N_("use any tag, even unannotated")),
561 OPT_BOOL(0, "long", &longformat
, N_("always use long format")),
562 OPT_BOOL(0, "first-parent", &first_parent
, N_("only follow first parent")),
563 OPT__ABBREV(&abbrev
),
564 OPT_SET_INT(0, "exact-match", &max_candidates
,
565 N_("only output exact matches"), 0),
566 OPT_INTEGER(0, "candidates", &max_candidates
,
567 N_("consider <n> most recent tags (default: 10)")),
568 OPT_STRING_LIST(0, "match", &patterns
, N_("pattern"),
569 N_("only consider tags matching <pattern>")),
570 OPT_STRING_LIST(0, "exclude", &exclude_patterns
, N_("pattern"),
571 N_("do not consider tags matching <pattern>")),
572 OPT_BOOL(0, "always", &always
,
573 N_("show abbreviated commit object as fallback")),
574 {OPTION_STRING
, 0, "dirty", &dirty
, N_("mark"),
575 N_("append <mark> on dirty working tree (default: \"-dirty\")"),
576 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "-dirty"},
577 {OPTION_STRING
, 0, "broken", &broken
, N_("mark"),
578 N_("append <mark> on broken working tree (default: \"-broken\")"),
579 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "-broken"},
583 git_config(git_default_config
, NULL
);
584 argc
= parse_options(argc
, argv
, prefix
, options
, describe_usage
, 0);
586 abbrev
= DEFAULT_ABBREV
;
588 if (max_candidates
< 0)
590 else if (max_candidates
> MAX_TAGS
)
591 max_candidates
= MAX_TAGS
;
593 save_commit_buffer
= 0;
595 if (longformat
&& abbrev
== 0)
596 die(_("options '%s' and '%s' cannot be used together"), "--long", "--abbrev=0");
599 struct string_list_item
*item
;
603 strvec_pushl(&args
, "name-rev",
604 "--peel-tag", "--name-only", "--no-undefined",
607 strvec_push(&args
, "--always");
609 strvec_push(&args
, "--tags");
610 for_each_string_list_item(item
, &patterns
)
611 strvec_pushf(&args
, "--refs=refs/tags/%s", item
->string
);
612 for_each_string_list_item(item
, &exclude_patterns
)
613 strvec_pushf(&args
, "--exclude=refs/tags/%s", item
->string
);
616 strvec_pushv(&args
, argv
);
618 strvec_push(&args
, "HEAD");
619 return cmd_name_rev(args
.nr
, args
.v
, prefix
);
622 hashmap_init(&names
, commit_name_neq
, NULL
, 0);
623 for_each_rawref(get_name
, NULL
);
624 if (!hashmap_get_size(&names
) && !always
)
625 die(_("No names found, cannot describe anything."));
629 struct child_process cp
= CHILD_PROCESS_INIT
;
630 strvec_pushv(&cp
.args
, diff_index_args
);
638 switch (run_command(&cp
)) {
646 /* diff-index aborted abnormally */
650 struct lock_file index_lock
= LOCK_INIT
;
651 struct rev_info revs
;
652 struct strvec args
= STRVEC_INIT
;
656 repo_read_index(the_repository
);
657 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
,
659 fd
= repo_hold_locked_index(the_repository
,
662 repo_update_index_if_able(the_repository
, &index_lock
);
664 repo_init_revisions(the_repository
, &revs
, prefix
);
665 strvec_pushv(&args
, diff_index_args
);
666 if (setup_revisions(args
.nr
, args
.v
, &revs
, NULL
) != 1)
667 BUG("malformed internal diff-index command line");
668 result
= run_diff_index(&revs
, 0);
670 if (!diff_result_code(&revs
.diffopt
, result
))
674 release_revisions(&revs
);
678 die(_("option '%s' and commit-ishes cannot be used together"), "--dirty");
680 die(_("option '%s' and commit-ishes cannot be used together"), "--broken");
683 describe(*argv
++, argc
== 0);