1 #define USE_THE_INDEX_COMPATIBILITY_MACROS
11 #include "parse-options.h"
15 #include "argv-array.h"
16 #include "run-command.h"
17 #include "object-store.h"
19 #include "list-objects.h"
20 #include "commit-slab.h"
22 #define MAX_TAGS (FLAG_BITS - 1)
24 define_commit_slab(commit_names
, struct commit_name
*);
26 static const char * const describe_usage
[] = {
27 N_("git describe [<options>] [<commit-ish>...]"),
28 N_("git describe [<options>] --dirty"),
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;
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
);
136 oidcpy(&e
->oid
, oid
);
138 e
->path
= xstrdup(path
);
142 static int get_name(const char *path
, const struct object_id
*oid
, int flag
, void *cb_data
)
145 struct object_id peeled
;
146 int is_annotated
, prio
;
147 const char *path_to_match
= NULL
;
149 if (skip_prefix(path
, "refs/tags/", &path_to_match
)) {
152 if ((exclude_patterns
.nr
|| patterns
.nr
) &&
153 !skip_prefix(path
, "refs/heads/", &path_to_match
) &&
154 !skip_prefix(path
, "refs/remotes/", &path_to_match
)) {
155 /* Only accept reference of known type if there are match/exclude patterns */
159 /* Reject anything outside refs/tags/ unless --all */
164 * If we're given exclude patterns, first exclude any tag which match
165 * any of the exclude pattern.
167 if (exclude_patterns
.nr
) {
168 struct string_list_item
*item
;
170 for_each_string_list_item(item
, &exclude_patterns
) {
171 if (!wildmatch(item
->string
, path_to_match
, 0))
177 * If we're given patterns, accept only tags which match at least one
182 struct string_list_item
*item
;
184 for_each_string_list_item(item
, &patterns
) {
185 if (!wildmatch(item
->string
, path_to_match
, 0)) {
195 /* Is it annotated? */
196 if (!peel_ref(path
, &peeled
)) {
197 is_annotated
= !oideq(oid
, &peeled
);
199 oidcpy(&peeled
, oid
);
204 * By default, we only use annotated tags, but with --tags
205 * we fall back to lightweight ones (even without --tags,
206 * we still remember lightweight ones, only to give hints
207 * in an error message). --all allows any refs to be used.
216 add_to_known_names(all
? path
+ 5 : path
+ 10, &peeled
, prio
, oid
);
220 struct possible_tag
{
221 struct commit_name
*name
;
224 unsigned flag_within
;
227 static int compare_pt(const void *a_
, const void *b_
)
229 struct possible_tag
*a
= (struct possible_tag
*)a_
;
230 struct possible_tag
*b
= (struct possible_tag
*)b_
;
231 if (a
->depth
!= b
->depth
)
232 return a
->depth
- b
->depth
;
233 if (a
->found_order
!= b
->found_order
)
234 return a
->found_order
- b
->found_order
;
238 static unsigned long finish_depth_computation(
239 struct commit_list
**list
,
240 struct possible_tag
*best
)
242 unsigned long seen_commits
= 0;
244 struct commit
*c
= pop_commit(list
);
245 struct commit_list
*parents
= c
->parents
;
247 if (c
->object
.flags
& best
->flag_within
) {
248 struct commit_list
*a
= *list
;
250 struct commit
*i
= a
->item
;
251 if (!(i
->object
.flags
& best
->flag_within
))
260 struct commit
*p
= parents
->item
;
262 if (!(p
->object
.flags
& SEEN
))
263 commit_list_insert_by_date(p
, list
);
264 p
->object
.flags
|= c
->object
.flags
;
265 parents
= parents
->next
;
271 static void append_name(struct commit_name
*n
, struct strbuf
*dst
)
273 if (n
->prio
== 2 && !n
->tag
) {
274 n
->tag
= lookup_tag(the_repository
, &n
->oid
);
275 if (!n
->tag
|| parse_tag(n
->tag
))
276 die(_("annotated tag %s not available"), n
->path
);
278 if (n
->tag
&& !n
->name_checked
) {
280 die(_("annotated tag %s has no embedded name"), n
->path
);
281 if (strcmp(n
->tag
->tag
, all
? n
->path
+ 5 : n
->path
))
282 warning(_("tag '%s' is really '%s' here"), n
->tag
->tag
, n
->path
);
288 strbuf_addstr(dst
, "tags/");
289 strbuf_addstr(dst
, n
->tag
->tag
);
291 strbuf_addstr(dst
, n
->path
);
295 static void append_suffix(int depth
, const struct object_id
*oid
, struct strbuf
*dst
)
297 strbuf_addf(dst
, "-%d-g%s", depth
, find_unique_abbrev(oid
, abbrev
));
300 static void describe_commit(struct object_id
*oid
, struct strbuf
*dst
)
302 struct commit
*cmit
, *gave_up_on
= NULL
;
303 struct commit_list
*list
;
304 struct commit_name
*n
;
305 struct possible_tag all_matches
[MAX_TAGS
];
306 unsigned int match_cnt
= 0, annotated_cnt
= 0, cur_match
;
307 unsigned long seen_commits
= 0;
308 unsigned int unannotated_cnt
= 0;
310 cmit
= lookup_commit_reference(the_repository
, oid
);
312 n
= find_commit_name(&cmit
->object
.oid
);
313 if (n
&& (tags
|| all
|| n
->prio
== 2)) {
315 * Exact match to an existing ref.
319 append_suffix(0, n
->tag
? &n
->tag
->tagged
->oid
: oid
, dst
);
321 strbuf_addstr(dst
, suffix
);
326 die(_("no tag exactly matches '%s'"), oid_to_hex(&cmit
->object
.oid
));
328 fprintf(stderr
, _("No exact match on refs or tags, searching to describe\n"));
331 struct hashmap_iter iter
;
333 struct commit_name
*n
;
335 init_commit_names(&commit_names
);
336 n
= hashmap_iter_first(&names
, &iter
);
337 for (; n
; n
= hashmap_iter_next(&iter
)) {
338 c
= lookup_commit_reference_gently(the_repository
,
341 *commit_names_at(&commit_names
, c
) = n
;
347 cmit
->object
.flags
= SEEN
;
348 commit_list_insert(cmit
, &list
);
350 struct commit
*c
= pop_commit(&list
);
351 struct commit_list
*parents
= c
->parents
;
352 struct commit_name
**slot
;
355 slot
= commit_names_peek(&commit_names
, c
);
356 n
= slot
? *slot
: NULL
;
358 if (!tags
&& !all
&& n
->prio
< 2) {
360 } else if (match_cnt
< max_candidates
) {
361 struct possible_tag
*t
= &all_matches
[match_cnt
++];
363 t
->depth
= seen_commits
- 1;
364 t
->flag_within
= 1u << match_cnt
;
365 t
->found_order
= match_cnt
;
366 c
->object
.flags
|= t
->flag_within
;
375 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
376 struct possible_tag
*t
= &all_matches
[cur_match
];
377 if (!(c
->object
.flags
& t
->flag_within
))
380 if (annotated_cnt
&& !list
) {
382 fprintf(stderr
, _("finished search at %s\n"),
383 oid_to_hex(&c
->object
.oid
));
387 struct commit
*p
= parents
->item
;
389 if (!(p
->object
.flags
& SEEN
))
390 commit_list_insert_by_date(p
, &list
);
391 p
->object
.flags
|= c
->object
.flags
;
392 parents
= parents
->next
;
400 struct object_id
*cmit_oid
= &cmit
->object
.oid
;
402 strbuf_add_unique_abbrev(dst
, cmit_oid
, abbrev
);
404 strbuf_addstr(dst
, suffix
);
408 die(_("No annotated tags can describe '%s'.\n"
409 "However, there were unannotated tags: try --tags."),
410 oid_to_hex(cmit_oid
));
412 die(_("No tags can describe '%s'.\n"
413 "Try --always, or create some tags."),
414 oid_to_hex(cmit_oid
));
417 QSORT(all_matches
, match_cnt
, compare_pt
);
420 commit_list_insert_by_date(gave_up_on
, &list
);
423 seen_commits
+= finish_depth_computation(&list
, &all_matches
[0]);
424 free_commit_list(list
);
427 static int label_width
= -1;
428 if (label_width
< 0) {
430 for (i
= 0; i
< ARRAY_SIZE(prio_names
); i
++) {
431 w
= strlen(_(prio_names
[i
]));
436 for (cur_match
= 0; cur_match
< match_cnt
; cur_match
++) {
437 struct possible_tag
*t
= &all_matches
[cur_match
];
438 fprintf(stderr
, " %-*s %8d %s\n",
439 label_width
, _(prio_names
[t
->name
->prio
]),
440 t
->depth
, t
->name
->path
);
442 fprintf(stderr
, _("traversed %lu commits\n"), seen_commits
);
445 _("more than %i tags found; listed %i most recent\n"
446 "gave up search at %s\n"),
447 max_candidates
, max_candidates
,
448 oid_to_hex(&gave_up_on
->object
.oid
));
452 append_name(all_matches
[0].name
, dst
);
454 append_suffix(all_matches
[0].depth
, &cmit
->object
.oid
, dst
);
456 strbuf_addstr(dst
, suffix
);
459 struct process_commit_data
{
460 struct object_id current_commit
;
461 struct object_id looking_for
;
463 struct rev_info
*revs
;
466 static void process_commit(struct commit
*commit
, void *data
)
468 struct process_commit_data
*pcd
= data
;
469 pcd
->current_commit
= commit
->object
.oid
;
472 static void process_object(struct object
*obj
, const char *path
, void *data
)
474 struct process_commit_data
*pcd
= data
;
476 if (oideq(&pcd
->looking_for
, &obj
->oid
) && !pcd
->dst
->len
) {
477 reset_revision_walk();
478 describe_commit(&pcd
->current_commit
, pcd
->dst
);
479 strbuf_addf(pcd
->dst
, ":%s", path
);
480 free_commit_list(pcd
->revs
->commits
);
481 pcd
->revs
->commits
= NULL
;
485 static void describe_blob(struct object_id oid
, struct strbuf
*dst
)
487 struct rev_info revs
;
488 struct argv_array args
= ARGV_ARRAY_INIT
;
489 struct process_commit_data pcd
= { null_oid
, oid
, dst
, &revs
};
491 argv_array_pushl(&args
, "internal: The first arg is not parsed",
492 "--objects", "--in-commit-order", "--reverse", "HEAD",
495 repo_init_revisions(the_repository
, &revs
, NULL
);
496 if (setup_revisions(args
.argc
, args
.argv
, &revs
, NULL
) > 1)
497 BUG("setup_revisions could not handle all args?");
499 if (prepare_revision_walk(&revs
))
500 die("revision walk setup failed");
502 traverse_commit_list(&revs
, process_commit
, process_object
, &pcd
);
503 reset_revision_walk();
506 static void describe(const char *arg
, int last_one
)
508 struct object_id oid
;
510 struct strbuf sb
= STRBUF_INIT
;
513 fprintf(stderr
, _("describe %s\n"), arg
);
515 if (get_oid(arg
, &oid
))
516 die(_("Not a valid object name %s"), arg
);
517 cmit
= lookup_commit_reference_gently(the_repository
, &oid
, 1);
520 describe_commit(&oid
, &sb
);
521 else if (oid_object_info(the_repository
, &oid
, NULL
) == OBJ_BLOB
)
522 describe_blob(oid
, &sb
);
524 die(_("%s is neither a commit nor blob"), arg
);
529 clear_commit_marks(cmit
, -1);
534 int cmd_describe(int argc
, const char **argv
, const char *prefix
)
537 struct option options
[] = {
538 OPT_BOOL(0, "contains", &contains
, N_("find the tag that comes after the commit")),
539 OPT_BOOL(0, "debug", &debug
, N_("debug search strategy on stderr")),
540 OPT_BOOL(0, "all", &all
, N_("use any ref")),
541 OPT_BOOL(0, "tags", &tags
, N_("use any tag, even unannotated")),
542 OPT_BOOL(0, "long", &longformat
, N_("always use long format")),
543 OPT_BOOL(0, "first-parent", &first_parent
, N_("only follow first parent")),
544 OPT__ABBREV(&abbrev
),
545 OPT_SET_INT(0, "exact-match", &max_candidates
,
546 N_("only output exact matches"), 0),
547 OPT_INTEGER(0, "candidates", &max_candidates
,
548 N_("consider <n> most recent tags (default: 10)")),
549 OPT_STRING_LIST(0, "match", &patterns
, N_("pattern"),
550 N_("only consider tags matching <pattern>")),
551 OPT_STRING_LIST(0, "exclude", &exclude_patterns
, N_("pattern"),
552 N_("do not consider tags matching <pattern>")),
553 OPT_BOOL(0, "always", &always
,
554 N_("show abbreviated commit object as fallback")),
555 {OPTION_STRING
, 0, "dirty", &dirty
, N_("mark"),
556 N_("append <mark> on dirty working tree (default: \"-dirty\")"),
557 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "-dirty"},
558 {OPTION_STRING
, 0, "broken", &broken
, N_("mark"),
559 N_("append <mark> on broken working tree (default: \"-broken\")"),
560 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "-broken"},
564 git_config(git_default_config
, NULL
);
565 argc
= parse_options(argc
, argv
, prefix
, options
, describe_usage
, 0);
567 abbrev
= DEFAULT_ABBREV
;
569 if (max_candidates
< 0)
571 else if (max_candidates
> MAX_TAGS
)
572 max_candidates
= MAX_TAGS
;
574 save_commit_buffer
= 0;
576 if (longformat
&& abbrev
== 0)
577 die(_("--long is incompatible with --abbrev=0"));
580 struct string_list_item
*item
;
581 struct argv_array args
;
583 argv_array_init(&args
);
584 argv_array_pushl(&args
, "name-rev",
585 "--peel-tag", "--name-only", "--no-undefined",
588 argv_array_push(&args
, "--always");
590 argv_array_push(&args
, "--tags");
591 for_each_string_list_item(item
, &patterns
)
592 argv_array_pushf(&args
, "--refs=refs/tags/%s", item
->string
);
593 for_each_string_list_item(item
, &exclude_patterns
)
594 argv_array_pushf(&args
, "--exclude=refs/tags/%s", item
->string
);
597 argv_array_pushv(&args
, argv
);
599 argv_array_push(&args
, "HEAD");
600 return cmd_name_rev(args
.argc
, args
.argv
, prefix
);
603 hashmap_init(&names
, commit_name_neq
, NULL
, 0);
604 for_each_rawref(get_name
, NULL
);
605 if (!hashmap_get_size(&names
) && !always
)
606 die(_("No names found, cannot describe anything."));
610 struct child_process cp
= CHILD_PROCESS_INIT
;
611 argv_array_pushv(&cp
.args
, diff_index_args
);
619 switch (run_command(&cp
)) {
627 /* diff-index aborted abnormally */
631 struct lock_file index_lock
= LOCK_INIT
;
632 struct rev_info revs
;
633 struct argv_array args
= ARGV_ARRAY_INIT
;
638 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
,
640 fd
= hold_locked_index(&index_lock
, 0);
642 repo_update_index_if_able(the_repository
, &index_lock
);
644 repo_init_revisions(the_repository
, &revs
, prefix
);
645 argv_array_pushv(&args
, diff_index_args
);
646 if (setup_revisions(args
.argc
, args
.argv
, &revs
, NULL
) != 1)
647 BUG("malformed internal diff-index command line");
648 result
= run_diff_index(&revs
, 0);
650 if (!diff_result_code(&revs
.diffopt
, result
))
657 die(_("--dirty is incompatible with commit-ishes"));
659 die(_("--broken is incompatible with commit-ishes"));
662 describe(*argv
++, argc
== 0);