@matheusbsbs / matheus-bagliano / commits / 5e1c71fd14
Fork 0

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 <jaysoffian@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jay Soffian committed Apr 19, 2010 at 18:08 UTC 5e1c71fd1488a33680c313f287b88d9f7a7d3e45
1 file changed +27 -33
builtin-receive-pack.c
+27 -33
@@ -134,8 +134,6 @@ struct command {
134 char ref_name[FLEX_ARRAY]; /* more */
135 };
136
137 -static struct command *commands;
138 -
137 static const char pre_receive_hook[] = "hooks/pre-receive";
138 static const char post_receive_hook[] = "hooks/post-receive";
139
@@ -188,7 +186,7 @@ static int copy_to_sideband(int in, int out, void *arg)
186 return 0;
187 }
188
191 -static int run_receive_hook(const char *hook_name)
189 +static int run_receive_hook(struct command *commands, const char *hook_name)
190 {
191 static char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4];
192 struct command *cmd;
@@ -447,15 +445,15 @@ static const char *update(struct command *cmd)
445
446 static char update_post_hook[] = "hooks/post-update";
447
450 -static void run_update_post_hook(struct command *cmd)
448 +static void run_update_post_hook(struct command *commands)
449 {
452 - struct command *cmd_p;
450 + struct command *cmd;
451 int argc;
452 const char **argv;
453 struct child_process proc;
454
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)
457 continue;
458 argc++;
459 }
@@ -464,12 +462,12 @@ static void run_update_post_hook(struct command *cmd)
462 argv = xmalloc(sizeof(*argv) * (2 + argc));
463 argv[0] = update_post_hook;
464
467 - for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
465 + for (argc = 1, cmd = commands; cmd; cmd = cmd->next) {
466 char *p;
469 - if (cmd_p->error_string)
467 + if (cmd->error_string)
468 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);
471 argv[argc] = p;
472 argc++;
473 }
@@ -488,37 +486,32 @@ static void run_update_post_hook(struct command *cmd)
486 }
487 }
488
491 -static void execute_commands(const char *unpacker_error)
489 +static void execute_commands(struct command *commands, const char *unpacker_error)
490 {
493 - struct command *cmd = commands;
491 + struct command *cmd;
492 unsigned char sha1[20];
493
494 if (unpacker_error) {
497 - while (cmd) {
495 + for (cmd = commands; cmd; cmd = cmd->next)
496 cmd->error_string = "n/a (unpacker error)";
499 - cmd = cmd->next;
500 - }
497 return;
498 }
499
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)
502 cmd->error_string = "pre-receive hook declined";
507 - cmd = cmd->next;
508 - }
503 return;
504 }
505
506 head_name = resolve_ref("HEAD", sha1, 0, NULL);
507
514 - while (cmd) {
508 + for (cmd = commands; cmd; cmd = cmd->next)
509 cmd->error_string = update(cmd);
516 - cmd = cmd->next;
517 - }
510 }
511
520 -static void read_head_info(void)
512 +static struct command *read_head_info(void)
513 {
514 + struct command *commands = NULL;
515 struct command **p = &commands;
516 for (;;) {
517 static char line[1000];
@@ -557,6 +550,7 @@ static void read_head_info(void)
550 *p = cmd;
551 p = &cmd->next;
552 }
553 + return commands;
554 }
555
556 static const char *parse_pack_header(struct pack_header *hdr)
@@ -643,7 +637,7 @@ static const char *unpack(void)
637 }
638 }
639
646 -static void report(const char *unpack_status)
640 +static void report(struct command *commands, const char *unpack_status)
641 {
642 struct command *cmd;
643 struct strbuf buf = STRBUF_INIT;
@@ -667,12 +661,12 @@ static void report(const char *unpack_status)
661 strbuf_release(&buf);
662 }
663
670 -static int delete_only(struct command *cmd)
664 +static int delete_only(struct command *commands)
665 {
672 - while (cmd) {
666 + struct command *cmd;
667 + for (cmd = commands; cmd; cmd = cmd->next) {
668 if (!is_null_sha1(cmd->new_sha1))
669 return 0;
675 - cmd = cmd->next;
670 }
671 return 1;
672 }
@@ -722,6 +716,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
716 int stateless_rpc = 0;
717 int i;
718 char *dir = NULL;
719 + struct command *commands;
720
721 argv++;
722 for (i = 1; i < argc; i++) {
@@ -772,18 +767,17 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
767 if (advertise_refs)
768 return 0;
769
775 - read_head_info();
776 - if (commands) {
770 + if ((commands = read_head_info()) != NULL) {
771 const char *unpack_status = NULL;
772
773 if (!delete_only(commands))
774 unpack_status = unpack();
781 - execute_commands(unpack_status);
775 + execute_commands(commands, unpack_status);
776 if (pack_lockfile)
777 unlink_or_warn(pack_lockfile);
778 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);
781 run_update_post_hook(commands);
782 if (auto_gc) {
783 const char *argv_gc_auto[] = {