buitin/describe.c: use new hash map implementation
Signed-off-by: Karsten Blees <blees@dcon.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Karsten Blees committed
Nov 14, 2013 at 20:18 UTC
29d8a834b5fcb1de057ed8563339b104e0251717
1 file changed
+24
-29
builtin/describe.c
+24
-29
@@ -6,7 +6,7 @@
6
#include "exec_cmd.h"
7
#include "parse-options.h"
8
#include "diff.h"
9
-#include "hash.h"
9
+#include "hashmap.h"
10
#include "argv-array.h"
11
12
#define SEEN (1u<<0)
@@ -25,7 +25,7 @@ static int longformat;
25
static int first_parent;
26
static int abbrev = -1; /* unspecified */
27
static int max_candidates = 10;
28
-static struct hash_table names;
28
+static struct hashmap names;
29
static int have_util;
30
static const char *pattern;
31
static int always;
@@ -38,7 +38,7 @@ static const char *diff_index_args[] = {
38
39
40
struct commit_name {
41
- struct commit_name *next;
41
+ struct hashmap_entry entry;
42
unsigned char peeled[20];
43
struct tag *tag;
44
unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */
@@ -50,6 +50,12 @@ static const char *prio_names[] = {
50
"head", "lightweight", "annotated",
51
};
52
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
+
59
static inline unsigned int hash_sha1(const unsigned char *sha1)
60
{
61
unsigned int hash;
@@ -59,21 +65,9 @@ static inline unsigned int hash_sha1(const unsigned char *sha1)
65
66
static inline struct commit_name *find_commit_name(const unsigned char *peeled)
67
{
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);
71
}
72
73
static int replace_name(struct commit_name *e,
@@ -118,16 +112,10 @@ static void add_to_known_names(const char *path,
112
struct tag *tag = NULL;
113
if (replace_name(e, prio, sha1, &tag)) {
114
if (!e) {
121
- void **pos;
115
e = xmalloc(sizeof(struct commit_name));
116
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);
119
e->path = NULL;
120
}
121
e->tag = tag;
@@ -292,7 +280,14 @@ static void describe(const char *arg, int last_one)
280
fprintf(stderr, _("searching to describe %s\n"), arg);
281
282
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
+ }
291
have_util = 1;
292
}
293
@@ -463,9 +458,9 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
458
return cmd_name_rev(args.argc, args.argv, prefix);
459
}
460
466
- init_hash(&names);
461
+ hashmap_init(&names, (hashmap_cmp_fn) commit_name_cmp, 0);
462
for_each_rawref(get_name, NULL);
468
- if (!names.nr && !always)
463
+ if (!names.size && !always)
464
die(_("No names found, cannot describe anything."));
465
466
if (argc == 0) {