connect: refactor git_connect to only get the protocol version once
Instead of having each builtin transport asking for which protocol version the user has configured in 'protocol.version' by calling `get_protocol_version_config()` multiple times, factor this logic out so there is just a single call at the beginning of `git_connect()`. This will be helpful in the next patch where we can have centralized logic which determines if we need to request a different protocol version than what the user has configured. Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Brandon Williams committed
Mar 15, 2018 at 10:31 UTC
40fc51e39f178e81cbd7feadda51fca604f02ea8
1 file changed
+15
-12
connect.c
+15
-12
@@ -1035,6 +1035,7 @@ static enum ssh_variant determine_ssh_variant(const char *ssh_command,
1035
*/
1036
static struct child_process *git_connect_git(int fd[2], char *hostandport,
1037
const char *path, const char *prog,
1038
+ enum protocol_version version,
1039
int flags)
1040
{
1041
struct child_process *conn;
@@ -1073,10 +1074,10 @@ static struct child_process *git_connect_git(int fd[2], char *hostandport,
1074
target_host, 0);
1075
1076
/* If using a new version put that stuff here after a second null byte */
1076
- if (get_protocol_version_config() > 0) {
1077
+ if (version > 0) {
1078
strbuf_addch(&request, '\0');
1079
strbuf_addf(&request, "version=%d%c",
1079
- get_protocol_version_config(), '\0');
1080
+ version, '\0');
1081
}
1082
1083
packet_write(fd[1], request.buf, request.len);
@@ -1092,14 +1093,14 @@ static struct child_process *git_connect_git(int fd[2], char *hostandport,
1093
*/
1094
static void push_ssh_options(struct argv_array *args, struct argv_array *env,
1095
enum ssh_variant variant, const char *port,
1095
- int flags)
1096
+ enum protocol_version version, int flags)
1097
{
1098
if (variant == VARIANT_SSH &&
1098
- get_protocol_version_config() > 0) {
1099
+ version > 0) {
1100
argv_array_push(args, "-o");
1101
argv_array_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT);
1102
argv_array_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1102
- get_protocol_version_config());
1103
+ version);
1104
}
1105
1106
if (flags & CONNECT_IPV4) {
@@ -1152,7 +1153,8 @@ static void push_ssh_options(struct argv_array *args, struct argv_array *env,
1153
1154
/* Prepare a child_process for use by Git's SSH-tunneled transport. */
1155
static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
1155
- const char *port, int flags)
1156
+ const char *port, enum protocol_version version,
1157
+ int flags)
1158
{
1159
const char *ssh;
1160
enum ssh_variant variant;
@@ -1186,14 +1188,14 @@ static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
1188
argv_array_push(&detect.args, ssh);
1189
argv_array_push(&detect.args, "-G");
1190
push_ssh_options(&detect.args, &detect.env_array,
1189
- VARIANT_SSH, port, flags);
1191
+ VARIANT_SSH, port, version, flags);
1192
argv_array_push(&detect.args, ssh_host);
1193
1194
variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
1195
}
1196
1197
argv_array_push(&conn->args, ssh);
1196
- push_ssh_options(&conn->args, &conn->env_array, variant, port, flags);
1198
+ push_ssh_options(&conn->args, &conn->env_array, variant, port, version, flags);
1199
argv_array_push(&conn->args, ssh_host);
1200
}
1201
@@ -1214,6 +1216,7 @@ struct child_process *git_connect(int fd[2], const char *url,
1216
char *hostandport, *path;
1217
struct child_process *conn;
1218
enum protocol protocol;
1219
+ enum protocol_version version = get_protocol_version_config();
1220
1221
/* Without this we cannot rely on waitpid() to tell
1222
* what happened to our children.
@@ -1228,7 +1231,7 @@ struct child_process *git_connect(int fd[2], const char *url,
1231
printf("Diag: path=%s\n", path ? path : "NULL");
1232
conn = NULL;
1233
} else if (protocol == PROTO_GIT) {
1231
- conn = git_connect_git(fd, hostandport, path, prog, flags);
1234
+ conn = git_connect_git(fd, hostandport, path, prog, version, flags);
1235
} else {
1236
struct strbuf cmd = STRBUF_INIT;
1237
const char *const *var;
@@ -1271,12 +1274,12 @@ struct child_process *git_connect(int fd[2], const char *url,
1274
strbuf_release(&cmd);
1275
return NULL;
1276
}
1274
- fill_ssh_args(conn, ssh_host, port, flags);
1277
+ fill_ssh_args(conn, ssh_host, port, version, flags);
1278
} else {
1279
transport_check_allowed("file");
1277
- if (get_protocol_version_config() > 0) {
1280
+ if (version > 0) {
1281
argv_array_pushf(&conn->env_array, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1279
- get_protocol_version_config());
1282
+ version);
1283
}
1284
}
1285
argv_array_push(&conn->args, cmd.buf);