1 #define USE_THE_INDEX_VARIABLE
4 #include "environment.h"
13 #include "object-name.h"
14 #include "parse-options.h"
15 #include "read-cache-ll.h"
21 #include "run-command.h"
22 #include "object-store-ll.h"
23 #include "list-objects.h"
24 #include "commit-slab.h"
25 #include "wildmatch.h"
27 #define MAX_TAGS (FLAG_BITS - 1)
29 define_commit_slab(commit_names
, struct commit_name
*);
31 static const char * const describe_usage
[] = {
32 N_("git describe [--all] [--tags] [--contains] [--abbrev=<n>] [<commit-ish>...]"),
33 N_("git describe [--all] [--tags] [--contains] [--abbrev=<n>] --dirty[=<mark>]"),
34 N_("git describe <blob>"),
38 static int debug
; /* Display lots of verbose info */
39 static int all
; /* Any valid ref can be used */
40 static int tags
; /* Allow lightweight tags */
41 static int longformat
;
42 static int first_parent
;
43 static int abbrev
= -1; /* unspecified */
44 static int max_candidates
= 10;
45 static struct hashmap names
;
47 static struct string_list patterns
= STRING_LIST_INIT_NODUP
;
48 static struct string_list exclude_patterns
= STRING_LIST_INIT_NODUP
;
50 static const char *suffix
, *dirty
, *broken
;
51 static struct commit_names commit_names
;
53 /* diff-index command arguments to check if working tree is dirty. */
54 static const char *diff_index_args
[] = {
55 "diff-index", "--quiet", "HEAD", "--", NULL
59 struct hashmap_entry entry
;
60 struct object_id peeled
;
62 unsigned prio
:2; /* annotated tag = 2, tag = 1, head = 0 */
63 unsigned name_checked
:1;
69 static const char *prio_names
[] = {
70 N_("head"), N_("lightweight"), N_("annotated"),
73 static int commit_name_neq(const void *cmp_data UNUSED
,
74 const struct hashmap_entry
*eptr
,
75 const struct hashmap_entry
*entry_or_key
,
78 const struct commit_name
*cn1
, *cn2
;
80 cn1
= container_of(eptr
, const struct commit_name
, entry
);
81 cn2
= container_of(entry_or_key
, const struct commit_name
, entry
);
83 return !oideq(&cn1
->peeled
, peeled
? peeled
: &cn2
->peeled
);
86 static inline struct commit_name
*find_commit_name(const struct object_id
*peeled
)
88 return hashmap_get_entry_from_hash(&names
, oidhash(peeled
), peeled
,
89 struct commit_name
, entry
);
92 static int replace_name(struct commit_name
*e
,
94 const struct object_id
*oid
,
97 if (!e
|| e
->prio
< prio
)
100 if (e
->prio
== 2 && prio
== 2) {
101 /* Multiple annotated tags point to the same commit.
102 * Select one to keep based upon their tagger date.
107 t
= lookup_tag(the_repository
, &e
->oid
);
108 if (!t
|| parse_tag(t
))
113 t
= lookup_tag(the_repository
, oid
);
114 if (!t
|| parse_tag(t
))
118 if (e
->tag
->date
< t
->date
)
125 static void add_to_known_names(const char *path
,
126 const struct object_id
*peeled
,
128 const struct object_id
*oid
)
130 struct commit_name
*e
= find_commit_name(peeled
);
131 struct tag
*tag
= NULL
;
132 if (replace_name(e
, prio
, oid
, &tag
)) {
134 e
= xmalloc(sizeof(struct commit_name
));
135 oidcpy(&e
->peeled
, peeled
);
136 hashmap_entry_init(&e
->entry
, oidhash(peeled
));
137 hashmap_add(&names
, &e
->entry
);
144 oidcpy(&e
->oid
, oid
);
146 e
->path
= xstrdup(path
);
150 static int get_name(const char *path
, const struct object_id
*oid
,
151 int flag UNUSED
, void *cb_data UNUSED
)
154 struct object_id peeled
;
155 int is_annotated
, prio
;
156 const char *path_to_match
= NULL
;
158 if (skip_prefix(path
, "refs/tags/", &path_to_match
)) {
161 if ((exclude_patterns
.nr
|| patterns
.nr
) &&
162 !skip_prefix(path
, "refs/heads/", &path_to_match
) &&
163 !skip_prefix(path
, "refs/remotes/", &path_to_match
)) {
164 /* Only accept reference of known type if there are match/exclude patterns */
168 /* Reject anything outside refs/tags/ unless --all */
173 * If we're given exclude patterns, first exclude any tag which match
174 * any of the exclude pattern.
176 if (exclude_patterns
.nr
) {
177 struct string_list_item
*item
;
179 for_each_string_list_item(item
, &exclude_patterns
) {
180 if (!wildmatch(item
->string
, path_to_match
, 0))
186 * If we're given patterns, accept only tags which match at least one
191 struct string_list_item
*item
;
193 for_each_string_list_item(item
, &patterns
) {
194 if (!wildmatch(item
->string
, path_to_match
, 0)) {
204 /* Is it annotated? */
205 if (!peel_iterated_oid(oid
, &peeled
)) {
206 is_annotated
= !oideq(oid
, &peeled
);
208 oidcpy(&peeled
, oid
);
213 * By default, we only use annotated tags, but with --tags
214 * we fall back to lightweight ones (even without --tags,
215 * we still remember lightweight ones, only to give hints
216 * in an error message). --all allows any refs to be used.
225 add_to_known_names(all
? path
+ 5 : path
+ 10, &peeled
, prio
, oid
);
229 struct possible_tag
{
230 struct commit_name
*name
;
233 unsigned flag_within
;
236 static int compare_pt(const void *a_
, const void *b_
)
238 struct possible_tag
*a
= (struct possible_tag
*)a_
;
239 struct possible_tag
*b
= (struct possible_tag
*)b_
;
240 if (a
->depth
!= b
->depth
)
241 return a
->depth
- b
->depth
;
242 if (a
->found_order
!= b
->found_order
)
243 return a
->found_order
- b
->found_order
;
247 static unsigned long finish_depth_computation(
248 struct commit_list
**list
,
249 struct possible_tag
*best
)
251 unsigned long seen_commits
= 0;
253 struct commit
*c
= pop_commit(list
);
254 struct commit_list
*parents
= c
->parents
;
256 if (c
->object
.flags
& best
->flag_within
) {
257 struct commit_list
*a
= *list
;
259 struct commit
*i
= a
->item
;
260 if (!(i
->object
.flags
& best
->flag_within
))
269 struct commit
*p
= parents
->item
;
270 repo_parse_commit(the_repository
, p
);
271 if (!(p
->object
.flags
& SEEN
))
272 commit_list_insert_by_date(p
, list
);
273 p
->object
.flags
|= c
->object
.flags
;
274 parents
= parents
->next
;
280 static void append_name(struct commit_name
*n
, struct strbuf
*dst
)
282 if (n
->prio
== 2 && !n
->tag
) {
283 n
->tag
= lookup_tag(the_repository
, &n
->oid
);
284 if (!n
->tag
|| parse_tag(n
->tag
))
285 die(_("annotated tag %s not available"), n
->path
);
287 if (n
->tag
&& !n
->name_checked
) {
288 if (strcmp(n
->tag
->tag
, all
? n
->path
+ 5 : n
->path
)) {
289 warning(_("tag '%s' is externally known as '%s'"),
290 n
->path
, n
->tag
->tag
);
298 strbuf_addstr(dst
, "tags/");
299 strbuf_addstr(dst
, n
->tag
->tag
);
301 strbuf_addstr(dst
, n
->path
);
305 static void append_suffix(int depth
, const struct object_id
*oid
, struct strbuf
*dst
)
307 strbuf_addf(dst
, "-%d-g%s", depth
,
308 repo_find_unique_abbrev(the_repository
, oid
, abbrev
));
311 static void describe_commit(struct object_id
*oid
, struct strbuf
*dst
)
313 struct commit
*cmit
, *gave_up_on
= NULL
;
314 struct commit_list
*list
;
315 struct commit_name
*n
;
316 struct possible_tag all_matches
[MAX_TAGS
];
317 unsigned int match_cnt
= 0, annotated_cnt
= 0, cur_match
;
318 unsigned long seen_commits
= 0;
319 unsigned int unannotated_cnt
= 0;
321 cmit
= lookup_commit_reference(the_repository
, oid
);
323 n
= find_commit_name(&cmit
->object
.oid
);
324 if (n
&& (tags
|| all
|| n
->prio
== 2)) {
326 * Exact match to an existing ref.
329 if (n
->misnamed
|| longformat
)
330 append_suffix(0, n
->tag
? get_tagged_oid(n
->tag
) : oid
, dst
);
332 strbuf_addstr(dst
, suffix
);
337 die(_("no tag exactly matches '%s'"), oid_to_hex(&cmit
->object
.oid
));
339 fprintf(stderr
, _("No exact match on refs or tags, searching to describe\n"));
342 struct hashmap_iter iter
;
344 struct commit_name
*n
;
346 init_commit_names(&commit_names
);
347 hashmap_for_each_entry(&names
, &iter
, n
,
348 entry
/* member name */) {
349 c
= lookup_commit_reference_gently(the_repository
,
352 *commit_names_at(&commit_names
, c
) = n
;
358 cmit
->object
.flags
= SEEN
;
359 commit_list_insert(cmit
, &list
);
361 struct commit
*c
= pop_commit(&list
);
362 struct commit_list
*parents
= c
->parents
;
363 struct commit_name
**slot
;
366 slot
= commit_names_peek(&commit_names
, c
);
367 n
= slot
? *slot
: NULL
;
369 if (!tags
&& !all
&& n
->prio
< 2) {
371 } else if (match_cnt
< max_candidates
) {
372 struct possible_tag
*t
= &all_matches
[match_cnt
++];
374 t
->depth
= seen_commits
- 1;
375 t
->flag_within
= 1u << match_cnt
;
376 t
->found_order
= match_cnt
;
377 c
->object
.flags
|= t
->flag_within
;
386 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
387 struct possible_tag
*t
= &all_matches
[cur_match
];
388 if (!(c
->object
.flags
& t
->flag_within
))
391 /* Stop if last remaining path already covered by best candidate(s) */
392 if (annotated_cnt
&& !list
) {
393 int best_depth
= INT_MAX
;
394 unsigned best_within
= 0;
395 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
396 struct possible_tag
*t
= &all_matches
[cur_match
];
397 if (t
->depth
< best_depth
) {
398 best_depth
= t
->depth
;
399 best_within
= t
->flag_within
;
400 } else if (t
->depth
== best_depth
) {
401 best_within
|= t
->flag_within
;
404 if ((c
->object
.flags
& best_within
) == best_within
) {
406 fprintf(stderr
, _("finished search at %s\n"),
407 oid_to_hex(&c
->object
.oid
));
412 struct commit
*p
= parents
->item
;
413 repo_parse_commit(the_repository
, p
);
414 if (!(p
->object
.flags
& SEEN
))
415 commit_list_insert_by_date(p
, &list
);
416 p
->object
.flags
|= c
->object
.flags
;
417 parents
= parents
->next
;
425 struct object_id
*cmit_oid
= &cmit
->object
.oid
;
427 strbuf_add_unique_abbrev(dst
, cmit_oid
, abbrev
);
429 strbuf_addstr(dst
, suffix
);
433 die(_("No annotated tags can describe '%s'.\n"
434 "However, there were unannotated tags: try --tags."),
435 oid_to_hex(cmit_oid
));
437 die(_("No tags can describe '%s'.\n"
438 "Try --always, or create some tags."),
439 oid_to_hex(cmit_oid
));
442 QSORT(all_matches
, match_cnt
, compare_pt
);
445 commit_list_insert_by_date(gave_up_on
, &list
);
448 seen_commits
+= finish_depth_computation(&list
, &all_matches
[0]);
449 free_commit_list(list
);
452 static int label_width
= -1;
453 if (label_width
< 0) {
455 for (i
= 0; i
< ARRAY_SIZE(prio_names
); i
++) {
456 w
= strlen(_(prio_names
[i
]));
461 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
462 struct possible_tag
*t
= &all_matches
[cur_match
];
463 fprintf(stderr
, " %-*s %8d %s\n",
464 label_width
, _(prio_names
[t
->name
->prio
]),
465 t
->depth
, t
->name
->path
);
467 fprintf(stderr
, _("traversed %lu commits\n"), seen_commits
);
470 _("more than %i tags found; listed %i most recent\n"
471 "gave up search at %s\n"),
472 max_candidates
, max_candidates
,
473 oid_to_hex(&gave_up_on
->object
.oid
));
477 append_name(all_matches
[0].name
, dst
);
478 if (all_matches
[0].name
->misnamed
|| abbrev
)
479 append_suffix(all_matches
[0].depth
, &cmit
->object
.oid
, dst
);
481 strbuf_addstr(dst
, suffix
);
484 struct process_commit_data
{
485 struct object_id current_commit
;
486 struct object_id looking_for
;
488 struct rev_info
*revs
;
491 static void process_commit(struct commit
*commit
, void *data
)
493 struct process_commit_data
*pcd
= data
;
494 pcd
->current_commit
= commit
->object
.oid
;
497 static void process_object(struct object
*obj
, const char *path
, void *data
)
499 struct process_commit_data
*pcd
= data
;
501 if (oideq(&pcd
->looking_for
, &obj
->oid
) && !pcd
->dst
->len
) {
502 reset_revision_walk();
503 describe_commit(&pcd
->current_commit
, pcd
->dst
);
504 strbuf_addf(pcd
->dst
, ":%s", path
);
505 free_commit_list(pcd
->revs
->commits
);
506 pcd
->revs
->commits
= NULL
;
510 static void describe_blob(struct object_id oid
, struct strbuf
*dst
)
512 struct rev_info revs
;
513 struct strvec args
= STRVEC_INIT
;
514 struct process_commit_data pcd
= { *null_oid(), oid
, dst
, &revs
};
516 strvec_pushl(&args
, "internal: The first arg is not parsed",
517 "--objects", "--in-commit-order", "--reverse", "HEAD",
520 repo_init_revisions(the_repository
, &revs
, NULL
);
521 if (setup_revisions(args
.nr
, args
.v
, &revs
, NULL
) > 1)
522 BUG("setup_revisions could not handle all args?");
524 if (prepare_revision_walk(&revs
))
525 die("revision walk setup failed");
527 traverse_commit_list(&revs
, process_commit
, process_object
, &pcd
);
528 reset_revision_walk();
529 release_revisions(&revs
);
532 static void describe(const char *arg
, int last_one
)
534 struct object_id oid
;
536 struct strbuf sb
= STRBUF_INIT
;
539 fprintf(stderr
, _("describe %s\n"), arg
);
541 if (repo_get_oid(the_repository
, arg
, &oid
))
542 die(_("Not a valid object name %s"), arg
);
543 cmit
= lookup_commit_reference_gently(the_repository
, &oid
, 1);
546 describe_commit(&oid
, &sb
);
547 else if (oid_object_info(the_repository
, &oid
, NULL
) == OBJ_BLOB
)
548 describe_blob(oid
, &sb
);
550 die(_("%s is neither a commit nor blob"), arg
);
555 clear_commit_marks(cmit
, -1);
560 int cmd_describe(int argc
, const char **argv
, const char *prefix
)
563 struct option options
[] = {
564 OPT_BOOL(0, "contains", &contains
, N_("find the tag that comes after the commit")),
565 OPT_BOOL(0, "debug", &debug
, N_("debug search strategy on stderr")),
566 OPT_BOOL(0, "all", &all
, N_("use any ref")),
567 OPT_BOOL(0, "tags", &tags
, N_("use any tag, even unannotated")),
568 OPT_BOOL(0, "long", &longformat
, N_("always use long format")),
569 OPT_BOOL(0, "first-parent", &first_parent
, N_("only follow first parent")),
570 OPT__ABBREV(&abbrev
),
571 OPT_SET_INT(0, "exact-match", &max_candidates
,
572 N_("only output exact matches"), 0),
573 OPT_INTEGER(0, "candidates", &max_candidates
,
574 N_("consider <n> most recent tags (default: 10)")),
575 OPT_STRING_LIST(0, "match", &patterns
, N_("pattern"),
576 N_("only consider tags matching <pattern>")),
577 OPT_STRING_LIST(0, "exclude", &exclude_patterns
, N_("pattern"),
578 N_("do not consider tags matching <pattern>")),
579 OPT_BOOL(0, "always", &always
,
580 N_("show abbreviated commit object as fallback")),
581 {OPTION_STRING
, 0, "dirty", &dirty
, N_("mark"),
582 N_("append <mark> on dirty working tree (default: \"-dirty\")"),
583 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "-dirty"},
584 {OPTION_STRING
, 0, "broken", &broken
, N_("mark"),
585 N_("append <mark> on broken working tree (default: \"-broken\")"),
586 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "-broken"},
590 git_config(git_default_config
, NULL
);
591 argc
= parse_options(argc
, argv
, prefix
, options
, describe_usage
, 0);
593 abbrev
= DEFAULT_ABBREV
;
595 if (max_candidates
< 0)
597 else if (max_candidates
> MAX_TAGS
)
598 max_candidates
= MAX_TAGS
;
600 save_commit_buffer
= 0;
602 if (longformat
&& abbrev
== 0)
603 die(_("options '%s' and '%s' cannot be used together"), "--long", "--abbrev=0");
606 struct string_list_item
*item
;
610 strvec_pushl(&args
, "name-rev",
611 "--peel-tag", "--name-only", "--no-undefined",
614 strvec_push(&args
, "--always");
616 strvec_push(&args
, "--tags");
617 for_each_string_list_item(item
, &patterns
)
618 strvec_pushf(&args
, "--refs=refs/tags/%s", item
->string
);
619 for_each_string_list_item(item
, &exclude_patterns
)
620 strvec_pushf(&args
, "--exclude=refs/tags/%s", item
->string
);
623 strvec_pushv(&args
, argv
);
625 strvec_push(&args
, "HEAD");
626 return cmd_name_rev(args
.nr
, args
.v
, prefix
);
629 hashmap_init(&names
, commit_name_neq
, NULL
, 0);
630 for_each_rawref(get_name
, NULL
);
631 if (!hashmap_get_size(&names
) && !always
)
632 die(_("No names found, cannot describe anything."));
636 struct child_process cp
= CHILD_PROCESS_INIT
;
637 strvec_pushv(&cp
.args
, diff_index_args
);
645 switch (run_command(&cp
)) {
653 /* diff-index aborted abnormally */
657 struct lock_file index_lock
= LOCK_INIT
;
658 struct rev_info revs
;
659 struct strvec args
= STRVEC_INIT
;
663 prepare_repo_settings(the_repository
);
664 the_repository
->settings
.command_requires_full_index
= 0;
665 repo_read_index(the_repository
);
666 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
,
668 fd
= repo_hold_locked_index(the_repository
,
671 repo_update_index_if_able(the_repository
, &index_lock
);
673 repo_init_revisions(the_repository
, &revs
, prefix
);
674 strvec_pushv(&args
, diff_index_args
);
675 if (setup_revisions(args
.nr
, args
.v
, &revs
, NULL
) != 1)
676 BUG("malformed internal diff-index command line");
677 result
= run_diff_index(&revs
, 0);
679 if (!diff_result_code(&revs
.diffopt
, result
))
683 release_revisions(&revs
);
687 die(_("option '%s' and commit-ishes cannot be used together"), "--dirty");
689 die(_("option '%s' and commit-ishes cannot be used together"), "--broken");
692 describe(*argv
++, argc
== 0);