These are a set of helper functions that use the generic hash table functions to provide a quick and easy mapping from ref name to sha1. Signed-off-by: Julian Phillips <julian@xxxxxxxxxxxxxxxxx> --- Makefile | 1 + ref-dict.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ref-dict.h | 13 ++++++++++ 3 files changed, 90 insertions(+), 0 deletions(-) create mode 100644 ref-dict.c create mode 100644 ref-dict.h diff --git a/Makefile b/Makefile index d4958b8..815e4c9 100644 --- a/Makefile +++ b/Makefile @@ -531,6 +531,7 @@ LIB_OBJS += reachable.o LIB_OBJS += read-cache.o LIB_OBJS += reflog-walk.o LIB_OBJS += refs.o +LIB_OBJS += ref-dict.o LIB_OBJS += remote.o LIB_OBJS += replace_object.o LIB_OBJS += rerere.o diff --git a/ref-dict.c b/ref-dict.c new file mode 100644 index 0000000..b9cab4b --- /dev/null +++ b/ref-dict.c @@ -0,0 +1,76 @@ +/* + * ref-dict.c + * + * A Hash-based dictionary for storing name-sha1 data for refs. + * + * Copyright (C) 2009 Julian Phillips + */ + +#include "cache.h" +#include "hash.h" +#include "remote.h" +#include "ref-dict.h" + +/* hash_name based on the function of the same name in name-hash.c */ +static unsigned int hash_name(const char *name) +{ + unsigned int hash = 0x123; + int namelen = strlen(name); + + do { + unsigned char c = *name++; + hash = hash*101 + c; + } while (--namelen); + return hash; +} + +/* + * A convienience function for creating a ref_dict from a ref_list. + */ +void ref_dict_create(struct hash_table *dict, const struct ref *ref_list) +{ + struct ref *ref; + + init_hash(dict); + + for (ref = (struct ref *)ref_list; ref; ref = ref->next) { + ref_dict_add(dict, ref->name, ref->old_sha1); + } +} + +/* + * Add an entry to the ref_dict, recording that name maps to sha1. + */ +void ref_dict_add(struct hash_table *dict, const char *name, + const unsigned char *sha1) +{ + struct ref **ref; + struct ref *new_ref = alloc_ref(name); + + hashcpy(new_ref->old_sha1, sha1); + + ref = (struct ref **)insert_hash(hash_name(name), new_ref, dict); + if (ref) { + new_ref->next = *ref; + *ref = new_ref; + } +} + +/* + * Find the sha1 for the given name. Returns 1 if found and copies the sha1 + * into the space pointed to by sha1, returns 0 otherwise and sha1 is untouched. + */ +int ref_dict_get(const struct hash_table *dict, const char *name, + unsigned char *sha1) +{ + struct ref *ref = lookup_hash(hash_name(name), dict); + + for (; ref; ref = ref->next) { + if (!strcmp(name, ref->name)) { + hashcpy(sha1, ref->old_sha1); + return 1; + } + } + + return 0; +} diff --git a/ref-dict.h b/ref-dict.h new file mode 100644 index 0000000..ca1e9a7 --- /dev/null +++ b/ref-dict.h @@ -0,0 +1,13 @@ +#ifndef REF_DICT_H +#define REF_DICT_H + +#include "cache.h" +#include "hash.h" + +void ref_dict_create(struct hash_table *dict, const struct ref *ref_list); +void ref_dict_add(struct hash_table *dict, const char *name, + const unsigned char *sha1); +int ref_dict_get(const struct hash_table *dict, const char *name, + unsigned char *sha1); + +#endif /* REF_DICT_H */ -- 1.6.4.2 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html