7 static const char describe_usage
[] = "git-describe [--all] <committish>*";
9 static int all
= 0; /* Default to tags only */
11 static int names
= 0, allocs
= 0;
12 static struct commit_name
{
13 const struct commit
*commit
;
15 } **name_array
= NULL
;
17 static struct commit_name
*match(struct commit
*cmit
)
20 struct commit_name
**p
= name_array
;
23 struct commit_name
*n
= *p
++;
24 if (n
->commit
== cmit
)
30 static void add_to_known_names(const char *path
, const struct commit
*commit
)
33 int len
= strlen(path
)+1;
34 struct commit_name
*name
= xmalloc(sizeof(struct commit_name
) + len
);
36 name
->commit
= commit
;
37 memcpy(name
->path
, path
, len
);
40 allocs
= (idx
+ 50) * 3 / 2;
41 name_array
= xrealloc(name_array
, allocs
*sizeof(*name_array
));
43 name_array
[idx
] = name
;
47 static int get_name(const char *path
, const unsigned char *sha1
)
49 struct commit
*commit
= lookup_commit_reference_gently(sha1
, 1);
52 if (!all
&& strncmp(path
, "refs/tags/", 10))
54 add_to_known_names(path
, commit
);
58 static int compare_names(const void *_a
, const void *_b
)
60 struct commit_name
*a
= *(struct commit_name
**)_a
;
61 struct commit_name
*b
= *(struct commit_name
**)_b
;
62 unsigned long a_date
= a
->commit
->date
;
63 unsigned long b_date
= b
->commit
->date
;
64 return (a_date
> b_date
) ? -1 : (a_date
== b_date
) ? 0 : 1;
67 static void describe(struct commit
*cmit
)
69 struct commit_list
*list
;
70 static int initialized
= 0;
71 struct commit_name
*n
;
75 for_each_ref(get_name
);
76 qsort(name_array
, names
, sizeof(*name_array
), compare_names
);
81 printf("%s\n", n
->path
);
86 commit_list_insert(cmit
, &list
);
88 struct commit
*c
= pop_most_recent_commit(&list
, SEEN
);
91 printf("%s-g%.8s\n", n
->path
, sha1_to_hex(cmit
->object
.sha1
));
97 int main(int argc
, char **argv
)
101 for (i
= 1; i
< argc
; i
++) {
102 const char *arg
= argv
[i
];
103 unsigned char sha1
[20];
106 if (!strcmp(arg
, "--all")) {
110 if (get_sha1(arg
, sha1
) < 0)
111 usage(describe_usage
);
112 cmit
= lookup_commit_reference(sha1
);
114 usage(describe_usage
);