Skip to content

Commit a8b1ba8

Browse files
10ne1gitster
authored andcommitted
hook: replace hook_list_clear() -> string_list_clear_func()
Replace the custom function with string_list_clear_func() which is a more common pattern for clearing a string_list. To be able to do this, rework hook_clear() into hook_free(), so it can be passed to string_list_clear_func(). A slight complication is the need to keep a copy of the internal cb data free() pointer, however I think it's worth it since the API becomes cleaner, e.g. no more calls with NULL function args like hook_list_clear(hooks, NULL). In other words, the callers don't need to keep track of hook internal state to determine when cleanup is necessary or not (pass NULL) because each `struct hook` now owns its data_free callback. Suggested-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 4d10f4a commit a8b1ba8

3 files changed

Lines changed: 37 additions & 25 deletions

File tree

builtin/hook.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static int list(int argc, const char **argv, const char *prefix,
7878
}
7979

8080
cleanup:
81-
hook_list_clear(head, NULL);
81+
string_list_clear_func(head, hook_free);
8282
free(head);
8383
return ret;
8484
}

hook.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ const char *find_hook(struct repository *r, const char *name)
5252
return path.buf;
5353
}
5454

55-
static void hook_clear(struct hook *h, hook_data_free_fn cb_data_free)
55+
void hook_free(void *p, const char *str UNUSED)
5656
{
57+
struct hook *h = p;
58+
5759
if (!h)
5860
return;
5961

@@ -64,22 +66,12 @@ static void hook_clear(struct hook *h, hook_data_free_fn cb_data_free)
6466
free((void *)h->u.configured.command);
6567
}
6668

67-
if (cb_data_free)
68-
cb_data_free(h->feed_pipe_cb_data);
69+
if (h->data_free && h->feed_pipe_cb_data)
70+
h->data_free(h->feed_pipe_cb_data);
6971

7072
free(h);
7173
}
7274

73-
void hook_list_clear(struct string_list *hooks, hook_data_free_fn cb_data_free)
74-
{
75-
struct string_list_item *item;
76-
77-
for_each_string_list_item(item, hooks)
78-
hook_clear(item->util, cb_data_free);
79-
80-
string_list_clear(hooks, 0);
81-
}
82-
8375
/* Helper to detect and add default "traditional" hooks from the hookdir. */
8476
static void list_hooks_add_default(struct repository *r, const char *hookname,
8577
struct string_list *hook_list,
@@ -100,9 +92,15 @@ static void list_hooks_add_default(struct repository *r, const char *hookname,
10092
if (options && options->dir)
10193
hook_path = absolute_path(hook_path);
10294

103-
/* Setup per-hook internal state cb data */
104-
if (options && options->feed_pipe_cb_data_alloc)
95+
/*
96+
* Setup per-hook internal state callback data.
97+
* When provided, the alloc/free callbacks are always provided
98+
* together, so use them to alloc/free the internal hook state.
99+
*/
100+
if (options && options->feed_pipe_cb_data_alloc) {
105101
h->feed_pipe_cb_data = options->feed_pipe_cb_data_alloc(options->feed_pipe_ctx);
102+
h->data_free = options->feed_pipe_cb_data_free;
103+
}
106104

107105
h->kind = HOOK_TRADITIONAL;
108106
h->u.traditional.path = xstrdup(hook_path);
@@ -316,10 +314,16 @@ static void list_hooks_add_configured(struct repository *r,
316314

317315
CALLOC_ARRAY(hook, 1);
318316

319-
if (options && options->feed_pipe_cb_data_alloc)
317+
/*
318+
* When provided, the alloc/free callbacks are always provided
319+
* together, so use them to alloc/free the internal hook state.
320+
*/
321+
if (options && options->feed_pipe_cb_data_alloc) {
320322
hook->feed_pipe_cb_data =
321323
options->feed_pipe_cb_data_alloc(
322324
options->feed_pipe_ctx);
325+
hook->data_free = options->feed_pipe_cb_data_free;
326+
}
323327

324328
hook->kind = HOOK_CONFIGURED;
325329
hook->u.configured.friendly_name = xstrdup(friendly_name);
@@ -362,7 +366,7 @@ int hook_exists(struct repository *r, const char *name)
362366
{
363367
struct string_list *hooks = list_hooks(r, name, NULL);
364368
int exists = hooks->nr > 0;
365-
hook_list_clear(hooks, NULL);
369+
string_list_clear_func(hooks, hook_free);
366370
free(hooks);
367371
return exists;
368372
}
@@ -516,7 +520,7 @@ int run_hooks_opt(struct repository *r, const char *hook_name,
516520
run_processes_parallel(&opts);
517521
ret = cb_data.rc;
518522
cleanup:
519-
hook_list_clear(cb_data.hook_command_list, options->feed_pipe_cb_data_free);
523+
string_list_clear_func(cb_data.hook_command_list, hook_free);
520524
free(cb_data.hook_command_list);
521525
run_hooks_opt_clear(options);
522526
return ret;

hook.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
struct repository;
99

10+
typedef void (*hook_data_free_fn)(void *data);
11+
typedef void *(*hook_data_alloc_fn)(void *init_ctx);
12+
1013
/**
1114
* Represents a hook command to be run.
1215
* Hooks can be:
@@ -41,10 +44,15 @@ struct hook {
4144
* Only useful when using `run_hooks_opt.feed_pipe`, otherwise ignore it.
4245
*/
4346
void *feed_pipe_cb_data;
44-
};
4547

46-
typedef void (*hook_data_free_fn)(void *data);
47-
typedef void *(*hook_data_alloc_fn)(void *init_ctx);
48+
/**
49+
* Callback to free `feed_pipe_cb_data`.
50+
*
51+
* It is called automatically and points to the `feed_pipe_cb_data_free`
52+
* provided via the `run_hook_opt` parameter.
53+
*/
54+
hook_data_free_fn data_free;
55+
};
4856

4957
struct run_hooks_opt {
5058
/* Environment vars to be set for each hook */
@@ -185,10 +193,10 @@ struct string_list *list_hooks(struct repository *r, const char *hookname,
185193
struct run_hooks_opt *options);
186194

187195
/**
188-
* Frees the memory allocated for the hook list, including the `struct hook`
189-
* items and their internal state.
196+
* Frees a struct hook stored as the util pointer of a string_list_item.
197+
* Suitable for use as a string_list_clear_func_t callback.
190198
*/
191-
void hook_list_clear(struct string_list *hooks, hook_data_free_fn cb_data_free);
199+
void hook_free(void *p, const char *str);
192200

193201
/**
194202
* Frees the hook configuration cache stored in `struct repository`.

0 commit comments

Comments
 (0)