Skip to content

Commit b06770e

Browse files
10ne1gitster
authored andcommitted
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>
1 parent 6b9f9e2 commit b06770e

7 files changed

Lines changed: 61 additions & 57 deletions

File tree

builtin/hook.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include "gettext.h"
66
#include "hook.h"
77
#include "parse-options.h"
8-
#include "strvec.h"
9-
#include "abspath.h"
108

119
#define BUILTIN_HOOK_RUN_USAGE \
1210
N_("git hook run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]")
@@ -51,15 +49,15 @@ static int list(int argc, const char **argv, const char *prefix,
5149
* arguments later they probably should be caught by parse_options.
5250
*/
5351
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"),
5553
builtin_hook_list_usage, list_options);
5654

5755
hookname = argv[0];
5856

5957
head = list_hooks(repo, hookname, NULL);
6058

6159
if (!head->nr) {
62-
warning(_("No hooks found for event '%s'"), hookname);
60+
warning(_("no hooks found for event '%s'"), hookname);
6361
ret = 1; /* no hooks found */
6462
goto cleanup;
6563
}

builtin/receive-pack.c

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,45 @@
33

44
#include "builtin.h"
55
#include "abspath.h"
6-
6+
#include "commit.h"
7+
#include "commit-reach.h"
78
#include "config.h"
9+
#include "connect.h"
10+
#include "connected.h"
811
#include "environment.h"
12+
#include "exec-cmd.h"
13+
#include "fsck.h"
914
#include "gettext.h"
15+
#include "gpg-interface.h"
1016
#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"
1717
#include "hook.h"
18-
#include "exec-cmd.h"
19-
#include "commit.h"
18+
#include "lockfile.h"
2019
#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"
3420
#include "object-file.h"
3521
#include "object-name.h"
3622
#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"
3729
#include "protocol.h"
38-
#include "commit-reach.h"
30+
#include "refs.h"
31+
#include "remote.h"
32+
#include "run-command.h"
3933
#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"
4041
#include "trace.h"
4142
#include "trace2.h"
43+
#include "version.h"
4244
#include "worktree.h"
43-
#include "shallow.h"
44-
#include "setup.h"
45-
#include "parse-options.h"
4645

4746
static const char * const receive_pack_usage[] = {
4847
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_
904903
static void *receive_hook_feed_state_alloc(void *feed_pipe_ctx)
905904
{
906905
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);
908909
data->report = init_state->report;
909910
data->cmd = init_state->cmd;
910911
data->skip_broken = init_state->skip_broken;
911912
strbuf_init(&data->buf, 0);
913+
912914
return data;
913915
}
914916

