submodule-config: convert structures to object_id
Convert struct submodule and struct parse_config_parameter to use struct object_id. Adjust the functions which take members of these structures as arguments to also use struct object_id. Include cache.h into submodule-config.h to make struct object_id visible. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
brian m. carlson committed
May 2, 2018 at 00:25 UTC
34caab0261d87b230520f97f2d1ce9ca4474dc73
2 files changed
+37
-36
submodule-config.c
+33
-33
@@ -44,7 +44,7 @@ static int config_path_cmp(const void *unused_cmp_data,
44
const struct submodule_entry *b = entry_or_key;
45
46
return strcmp(a->config->path, b->config->path) ||
47
- hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
47
+ oidcmp(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
48
}
49
50
static int config_name_cmp(const void *unused_cmp_data,
@@ -56,7 +56,7 @@ static int config_name_cmp(const void *unused_cmp_data,
56
const struct submodule_entry *b = entry_or_key;
57
58
return strcmp(a->config->name, b->config->name) ||
59
- hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
59
+ oidcmp(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
60
}
61
62
static struct submodule_cache *submodule_cache_alloc(void)
@@ -109,17 +109,17 @@ void submodule_cache_free(struct submodule_cache *cache)
109
free(cache);
110
}
111
112
-static unsigned int hash_sha1_string(const unsigned char *sha1,
113
- const char *string)
112
+static unsigned int hash_oid_string(const struct object_id *oid,
113
+ const char *string)
114
{
115
- return memhash(sha1, 20) + strhash(string);
115
+ return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string);
116
}
117
118
static void cache_put_path(struct submodule_cache *cache,
119
struct submodule *submodule)
120
{
121
- unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
122
- submodule->path);
121
+ unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
122
+ submodule->path);
123
struct submodule_entry *e = xmalloc(sizeof(*e));
124
hashmap_entry_init(e, hash);
125
e->config = submodule;
@@ -129,8 +129,8 @@ static void cache_put_path(struct submodule_cache *cache,
129
static void cache_remove_path(struct submodule_cache *cache,
130
struct submodule *submodule)
131
{
132
- unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
133
- submodule->path);
132
+ unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
133
+ submodule->path);
134
struct submodule_entry e;
135
struct submodule_entry *removed;
136
hashmap_entry_init(&e, hash);
@@ -142,8 +142,8 @@ static void cache_remove_path(struct submodule_cache *cache,
142
static void cache_add(struct submodule_cache *cache,
143
struct submodule *submodule)
144
{
145
- unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
146
- submodule->name);
145
+ unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
146
+ submodule->name);
147
struct submodule_entry *e = xmalloc(sizeof(*e));
148
hashmap_entry_init(e, hash);
149
e->config = submodule;
@@ -151,14 +151,14 @@ static void cache_add(struct submodule_cache *cache,
151
}
152
153
static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
154
- const unsigned char *gitmodules_sha1, const char *path)
154
+ const struct object_id *gitmodules_oid, const char *path)
155
{
156
struct submodule_entry *entry;
157
- unsigned int hash = hash_sha1_string(gitmodules_sha1, path);
157
+ unsigned int hash = hash_oid_string(gitmodules_oid, path);
158
struct submodule_entry key;
159
struct submodule key_config;
160
161
- hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
161
+ oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
162
key_config.path = path;
163
164
hashmap_entry_init(&key, hash);
@@ -171,14 +171,14 @@ static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
171
}
172
173
static struct submodule *cache_lookup_name(struct submodule_cache *cache,
174
- const unsigned char *gitmodules_sha1, const char *name)
174
+ const struct object_id *gitmodules_oid, const char *name)
175
{
176
struct submodule_entry *entry;
177
- unsigned int hash = hash_sha1_string(gitmodules_sha1, name);
177
+ unsigned int hash = hash_oid_string(gitmodules_oid, name);
178
struct submodule_entry key;
179
struct submodule key_config;
180
181
- hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
181
+ oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
182
key_config.name = name;
183
184
hashmap_entry_init(&key, hash);
@@ -207,12 +207,12 @@ static int name_and_item_from_var(const char *var, struct strbuf *name,
207
}
208
209
static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
210
- const unsigned char *gitmodules_sha1, const char *name)
210
+ const struct object_id *gitmodules_oid, const char *name)
211
{
212
struct submodule *submodule;
213
struct strbuf name_buf = STRBUF_INIT;
214
215
- submodule = cache_lookup_name(cache, gitmodules_sha1, name);
215
+ submodule = cache_lookup_name(cache, gitmodules_oid, name);
216
if (submodule)
217
return submodule;
218
@@ -230,7 +230,7 @@ static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
230
submodule->branch = NULL;
231
submodule->recommend_shallow = -1;
232
233
- hashcpy(submodule->gitmodules_sha1, gitmodules_sha1);
233
+ oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
234
235
cache_add(cache, submodule);
236
@@ -341,12 +341,12 @@ int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
341
return parse_push_recurse(opt, arg, 1);
342
}
343
344
-static void warn_multiple_config(const unsigned char *treeish_name,
344
+static void warn_multiple_config(const struct object_id *treeish_name,
345
const char *name, const char *option)
346
{
347
const char *commit_string = "WORKTREE";
348
if (treeish_name)
349
- commit_string = sha1_to_hex(treeish_name);
349
+ commit_string = oid_to_hex(treeish_name);
350
warning("%s:.gitmodules, multiple configurations found for "
351
"'submodule.%s.%s'. Skipping second one!",
352
commit_string, name, option);
@@ -354,8 +354,8 @@ static void warn_multiple_config(const unsigned char *treeish_name,
354
355
struct parse_config_parameter {
356
struct submodule_cache *cache;
357
- const unsigned char *treeish_name;
358
- const unsigned char *gitmodules_sha1;
357
+ const struct object_id *treeish_name;
358
+ const struct object_id *gitmodules_oid;
359
int overwrite;
360
};
361
@@ -371,7 +371,7 @@ static int parse_config(const char *var, const char *value, void *data)
371
return 0;
372
373
submodule = lookup_or_create_by_name(me->cache,
374
- me->gitmodules_sha1,
374
+ me->gitmodules_oid,
375
name.buf);
376
377
if (!strcmp(item.buf, "path")) {
@@ -389,7 +389,7 @@ static int parse_config(const char *var, const char *value, void *data)
389
}
390
} else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
391
/* when parsing worktree configurations we can die early */
392
- int die_on_error = is_null_sha1(me->gitmodules_sha1);
392
+ int die_on_error = is_null_oid(me->gitmodules_oid);
393
if (!me->overwrite &&
394
submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
395
warn_multiple_config(me->treeish_name, submodule->name,
@@ -511,10 +511,10 @@ static const struct submodule *config_from(struct submodule_cache *cache,
511
512
switch (lookup_type) {
513
case lookup_name:
514
- submodule = cache_lookup_name(cache, oid.hash, key);
514
+ submodule = cache_lookup_name(cache, &oid, key);
515
break;
516
case lookup_path:
517
- submodule = cache_lookup_path(cache, oid.hash, key);
517
+ submodule = cache_lookup_path(cache, &oid, key);
518
break;
519
}
520
if (submodule)
@@ -526,8 +526,8 @@ static const struct submodule *config_from(struct submodule_cache *cache,
526
527
/* fill the submodule config into the cache */
528
parameter.cache = cache;
529
- parameter.treeish_name = treeish_name->hash;
530
- parameter.gitmodules_sha1 = oid.hash;
529
+ parameter.treeish_name = treeish_name;
530
+ parameter.gitmodules_oid = &oid;
531
parameter.overwrite = 0;
532
git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
533
config, config_size, ¶meter);
@@ -536,9 +536,9 @@ static const struct submodule *config_from(struct submodule_cache *cache,
536
537
switch (lookup_type) {
538
case lookup_name:
539
- return cache_lookup_name(cache, oid.hash, key);
539
+ return cache_lookup_name(cache, &oid, key);
540
case lookup_path:
541
- return cache_lookup_path(cache, oid.hash, key);
541
+ return cache_lookup_path(cache, &oid, key);
542
default:
543
return NULL;
544
}
@@ -567,7 +567,7 @@ static int gitmodules_cb(const char *var, const char *value, void *data)
567
568
parameter.cache = repo->submodule_cache;
569
parameter.treeish_name = NULL;
570
- parameter.gitmodules_sha1 = null_sha1;
570
+ parameter.gitmodules_oid = &null_oid;
571
parameter.overwrite = 1;
572
573
return parse_config(var, value, ¶meter);
submodule-config.h
+4
-3
@@ -1,6 +1,7 @@
1
#ifndef SUBMODULE_CONFIG_CACHE_H
2
#define SUBMODULE_CONFIG_CACHE_H
3
4
+#include "cache.h"
5
#include "hashmap.h"
6
#include "submodule.h"
7
#include "strbuf.h"
@@ -17,13 +18,13 @@ struct submodule {
18
const char *ignore;
19
const char *branch;
20
struct submodule_update_strategy update_strategy;
20
- /* the sha1 blob id of the responsible .gitmodules file */
21
- unsigned char gitmodules_sha1[20];
21
+ /* the object id of the responsible .gitmodules file */
22
+ struct object_id gitmodules_oid;
23
int recommend_shallow;
24
};
25
26
#define SUBMODULE_INIT { NULL, NULL, NULL, RECURSE_SUBMODULES_NONE, \
26
- NULL, NULL, SUBMODULE_UPDATE_STRATEGY_INIT, {0}, -1 };
27
+ NULL, NULL, SUBMODULE_UPDATE_STRATEGY_INIT, { { 0 } }, -1 };
28
29
struct submodule_cache;
30
struct repository;