hook: fix minor style issues

Fix some minor style nits pointed out by Patrick, Junio and Eric: * Use CALLOC_ARRAY instead of xcalloc. * Init struct members during declaration. * Simplify if condition boolean logic. * Missing curly braces in if/else stmts. * Unnecessary header includes. * Capitalization and full-stop in error/warn messages. * Curly brace on separate line when defining struct. * Comment spelling: free'd -> freed. * Sort the included headers. * Blank line fixes to improve readability. These contain no logic changes, the code behaves the same as before. Suggested-by: Eric Sunshine <sunshine@sunshineco.com> Suggested-by: Junio C Hamano <gitster@pobox.com> 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>

Adrian Ratiu committed Mar 25, 2026 at 21:54 UTC b06770e5d8948c7cad76d7507423376eacf1e005
7 files changed +61 -57
builtin/hook.c
+2 -4
@@ -5,8 +5,6 @@
5 #include "gettext.h"
6 #include "hook.h"
7 #include "parse-options.h"
8 -#include "strvec.h"
9 -#include "abspath.h"
8
9 #define BUILTIN_HOOK_RUN_USAGE \
10 N_("git hook run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]")
@@ -51,7 +49,7 @@ static int list(int argc, const char **argv, const char *prefix,
49 * arguments later they probably should be caught by parse_options.
50 */
51 if (argc != 1)
54 - usage_msg_opt(_("You must specify a hook event name to list."),
52 + usage_msg_opt(_("you must specify a hook event name to list"),
53 builtin_hook_list_usage, list_options);
54
55 hookname = argv[0];
@@ -59,7 +57,7 @@ static int list(int argc, const char **argv, const char *prefix,
57 head = list_hooks(repo, hookname, NULL);
58
59 if (!head->nr) {
62 - warning(_("No hooks found for event '%s'"), hookname);
60 + warning(_("no hooks found for event '%s'"), hookname);
61 ret = 1; /* no hooks found */
62 goto cleanup;
63 }
builtin/receive-pack.c
+34 -31
@@ -3,46 +3,45 @@
3
4 #include "builtin.h"
5 #include "abspath.h"
6 -
6 +#include "commit.h"
7 +#include "commit-reach.h"
8 #include "config.h"
9 +#include "connect.h"
10 +#include "connected.h"
11 #include "environment.h"
12 +#include "exec-cmd.h"
13 +#include "fsck.h"
14 #include "gettext.h"
15 +#include "gpg-interface.h"
16 #include "hex.h"
11 -#include "lockfile.h"
12 -#include "pack.h"
13 -#include "refs.h"
14 -#include "pkt-line.h"
15 -#include "sideband.h"
16 -#include "run-command.h"
17 #include "hook.h"
18 -#include "exec-cmd.h"
19 -#include "commit.h"
18 +#include "lockfile.h"
19 #include "object.h"
21 -#include "remote.h"
22 -#include "connect.h"
23 -#include "string-list.h"
24 -#include "oid-array.h"
25 -#include "connected.h"
26 -#include "strvec.h"
27 -#include "version.h"
28 -#include "gpg-interface.h"
29 -#include "sigchain.h"
30 -#include "fsck.h"
31 -#include "tmp-objdir.h"
32 -#include "oidset.h"
33 -#include "packfile.h"
20 #include "object-file.h"
21 #include "object-name.h"
22 #include "odb.h"
23 +#include "oid-array.h"
24 +#include "oidset.h"
25 +#include "pack.h"
26 +#include "packfile.h"
27 +#include "parse-options.h"
28 +#include "pkt-line.h"
29 #include "protocol.h"
38 -#include "commit-reach.h"
30 +#include "refs.h"
31 +#include "remote.h"
32 +#include "run-command.h"
33 #include "server-info.h"
34 +#include "setup.h"
35 +#include "shallow.h"
36 +#include "sideband.h"
37 +#include "sigchain.h"
38 +#include "string-list.h"
39 +#include "strvec.h"
40 +#include "tmp-objdir.h"
41 #include "trace.h"
42 #include "trace2.h"
43 +#include "version.h"
44 #include "worktree.h"
43 -#include "shallow.h"
44 -#include "setup.h"
45 -#include "parse-options.h"
45
46 static const char * const receive_pack_usage[] = {
47 N_("git receive-pack <git-dir>"),
@@ -904,11 +903,14 @@ static int feed_receive_hook_cb(int hook_stdin_fd, void *pp_cb UNUSED, void *pp_
903 static void *receive_hook_feed_state_alloc(void *feed_pipe_ctx)
904 {
905 struct receive_hook_feed_state *init_state = feed_pipe_ctx;
907 - struct receive_hook_feed_state *data = xcalloc(1, sizeof(*data));
906 + struct receive_hook_feed_state *data;
907 +
908 + CALLOC_ARRAY(data, 1);
909 data->report = init_state->report;
910 data->cmd = init_state->cmd;
911 data->skip_broken = init_state->skip_broken;
912 strbuf_init(&data->buf, 0);
913 +
914 return data;
915 }
916
@@ -928,7 +930,11 @@ static int run_receive_hook(struct command *commands,
930 {
931 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
932 struct command *iter = commands;
931 - struct receive_hook_feed_state feed_init_state = { 0 };
933 + struct receive_hook_feed_state feed_init_state = {
934 + .cmd = commands,
935 + .skip_broken = skip_broken,
936 + .buf = STRBUF_INIT,
937 + };
938 struct async sideband_async;
939 int sideband_async_started = 0;
940 int saved_stderr = -1;
@@ -961,9 +967,6 @@ static int run_receive_hook(struct command *commands,
967 prepare_sideband_async(&sideband_async, &saved_stderr, &sideband_async_started);
968
969 /* set up stdin callback */
964 - feed_init_state.cmd = commands;
965 - feed_init_state.skip_broken = skip_broken;
966 - strbuf_init(&feed_init_state.buf, 0);
970 opt.feed_pipe_ctx = &feed_init_state;
971 opt.feed_pipe = feed_receive_hook_cb;
972 opt.feed_pipe_cb_data_alloc = receive_hook_feed_state_alloc;
hook.c
+18 -16
@@ -1,16 +1,16 @@
1 #include "git-compat-util.h"
2 #include "abspath.h"
3 #include "advice.h"
4 +#include "config.h"
5 +#include "environment.h"
6 #include "gettext.h"
7 #include "hook.h"
6 -#include "path.h"
8 #include "parse.h"
9 +#include "path.h"
10 #include "run-command.h"
9 -#include "config.h"
11 +#include "setup.h"
12 #include "strbuf.h"
13 #include "strmap.h"
12 -#include "environment.h"
13 -#include "setup.h"
14
15 const char *find_hook(struct repository *r, const char *name)
16 {
@@ -57,9 +57,9 @@ static void hook_clear(struct hook *h, cb_data_free_fn cb_data_free)
57 if (!h)
58 return;
59
60 - if (h->kind == HOOK_TRADITIONAL)
60 + if (h->kind == HOOK_TRADITIONAL) {
61 free((void *)h->u.traditional.path);
62 - else if (h->kind == HOOK_CONFIGURED) {
62 + } else if (h->kind == HOOK_CONFIGURED) {
63 free((void *)h->u.configured.friendly_name);
64 free((void *)h->u.configured.command);
65 }
@@ -91,7 +91,7 @@ static void list_hooks_add_default(struct repository *r, const char *hookname,
91 if (!hook_path)
92 return;
93
94 - h = xcalloc(1, sizeof(struct hook));
94 + CALLOC_ARRAY(h, 1);
95
96 /*
97 * If the hook is to run in a specific dir, a relative path can
@@ -154,7 +154,7 @@ static int hook_config_lookup_all(const char *key, const char *value,
154 strmap_get(&data->event_hooks, value);
155
156 if (!hooks) {
157 - hooks = xcalloc(1, sizeof(*hooks));
157 + CALLOC_ARRAY(hooks, 1);
158 string_list_init_dup(hooks);
159 strmap_put(&data->event_hooks, value, hooks);
160 }
@@ -227,8 +227,9 @@ static void build_hook_config_map(struct repository *r, struct strmap *cache)
227 /* Construct the cache from parsed configs. */
228 strmap_for_each_entry(&cb_data.event_hooks, &iter, e) {
229 struct string_list *hook_names = e->value;
230 - struct string_list *hooks = xcalloc(1, sizeof(*hooks));
230 + struct string_list *hooks;
231
232 + CALLOC_ARRAY(hooks, 1);
233 string_list_init_dup(hooks);
234
235 for (size_t i = 0; i < hook_names->nr; i++) {
@@ -281,7 +282,7 @@ static struct strmap *get_hook_config_cache(struct repository *r)
282 * it just once on the first call.
283 */
284 if (!r->hook_config_cache) {
284 - r->hook_config_cache = xcalloc(1, sizeof(*cache));
285 + CALLOC_ARRAY(r->hook_config_cache, 1);
286 strmap_init(r->hook_config_cache);
287 build_hook_config_map(r, r->hook_config_cache);
288 }
@@ -289,9 +290,9 @@ static struct strmap *get_hook_config_cache(struct repository *r)
290 } else {
291 /*
292 * Out-of-repo calls (no gitdir) allocate and return a temporary
292 - * map cache which gets free'd immediately by the caller.
293 + * cache which gets freed immediately by the caller.
294 */
294 - cache = xcalloc(1, sizeof(*cache));
295 + CALLOC_ARRAY(cache, 1);
296 strmap_init(cache);
297 build_hook_config_map(r, cache);
298 }
@@ -311,7 +312,9 @@ static void list_hooks_add_configured(struct repository *r,
312 for (size_t i = 0; configured_hooks && i < configured_hooks->nr; i++) {
313 const char *friendly_name = configured_hooks->items[i].string;
314 const char *command = configured_hooks->items[i].util;
314 - struct hook *hook = xcalloc(1, sizeof(struct hook));
315 + struct hook *hook;
316 +
317 + CALLOC_ARRAY(hook, 1);
318
319 if (options && options->feed_pipe_cb_data_alloc)
320 hook->feed_pipe_cb_data =
@@ -343,7 +346,7 @@ struct string_list *list_hooks(struct repository *r, const char *hookname,
346 if (!hookname)
347 BUG("null hookname was provided to hook_list()!");
348
346 - hook_head = xmalloc(sizeof(struct string_list));
349 + CALLOC_ARRAY(hook_head, 1);
350 string_list_init_dup(hook_head);
351
352 /* Add hooks from the config, e.g. hook.myhook.event = pre-commit */
@@ -493,8 +496,7 @@ int run_hooks_opt(struct repository *r, const char *hook_name,
496 * Ensure cb_data copy and free functions are either provided together,
497 * or neither one is provided.
498 */
496 - if ((options->feed_pipe_cb_data_alloc && !options->feed_pipe_cb_data_free) ||
497 - (!options->feed_pipe_cb_data_alloc && options->feed_pipe_cb_data_free))
499 + if (!options->feed_pipe_cb_data_alloc != !options->feed_pipe_cb_data_free)
500 BUG("feed_pipe_cb_data_alloc and feed_pipe_cb_data_free must be set together");
501
502 if (options->invoked_hook)
hook.h
+2 -3
@@ -1,9 +1,9 @@
1 #ifndef HOOK_H
2 #define HOOK_H
3 -#include "strvec.h"
3 #include "run-command.h"
4 #include "string-list.h"
5 #include "strmap.h"
6 +#include "strvec.h"
7
8 struct repository;
9
@@ -46,8 +46,7 @@ struct hook {
46 typedef void (*cb_data_free_fn)(void *data);
47 typedef void *(*cb_data_alloc_fn)(void *init_ctx);
48
49 -struct run_hooks_opt
50 -{
49 +struct run_hooks_opt {
50 /* Environment vars to be set for each hook */
51 struct strvec env;
52
refs.c
+2 -1
@@ -2599,7 +2599,8 @@ static int transaction_hook_feed_stdin(int hook_stdin_fd, void *pp_cb, void *pp_
2599
2600 static void *transaction_feed_cb_data_alloc(void *feed_pipe_ctx UNUSED)
2601 {
2602 - struct transaction_feed_cb_data *data = xmalloc(sizeof(*data));
2602 + struct transaction_feed_cb_data *data;
2603 + CALLOC_ARRAY(data, 1);
2604 strbuf_init(&data->buf, 0);
2605 data->index = 0;
2606 return data;
t/t1800-hook.sh
+1 -1
@@ -34,7 +34,7 @@ test_expect_success 'git hook usage' '
34
35 test_expect_success 'git hook list: nonexistent hook' '
36 cat >stderr.expect <<-\EOF &&
37 - warning: No hooks found for event '\''test-hook'\''
37 + warning: no hooks found for event '\''test-hook'\''
38 EOF
39 test_expect_code 1 git hook list test-hook 2>stderr.actual &&
40 test_cmp stderr.expect stderr.actual
transport.c
+2 -1
@@ -1360,7 +1360,8 @@ static int pre_push_hook_feed_stdin(int hook_stdin_fd, void *pp_cb UNUSED, void
1360
1361 static void *pre_push_hook_data_alloc(void *feed_pipe_ctx)
1362 {
1363 - struct feed_pre_push_hook_data *data = xmalloc(sizeof(*data));
1363 + struct feed_pre_push_hook_data *data;
1364 + CALLOC_ARRAY(data, 1);
1365 strbuf_init(&data->buf, 0);
1366 data->refs = (struct ref *)feed_pipe_ctx;
1367 return data;