sha1-file: provide functions to look up hash algorithms
There are several ways we might refer to a hash algorithm: by name, such as in the config file; by format ID, such as in a pack; or internally, by a pointer to the hash_algos array. Provide functions to look up hash algorithms based on these various forms and return the internal constant used for them. If conversion to another form is necessary, this internal constant can be used to look up the proper data in the hash_algos array. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
brian m. carlson committed
Oct 22, 2018 at 02:43 UTC
2f90b9d9b4ba1f8e79318853301e8ef19ca02681
2 files changed
+34
hash.h
+13
@@ -98,4 +98,17 @@ struct git_hash_algo {
98
};
99
extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS];
100
101
+/*
102
+ * Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if
103
+ * the name doesn't match a known algorithm.
104
+ */
105
+int hash_algo_by_name(const char *name);
106
+/* Identical, except based on the format ID. */
107
+int hash_algo_by_id(uint32_t format_id);
108
+/* Identical, except for a pointer to struct git_hash_algo. */
109
+static inline int hash_algo_by_ptr(const struct git_hash_algo *p)
110
+{
111
+ return p - hash_algos;
112
+}
113
+
114
#endif
sha1-file.c
+21
@@ -122,6 +122,27 @@ const char *empty_blob_oid_hex(void)
122
return oid_to_hex_r(buf, the_hash_algo->empty_blob);
123
}
124
125
+int hash_algo_by_name(const char *name)
126
+{
127
+ int i;
128
+ if (!name)
129
+ return GIT_HASH_UNKNOWN;
130
+ for (i = 1; i < GIT_HASH_NALGOS; i++)
131
+ if (!strcmp(name, hash_algos[i].name))
132
+ return i;
133
+ return GIT_HASH_UNKNOWN;
134
+}
135
+
136
+int hash_algo_by_id(uint32_t format_id)
137
+{
138
+ int i;
139
+ for (i = 1; i < GIT_HASH_NALGOS; i++)
140
+ if (format_id == hash_algos[i].format_id)
141
+ return i;
142
+ return GIT_HASH_UNKNOWN;
143
+}
144
+
145
+
146
/*
147
* This is meant to hold a *small* number of objects that you would
148
* want read_sha1_file() to be able to return, but yet you do not want