@@ -928,7 +930,11 @@ static int run_receive_hook(struct command *commands,
928930
{
929931
struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
930932
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+
};
932938
struct async sideband_async;
933939
int sideband_async_started = 0;
934940
int saved_stderr = -1;
@@ -961,9 +967,6 @@ static int run_receive_hook(struct command *commands,
961967
prepare_sideband_async(&sideband_async, &saved_stderr, &sideband_async_started);
962968

963969
/* 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);
967970
opt.feed_pipe_ctx = &feed_init_state;
968971
opt.feed_pipe = feed_receive_hook_cb;
969972
opt.feed_pipe_cb_data_alloc = receive_hook_feed_state_alloc;

hook.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#include "git-compat-util.h"
22
#include "abspath.h"
33
#include "advice.h"
4+
#include "config.h"
5+
#include "environment.h"
46
#include "gettext.h"
57
#include "hook.h"
6-
#include "path.h"
78
#include "parse.h"
9+
#include "path.h"
810
#include "run-command.h"
9-
#include "config.h"
11+
#include "setup.h"
1012
#include "strbuf.h"
1113
#include "strmap.h"
12-
#include "environment.h"
13-
#include "setup.h"
1414

1515
const char *find_hook(struct repository *r, const char *name)
1616
{
@@ -57,9 +57,9 @@ static void hook_clear(struct hook *h, cb_data_free_fn cb_data_free)
5757
if (!h)
5858
return;
5959

60-
if (h->kind == HOOK_TRADITIONAL)
60+
if (h->kind == HOOK_TRADITIONAL) {
6161
free((void *)h->u.traditional.path);
62-
else if (h->kind == HOOK_CONFIGURED) {
62+
} else if (h->kind == HOOK_CONFIGURED) {
6363
free((void *)h->u.configured.friendly_name);
6464
free((void *)h->u.configured.command);
6565
}
@@ -91,7 +91,7 @@ static void list_hooks_add_default(struct repository *r, const char *hookname,
9191
if (!hook_path)
9292
return;
9393

94-
h = xcalloc(1, sizeof(struct hook));
94+
CALLOC_ARRAY(h, 1);
9595

9696
/*
9797
* 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,
154154
strmap_get(&data->event_hooks, value);
155155

156156
if (!hooks) {
157-
hooks = xcalloc(1, sizeof(*hooks));
157+
CALLOC_ARRAY(hooks, 1);
158158
string_list_init_dup(hooks);
159159
strmap_put(&data->event_hooks, value, hooks);
160160
}
@@ -227,8 +227,9 @@ static void build_hook_config_map(struct repository *r, struct strmap *cache)
227227
/* Construct the cache from parsed configs. */
228228
strmap_for_each_entry(&cb_data.event_hooks, &iter, e) {
229229
struct string_list *hook_names = e->value;
230-
struct string_list *hooks = xcalloc(1, sizeof(*hooks));
230+
struct string_list *hooks;
231231

232+
CALLOC_ARRAY(hooks, 1);
232233
string_list_init_dup(hooks);
233234

234235
for (size_t i = 0; i < hook_names->nr; i++) {
@@ -281,17 +282,17 @@ static struct strmap *get_hook_config_cache(struct repository *r)
281282
* it just once on the first call.
282283
*/
283284
if (!r->hook_config_cache) {
284-
r->hook_config_cache = xcalloc(1, sizeof(*cache));
285+
CALLOC_ARRAY(r->hook_config_cache, 1);
285286
strmap_init(r->hook_config_cache);
286287
build_hook_config_map(r, r->hook_config_cache);
287288
}
288289
cache = r->hook_config_cache;
289290
} else {
290291
/*
291292
* 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.
293294
*/
294-
cache = xcalloc(1, sizeof(*cache));
295+
CALLOC_ARRAY(cache, 1);
295296
strmap_init(cache);
296297
build_hook_config_map(r, cache);
297298
}
@@ -311,7 +312,9 @@ static void list_hooks_add_configured(struct repository *r,
311312
for (size_t i = 0; configured_hooks && i < configured_hooks->nr; i++) {
312313
const char *friendly_name = configured_hooks->items[i].string;
313314
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);
315318

316319
if (options && options->feed_pipe_cb_data_alloc)
317320
hook->feed_pipe_cb_data =
@@ -343,7 +346,7 @@ struct string_list *list_hooks(struct repository *r, const char *hookname,
343346
if (!hookname)
344347
BUG("null hookname was provided to hook_list()!");
345348

346-
hook_head = xmalloc(sizeof(struct string_list));
349+
CALLOC_ARRAY(hook_head, 1);
347350
string_list_init_dup(hook_head);
348351

349352
/* 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,
493496
* Ensure cb_data copy and free functions are either provided together,
494497
* or neither one is provided.
495498
*/
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)
498500
BUG("feed_pipe_cb_data_alloc and feed_pipe_cb_data_free must be set together");
499501

500502
if (options->invoked_hook)

hook.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#ifndef HOOK_H
22
#define HOOK_H
3-
#include "strvec.h"
43
#include "run-command.h"
54
#include "string-list.h"
65
#include "strmap.h"
6+
#include "strvec.h"
77

88
struct repository;
99

@@ -46,8 +46,7 @@ struct hook {
4646
typedef void (*cb_data_free_fn)(void *data);
4747
typedef void *(*cb_data_alloc_fn)(void *init_ctx);
4848

49-
struct run_hooks_opt
50-
{
49+
struct run_hooks_opt {
5150
/* Environment vars to be set for each hook */
5251
struct strvec env;
5352

refs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2599,7 +2599,8 @@ static int transaction_hook_feed_stdin(int hook_stdin_fd, void *pp_cb, void *pp_
25992599

26002600
static void *transaction_feed_cb_data_alloc(void *feed_pipe_ctx UNUSED)
26012601
{
2602-
struct transaction_feed_cb_data *data = xmalloc(sizeof(*data));
2602+
struct transaction_feed_cb_data *data;
2603+
CALLOC_ARRAY(data, 1);
26032604
strbuf_init(&data->buf, 0);
26042605
data->index = 0;
26052606
return data;

t/t1800-hook.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ test_expect_success 'git hook usage' '
3434

3535
test_expect_success 'git hook list: nonexistent hook' '
3636
cat >stderr.expect <<-\EOF &&
37-
warning: No hooks found for event '\''test-hook'\''
37+
warning: no hooks found for event '\''test-hook'\''
3838
EOF
3939
test_expect_code 1 git hook list test-hook 2>stderr.actual &&
4040
test_cmp stderr.expect stderr.actual

transport.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1360,7 +1360,8 @@ static int pre_push_hook_feed_stdin(int hook_stdin_fd, void *pp_cb UNUSED, void
13601360

13611361
static void *pre_push_hook_data_alloc(void *feed_pipe_ctx)
13621362
{
1363-
struct feed_pre_push_hook_data *data = xmalloc(sizeof(*data));
1363+
struct feed_pre_push_hook_data *data;
1364+
CALLOC_ARRAY(data, 1);
13641365
strbuf_init(&data->buf, 0);
13651366
data->refs = (struct ref *)feed_pipe_ctx;
13661367
return data;

0 commit comments

Comments
 (0)