8 static const char describe_usage
[] = "git-describe [--all] <committish>*";
10 static int all
= 0; /* Default to annotated tags only */
12 static int names
= 0, allocs
= 0;
13 static struct commit_name
{
14 const struct commit
*commit
;
16 } **name_array
= NULL
;
18 static struct commit_name
*match(struct commit
*cmit
)
21 struct commit_name
**p
= name_array
;
24 struct commit_name
*n
= *p
++;
25 if (n
->commit
== cmit
)
31 static void add_to_known_names(const char *path
, const struct commit
*commit
)
34 int len
= strlen(path
)+1;
35 struct commit_name
*name
= xmalloc(sizeof(struct commit_name
) + len
);
37 name
->commit
= commit
;
38 memcpy(name
->path
, path
, len
);
41 allocs
= (idx
+ 50) * 3 / 2;
42 name_array
= xrealloc(name_array
, allocs
*sizeof(*name_array
));
44 name_array
[idx
] = name
;
48 static int get_name(const char *path
, const unsigned char *sha1
)
50 struct commit
*commit
= lookup_commit_reference_gently(sha1
, 1);
54 struct object
*object
;
55 if (strncmp(path
, "refs/tags/", 10))
57 object
= parse_object(sha1
);
58 if (object
->type
!= tag_type
)
61 add_to_known_names(all
? path
: path
+ 10, commit
);
65 static int compare_names(const void *_a
, const void *_b
)
67 struct commit_name
*a
= *(struct commit_name
**)_a
;
68 struct commit_name
*b
= *(struct commit_name
**)_b
;
69 unsigned long a_date
= a
->commit
->date
;
70 unsigned long b_date
= b
->commit
->date
;
71 return (a_date
> b_date
) ? -1 : (a_date
== b_date
) ? 0 : 1;
74 static void describe(struct commit
*cmit
)
76 struct commit_list
*list
;
77 static int initialized
= 0;
78 struct commit_name
*n
;
82 for_each_ref(get_name
);
83 qsort(name_array
, names
, sizeof(*name_array
), compare_names
);
88 printf("%s\n", n
->path
);
93 commit_list_insert(cmit
, &list
);
95 struct commit
*c
= pop_most_recent_commit(&list
, SEEN
);
98 printf("%s-g%.8s\n", n
->path
, sha1_to_hex(cmit
->object
.sha1
));
104 int main(int argc
, char **argv
)
108 for (i
= 1; i
< argc
; i
++) {
109 const char *arg
= argv
[i
];
110 unsigned char sha1
[20];
113 if (!strcmp(arg
, "--all")) {
117 if (get_sha1(arg
, sha1
) < 0)
118 usage(describe_usage
);
119 cmit
= lookup_commit_reference(sha1
);
121 usage(describe_usage
);