Skip to content

Commit d8513bc

Browse files
10ne1gitster
authored andcommitted
hook: introduce hook_config_cache_entry for per-hook data
Replace the bare `char *command` util pointer stored in each string_list item with a heap-allocated `struct hook_config_cache_entry` that carries that command string. This is just a refactoring with no behavior changes, to give the cache entry room to grow, so it can carry the additional hook metadata we'll be adding in the following commits. Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent e0fceec commit d8513bc

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

hook.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ static void list_hooks_add_default(struct repository *r, const char *hookname,
108108
string_list_append(hook_list, hook_path)->util = h;
109109
}
110110

111+
/*
112+
* Cache entry stored as the .util pointer of string_list items inside the
113+
* hook config cache. For now carries only the command for the hook. Next
114+
* commits will add more data.
115+
*/
116+
struct hook_config_cache_entry {
117+
char *command;
118+
};
119+
111120
/*
112121
* Callback struct to collect all hook.* keys in a single config pass.
113122
* commands: friendly-name to command map.
@@ -202,7 +211,12 @@ void hook_cache_clear(struct strmap *cache)
202211

203212
strmap_for_each_entry(cache, &iter, e) {
204213
struct string_list *hooks = e->value;
205-
string_list_clear(hooks, 1); /* free util (command) pointers */
214+
for (size_t i = 0; i < hooks->nr; i++) {
215+
struct hook_config_cache_entry *entry = hooks->items[i].util;
216+
free(entry->command);
217+
free(entry);
218+
}
219+
string_list_clear(hooks, 0);
206220
free(hooks);
207221
}
208222
strmap_clear(cache, 0);
@@ -232,6 +246,7 @@ static void build_hook_config_map(struct repository *r, struct strmap *cache)
232246

233247
for (size_t i = 0; i < hook_names->nr; i++) {
234248
const char *hname = hook_names->items[i].string;
249+
struct hook_config_cache_entry *entry;
235250
char *command;
236251

237252
/* filter out disabled hooks */
@@ -245,9 +260,10 @@ static void build_hook_config_map(struct repository *r, struct strmap *cache)
245260
"'hook.%s.event' must be removed;"
246261
" aborting."), hname, hname);
247262

248-
/* util stores the command; owned by the cache. */
249-
string_list_append(hooks, hname)->util =
250-
xstrdup(command);
263+
/* util stores a cache entry; owned by the cache. */
264+
CALLOC_ARRAY(entry, 1);
265+
entry->command = xstrdup(command);
266+
string_list_append(hooks, hname)->util = entry;
251267
}
252268

253269
strmap_put(cache, e->key, hooks);
@@ -309,7 +325,7 @@ static void list_hooks_add_configured(struct repository *r,
309325
/* Iterate through configured hooks and initialize internal states */
310326
for (size_t i = 0; configured_hooks && i < configured_hooks->nr; i++) {
311327
const char *friendly_name = configured_hooks->items[i].string;
312-
const char *command = configured_hooks->items[i].util;
328+
struct hook_config_cache_entry *entry = configured_hooks->items[i].util;
313329
struct hook *hook;
314330

315331
CALLOC_ARRAY(hook, 1);
@@ -327,7 +343,7 @@ static void list_hooks_add_configured(struct repository *r,
327343

328344
hook->kind = HOOK_CONFIGURED;
329345
hook->u.configured.friendly_name = xstrdup(friendly_name);
330-
hook->u.configured.command = xstrdup(command);
346+
hook->u.configured.command = xstrdup(entry->command);
331347

332348
string_list_append(list, friendly_name)->util = hook;
333349
}

0 commit comments

Comments
 (0)