10 static const char describe_usage
[] =
11 "git-describe [--all] [--tags] [--abbrev=<n>] <committish>*";
13 static int all
; /* Default to annotated tags only */
14 static int tags
; /* But allow any tags if --tags is specified */
16 static int abbrev
= DEFAULT_ABBREV
;
18 static int names
, allocs
;
19 static struct commit_name
{
20 struct commit
*commit
;
21 int prio
; /* annotated tag = 2, tag = 1, head = 0 */
22 char path
[FLEX_ARRAY
]; /* more */
23 } **name_array
= NULL
;
25 static struct commit_name
*match(struct commit
*cmit
)
28 struct commit_name
**p
= name_array
;
31 struct commit_name
*n
= *p
++;
32 if (n
->commit
== cmit
)
38 static void add_to_known_names(const char *path
,
39 struct commit
*commit
,
43 int len
= strlen(path
)+1;
44 struct commit_name
*name
= xmalloc(sizeof(struct commit_name
) + len
);
46 name
->commit
= commit
;
48 memcpy(name
->path
, path
, len
);
51 allocs
= (idx
+ 50) * 3 / 2;
52 name_array
= xrealloc(name_array
, allocs
*sizeof(*name_array
));
54 name_array
[idx
] = name
;
58 static int get_name(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
60 struct commit
*commit
= lookup_commit_reference_gently(sha1
, 1);
61 struct object
*object
;
66 object
= parse_object(sha1
);
67 /* If --all, then any refs are used.
68 * If --tags, then any tags are used.
69 * Otherwise only annotated tags are used.
71 if (!strncmp(path
, "refs/tags/", 10)) {
72 if (object
->type
== OBJ_TAG
)
83 if (!tags
&& prio
< 2)
86 add_to_known_names(all
? path
+ 5 : path
+ 10, commit
, prio
);
90 static int compare_names(const void *_a
, const void *_b
)
92 struct commit_name
*a
= *(struct commit_name
**)_a
;
93 struct commit_name
*b
= *(struct commit_name
**)_b
;
94 unsigned long a_date
= a
->commit
->date
;
95 unsigned long b_date
= b
->commit
->date
;
97 if (a
->prio
!= b
->prio
)
98 return b
->prio
- a
->prio
;
99 return (a_date
> b_date
) ? -1 : (a_date
== b_date
) ? 0 : 1;
102 struct possible_tag
{
103 struct possible_tag
*next
;
104 struct commit_name
*name
;
108 static void describe(const char *arg
, int last_one
)
110 unsigned char sha1
[20];
112 struct commit_list
*list
;
113 static int initialized
= 0;
114 struct commit_name
*n
;
115 struct possible_tag
*all_matches
, *min_match
, *cur_match
;
117 if (get_sha1(arg
, sha1
))
118 die("Not a valid object name %s", arg
);
119 cmit
= lookup_commit_reference(sha1
);
121 die("%s is not a valid '%s' object", arg
, commit_type
);
125 for_each_ref(get_name
, NULL
);
126 qsort(name_array
, names
, sizeof(*name_array
), compare_names
);
131 printf("%s\n", n
->path
);
138 commit_list_insert(cmit
, &list
);
140 struct commit
*c
= pop_commit(&list
);
143 struct possible_tag
*p
= xmalloc(sizeof(*p
));
152 struct commit_list
*parents
= c
->parents
;
154 struct commit
*p
= parents
->item
;
156 if (!(p
->object
.flags
& SEEN
)) {
157 p
->object
.flags
|= SEEN
;
158 insert_by_date(p
, &list
);
160 parents
= parents
->next
;
166 die("cannot describe '%s'", sha1_to_hex(cmit
->object
.sha1
));
169 for (cur_match
= all_matches
; cur_match
; cur_match
= cur_match
->next
) {
170 struct rev_info revs
;
171 struct commit
*tagged
= cur_match
->name
->commit
;
173 clear_commit_marks(cmit
, -1);
174 init_revisions(&revs
, NULL
);
175 tagged
->object
.flags
|= UNINTERESTING
;
176 add_pending_object(&revs
, &tagged
->object
, NULL
);
177 add_pending_object(&revs
, &cmit
->object
, NULL
);
179 prepare_revision_walk(&revs
);
180 cur_match
->depth
= 0;
181 while ((!min_match
|| cur_match
->depth
< min_match
->depth
)
182 && get_revision(&revs
))
184 if (!min_match
|| cur_match
->depth
< min_match
->depth
)
185 min_match
= cur_match
;
186 free_commit_list(revs
.commits
);
188 printf("%s-g%s\n", min_match
->name
->path
,
189 find_unique_abbrev(cmit
->object
.sha1
, abbrev
));
192 for (cur_match
= all_matches
; cur_match
; cur_match
= min_match
) {
193 min_match
= cur_match
->next
;
196 clear_commit_marks(cmit
, SEEN
);
200 int cmd_describe(int argc
, const char **argv
, const char *prefix
)
204 for (i
= 1; i
< argc
; i
++) {
205 const char *arg
= argv
[i
];
209 else if (!strcmp(arg
, "--all"))
211 else if (!strcmp(arg
, "--tags"))
213 else if (!strncmp(arg
, "--abbrev=", 9)) {
214 abbrev
= strtoul(arg
+ 9, NULL
, 10);
215 if (abbrev
< MINIMUM_ABBREV
|| 40 < abbrev
)
216 abbrev
= DEFAULT_ABBREV
;
219 usage(describe_usage
);
222 save_commit_buffer
= 0;
228 describe(argv
[i
], (i
== argc
- 1));