Skip to content

Commit 40fc51e

Browse files
bmwillgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f7e2050 commit 40fc51e

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

connect.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,7 @@ static enum ssh_variant determine_ssh_variant(const char *ssh_command,
10351035
*/
10361036
static struct child_process *git_connect_git(int fd[2], char *hostandport,
10371037
const char *path, const char *prog,
1038+
enum protocol_version version,
10381039
int flags)
10391040
{
10401041
struct child_process *conn;
@@ -1073,10 +1074,10 @@ static struct child_process *git_connect_git(int fd[2], char *hostandport,
10731074
target_host, 0);
10741075

10751076
/* 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) {
10771078
strbuf_addch(&request, '\0');
10781079
strbuf_addf(&request, "version=%d%c",
1079-
get_protocol_version_config(), '\0');
1080+
version, '\0');
10801081
}
10811082

10821083
packet_write(fd[1], request.buf, request.len);
@@ -1092,14 +1093,14 @@ static struct child_process *git_connect_git(int fd[2], char *hostandport,
10921093
*/
10931094
static void push_ssh_options(struct argv_array *args, struct argv_array *env,
10941095
enum ssh_variant variant, const char *port,
1095-
int flags)
1096+
enum protocol_version version, int flags)
10961097
{
10971098
if (variant == VARIANT_SSH &&
1098-
get_protocol_version_config() > 0) {
1099+
version > 0) {
10991100
argv_array_push(args, "-o");
11001101
argv_array_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT);
11011102
argv_array_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1102-
get_protocol_version_config());
1103+
version);
11031104
}
11041105

11051106
if (flags & CONNECT_IPV4) {
@@ -1152,7 +1153,8 @@ static void push_ssh_options(struct argv_array *args, struct argv_array *env,
11521153

11531154
/* Prepare a child_process for use by Git's SSH-tunneled transport. */
11541155
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)
11561158
{
11571159
const char *ssh;
11581160
enum ssh_variant variant;
@@ -1186,14 +1188,14 @@ static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
11861188
argv_array_push(&detect.args, ssh);
11871189
argv_array_push(&detect.args, "-G");
11881190
push_ssh_options(&detect.args, &detect.env_array,
1189-
VARIANT_SSH, port, flags);
1191+
VARIANT_SSH, port, version, flags);
11901192
argv_array_push(&detect.args, ssh_host);
11911193

11921194
variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
11931195
}
11941196

11951197
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);
11971199
argv_array_push(&conn->args, ssh_host);
11981200
}
11991201

@@ -1214,6 +1216,7 @@ struct child_process *git_connect(int fd[2], const char *url,
12141216
char *hostandport, *path;
12151217
struct child_process *conn;
12161218
enum protocol protocol;
1219+
enum protocol_version version = get_protocol_version_config();
12171220

12181221
/* Without this we cannot rely on waitpid() to tell
12191222
* what happened to our children.
@@ -1228,7 +1231,7 @@ struct child_process *git_connect(int fd[2], const char *url,
12281231
printf("Diag: path=%s\n", path ? path : "NULL");
12291232
conn = NULL;
12301233
} 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);
12321235
} else {
12331236
struct strbuf cmd = STRBUF_INIT;
12341237
const char *const *var;
@@ -1271,12 +1274,12 @@ struct child_process *git_connect(int fd[2], const char *url,
12711274
strbuf_release(&cmd);
12721275
return NULL;
12731276
}
1274-
fill_ssh_args(conn, ssh_host, port, flags);
1277+
fill_ssh_args(conn, ssh_host, port, version, flags);
12751278
} else {
12761279
transport_check_allowed("file");
1277-
if (get_protocol_version_config() > 0) {
1280+
if (version > 0) {
12781281
argv_array_pushf(&conn->env_array, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1279-
get_protocol_version_config());
1282+
version);
12801283
}
12811284
}
12821285
argv_array_push(&conn->args, cmd.buf);

0 commit comments

Comments
 (0)