Skip to content

Commit 5e1c71f

Browse files
jaysoffiangitster
authored andcommitted
receive-pack: switch global variable 'commands' to a parameter
Receive-pack is inconsistent in its usage of the 'commands' variable; though it is setup as a global and accessed that way by execute_commands(), report(), and run_receive_hook(), it is also passed as a parameter to delete_only() and run_update_post_hook(). For consistency, make it local to cmd_receive_pack and pass it as a parameter. As long as we're cleaning up, also make our use of the names 'commands' and 'cmd' consistent. Signed-off-by: Jay Soffian <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f78683f commit 5e1c71f

File tree

1 file changed

+27
-33
lines changed

1 file changed

+27
-33
lines changed

builtin-receive-pack.c

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,6 @@ struct command {
134134
char ref_name[FLEX_ARRAY]; /* more */
135135
};
136136

137-
static struct command *commands;
138-
139137
static const char pre_receive_hook[] = "hooks/pre-receive";
140138
static const char post_receive_hook[] = "hooks/post-receive";
141139

@@ -188,7 +186,7 @@ static int copy_to_sideband(int in, int out, void *arg)
188186
return 0;
189187
}
190188

191-
static int run_receive_hook(const char *hook_name)
189+
static int run_receive_hook(struct command *commands, const char *hook_name)
192190
{
193191
static char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4];
194192
struct command *cmd;
@@ -447,15 +445,15 @@ static const char *update(struct command *cmd)
447445

448446
static char update_post_hook[] = "hooks/post-update";
449447

450-
static void run_update_post_hook(struct command *cmd)
448+
static void run_update_post_hook(struct command *commands)
451449
{
452-
struct command *cmd_p;
450+
struct command *cmd;
453451
int argc;
454452
const char **argv;
455453
struct child_process proc;
456454

457-
for (argc = 0, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
458-
if (cmd_p->error_string)
455+
for (argc = 0, cmd = commands; cmd; cmd = cmd->next) {
456+
if (cmd->error_string)
459457
continue;
460458
argc++;
461459
}
@@ -464,12 +462,12 @@ static void run_update_post_hook(struct command *cmd)
464462
argv = xmalloc(sizeof(*argv) * (2 + argc));
465463
argv[0] = update_post_hook;
466464

467-
for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
465+
for (argc = 1, cmd = commands; cmd; cmd = cmd->next) {
468466
char *p;
469-
if (cmd_p->error_string)
467+
if (cmd->error_string)
470468
continue;
471-
p = xmalloc(strlen(cmd_p->ref_name) + 1);
472-
strcpy(p, cmd_p->ref_name);
469+
p = xmalloc(strlen(cmd->ref_name) + 1);
470+
strcpy(p, cmd->ref_name);
473471
argv[argc] = p;
474472
argc++;
475473
}
@@ -488,37 +486,32 @@ static void run_update_post_hook(struct command *cmd)
488486
}
489487
}
490488

491-
static void execute_commands(const char *unpacker_error)
489+
static void execute_commands(struct command *commands, const char *unpacker_error)
492490
{
493-
struct command *cmd = commands;
491+
struct command *cmd;
494492
unsigned char sha1[20];
495493

496494
if (unpacker_error) {
497-
while (cmd) {
495+
for (cmd = commands; cmd; cmd = cmd->next)
498496
cmd->error_string = "n/a (unpacker error)";
499-
cmd = cmd->next;
500-
}
501497
return;
502498
}
503499

504-
if (run_receive_hook(pre_receive_hook)) {
505-
while (cmd) {
500+
if (run_receive_hook(commands, pre_receive_hook)) {
501+
for (cmd = commands; cmd; cmd = cmd->next)
506502
cmd->error_string = "pre-receive hook declined";
507-
cmd = cmd->next;
508-
}
509503
return;
510504
}
511505

512506
head_name = resolve_ref("HEAD", sha1, 0, NULL);
513507

514-
while (cmd) {
508+
for (cmd = commands; cmd; cmd = cmd->next)
515509
cmd->error_string = update(cmd);
516-
cmd = cmd->next;
517-
}
518510
}
519511

520-
static void read_head_info(void)
512+
static struct command *read_head_info(void)
521513
{
514+
struct command *commands = NULL;
522515
struct command **p = &commands;
523516
for (;;) {
524517
static char line[1000];
@@ -557,6 +550,7 @@ static void read_head_info(void)
557550
*p = cmd;
558551
p = &cmd->next;
559552
}
553+
return commands;
560554
}
561555

562556
static const char *parse_pack_header(struct pack_header *hdr)
@@ -643,7 +637,7 @@ static const char *unpack(void)
643637
}
644638
}
645639

646-
static void report(const char *unpack_status)
640+
static void report(struct command *commands, const char *unpack_status)
647641
{
648642
struct command *cmd;
649643
struct strbuf buf = STRBUF_INIT;
@@ -667,12 +661,12 @@ static void report(const char *unpack_status)
667661
strbuf_release(&buf);
668662
}
669663

670-
static int delete_only(struct command *cmd)
664+
static int delete_only(struct command *commands)
671665
{
672-
while (cmd) {
666+
struct command *cmd;
667+
for (cmd = commands; cmd; cmd = cmd->next) {
673668
if (!is_null_sha1(cmd->new_sha1))
674669
return 0;
675-
cmd = cmd->next;
676670
}
677671
return 1;
678672
}
@@ -722,6 +716,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
722716
int stateless_rpc = 0;
723717
int i;
724718
char *dir = NULL;
719+
struct command *commands;
725720

726721
argv++;
727722
for (i = 1; i < argc; i++) {
@@ -772,18 +767,17 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
772767
if (advertise_refs)
773768
return 0;
774769

775-
read_head_info();
776-
if (commands) {
770+
if ((commands = read_head_info()) != NULL) {
777771
const char *unpack_status = NULL;
778772

779773
if (!delete_only(commands))
780774
unpack_status = unpack();
781-
execute_commands(unpack_status);
775+
execute_commands(commands, unpack_status);
782776
if (pack_lockfile)
783777
unlink_or_warn(pack_lockfile);
784778
if (report_status)
785-
report(unpack_status);
786-
run_receive_hook(post_receive_hook);
779+
report(commands, unpack_status);
780+
run_receive_hook(commands, post_receive_hook);
787781
run_update_post_hook(commands);
788782
if (auto_gc) {
789783
const char *argv_gc_auto[] = {

0 commit comments

Comments
 (0)