git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[RFC/PATCH 1/2] ref-dict: Add a set of functions for working with a ref dictionary

From
Julian Phillips <julian@quantumfyre.co.uk>
Date
Sep 16, 2009, 07:53 UTC
Message-ID
<20090916075304.58044.664.julian@quantumfyre.co.uk>
In-Reply-To
<20090916074737.58044.42776.julian@quantumfyre.co.uk>

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@quantumfyre.co.uk>
---
 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
Previous: Julian PhillipsNext: Julian Phillips
Message 2 of 17 in “Speed up fetch with large number of tags”
  1. 0/2 Speed up fetch with large number of tagsJulian Phillips, Sep 16, 2009
  2. 1/2 ref-dict: Add a set of functions for working with a ref dictionaryJulian Phillips, Sep 16, 2009
  3. 2/2 fetch: Speed up fetch by using ref dictionaryJulian Phillips, Sep 16, 2009
  4. Junio C HamanoSep 16, 2009
  5. Julian PhillipsSep 16, 2009
  6. Shawn O. PearceSep 16, 2009
  7. Junio C HamanoSep 16, 2009
  8. Shawn O. PearceSep 16, 2009
  9. Junio C HamanoSep 16, 2009
  10. fetch: Speed up fetch by rewriting find_non_local_tagsJulian Phillips, Sep 16, 2009
  11. Junio C HamanoSep 16, 2009
  12. Julian PhillipsSep 16, 2009
  13. Julian PhillipsSep 17, 2009
  14. Johan HerlandSep 17, 2009
  15. fetch: Speed up fetch by rewriting find_non_local_tagsJulian Phillips, Sep 17, 2009
  16. Shawn O. PearceSep 16, 2009
  17. Junio C HamanoSep 22, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.