Skip to content

Commit 29d8a83

Browse files
kbleesgitster
authored andcommitted
buitin/describe.c: use new hash map implementation
Signed-off-by: Karsten Blees <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6a364ce commit 29d8a83

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

builtin/describe.c

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "exec_cmd.h"
77
#include "parse-options.h"
88
#include "diff.h"
9-
#include "hash.h"
9+
#include "hashmap.h"
1010
#include "argv-array.h"
1111

1212
#define SEEN (1u<<0)
@@ -25,7 +25,7 @@ static int longformat;
2525
static int first_parent;
2626
static int abbrev = -1; /* unspecified */
2727
static int max_candidates = 10;
28-
static struct hash_table names;
28+
static struct hashmap names;
2929
static int have_util;
3030
static const char *pattern;
3131
static int always;
@@ -38,7 +38,7 @@ static const char *diff_index_args[] = {
3838

3939

4040
struct commit_name {
41-
struct commit_name *next;
41+
struct hashmap_entry entry;
4242
unsigned char peeled[20];
4343
struct tag *tag;
4444
unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */
@@ -50,6 +50,12 @@ static const char *prio_names[] = {
5050
"head", "lightweight", "annotated",
5151
};
5252

53+
static int commit_name_cmp(const struct commit_name *cn1,
54+
const struct commit_name *cn2, const void *peeled)
55+
{
56+
return hashcmp(cn1->peeled, peeled ? peeled : cn2->peeled);
57+
}
58+
5359
static inline unsigned int hash_sha1(const unsigned char *sha1)
5460
{
5561
unsigned int hash;
@@ -59,21 +65,9 @@ static inline unsigned int hash_sha1(const unsigned char *sha1)
5965

6066
static inline struct commit_name *find_commit_name(const unsigned char *peeled)
6167
{
62-
struct commit_name *n = lookup_hash(hash_sha1(peeled), &names);
63-
while (n && !!hashcmp(peeled, n->peeled))
64-
n = n->next;
65-
return n;
66-
}
67-
68-
static int set_util(void *chain, void *data)
69-
{
70-
struct commit_name *n;
71-
for (n = chain; n; n = n->next) {
72-
struct commit *c = lookup_commit_reference_gently(n->peeled, 1);
73-
if (c)
74-
c->util = n;
75-
}
76-
return 0;
68+
struct commit_name key;
69+
hashmap_entry_init(&key, hash_sha1(peeled));
70+
return hashmap_get(&names, &key, peeled);
7771
}
7872

7973
static int replace_name(struct commit_name *e,
@@ -118,16 +112,10 @@ static void add_to_known_names(const char *path,
118112
struct tag *tag = NULL;
119113
if (replace_name(e, prio, sha1, &tag)) {
120114
if (!e) {
121-
void **pos;
122115
e = xmalloc(sizeof(struct commit_name));
123116
hashcpy(e->peeled, peeled);
124-
pos = insert_hash(hash_sha1(peeled), e, &names);
125-
if (pos) {
126-
e->next = *pos;
127-
*pos = e;
128-
} else {
129-
e->next = NULL;
130-
}
117+
hashmap_entry_init(e, hash_sha1(peeled));
118+
hashmap_add(&names, e);
131119
e->path = NULL;
132120
}
133121
e->tag = tag;
@@ -292,7 +280,14 @@ static void describe(const char *arg, int last_one)
292280
fprintf(stderr, _("searching to describe %s\n"), arg);
293281

294282
if (!have_util) {
295-
for_each_hash(&names, set_util, NULL);
283+
struct hashmap_iter iter;
284+
struct commit *c;
285+
struct commit_name *n = hashmap_iter_first(&names, &iter);
286+
for (; n; n = hashmap_iter_next(&iter)) {
287+
c = lookup_commit_reference_gently(n->peeled, 1);
288+
if (c)
289+
c->util = n;
290+
}
296291
have_util = 1;
297292
}
298293

@@ -463,9 +458,9 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
463458
return cmd_name_rev(args.argc, args.argv, prefix);
464459
}
465460

466-
init_hash(&names);
461+
hashmap_init(&names, (hashmap_cmp_fn) commit_name_cmp, 0);
467462
for_each_rawref(get_name, NULL);
468-
if (!names.nr && !always)
463+
if (!names.size && !always)
469464
die(_("No names found, cannot describe anything."));
470465

471466
if (argc == 0) {

0 commit comments

Comments
 (0)