receive-pack.c: shorten the execute_commands loop over all commands
Make the main "execute_commands" loop in receive-pack easier to read by splitting out some steps into helper functions. The new helper 'should_process_cmd' checks if a ref update is unnecessary, whether due to an error having occurred or for another reason. The helper 'warn_if_skipped_connectivity_check' warns if we have forgotten to run a connectivity check on a ref which is shallow for the client which would be a bug. This will help us to duplicate less code in a later patch when we make a second copy of the "execute_commands" loop. No functional change intended. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Stefan Beller committed
Jan 7, 2015 at 19:23 UTC
a6a843196869659ee55238e474be29018987c4bf
1 file changed
+27
-16
builtin/receive-pack.c
+27
-16
@@ -1042,11 +1042,34 @@ static void reject_updates_to_hidden(struct command *commands)
1042
}
1043
}
1044
1045
+static int should_process_cmd(struct command *cmd)
1046
+{
1047
+ return !cmd->error_string && !cmd->skip_update;
1048
+}
1049
+
1050
+static void warn_if_skipped_connectivity_check(struct command *commands,
1051
+ struct shallow_info *si)
1052
+{
1053
+ struct command *cmd;
1054
+ int checked_connectivity = 1;
1055
+
1056
+ for (cmd = commands; cmd; cmd = cmd->next) {
1057
+ if (should_process_cmd(cmd) && si->shallow_ref[cmd->index]) {
1058
+ error("BUG: connectivity check has not been run on ref %s",
1059
+ cmd->ref_name);
1060
+ checked_connectivity = 0;
1061
+ }
1062
+ }
1063
+ if (!checked_connectivity)
1064
+ error("BUG: run 'git fsck' for safety.\n"
1065
+ "If there are errors, try to remove "
1066
+ "the reported refs above");
1067
+}
1068
+
1069
static void execute_commands(struct command *commands,
1070
const char *unpacker_error,
1071
struct shallow_info *si)
1072
{
1049
- int checked_connectivity;
1073
struct command *cmd;
1074
unsigned char sha1[20];
1075
struct iterate_data data;
@@ -1077,27 +1100,15 @@ static void execute_commands(struct command *commands,
1100
free(head_name_to_free);
1101
head_name = head_name_to_free = resolve_refdup("HEAD", 0, sha1, NULL);
1102
1080
- checked_connectivity = 1;
1103
for (cmd = commands; cmd; cmd = cmd->next) {
1082
- if (cmd->error_string)
1083
- continue;
1084
-
1085
- if (cmd->skip_update)
1104
+ if (!should_process_cmd(cmd))
1105
continue;
1106
1107
cmd->error_string = update(cmd, si);
1089
- if (shallow_update && !cmd->error_string &&
1090
- si->shallow_ref[cmd->index]) {
1091
- error("BUG: connectivity check has not been run on ref %s",
1092
- cmd->ref_name);
1093
- checked_connectivity = 0;
1094
- }
1108
}
1109
1097
- if (shallow_update && !checked_connectivity)
1098
- error("BUG: run 'git fsck' for safety.\n"
1099
- "If there are errors, try to remove "
1100
- "the reported refs above");
1110
+ if (shallow_update)
1111
+ warn_if_skipped_connectivity_check(commands, si);
1112
}
1113
1114
static struct command **queue_command(struct command **tail,