git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/4] Refactoring: connect.c: move duplicated code to get_host_and_port

From
Michael Lukashov <michael.lukashov@gmail.com>
Date
Feb 14, 2010, 21:27 UTC
Message-ID
<1266182863-5048-2-git-send-email-michael.lukashov@gmail.com>
In-Reply-To
<1266182863-5048-1-git-send-email-michael.lukashov@gmail.com>
The following functions:
  git_tcp_connect_sock
  git_tcp_connect_sock,
  git_proxy_connect
have common block of code, which is moved to get_host_and_port
Signed-off-by: Michael Lukashov <michael.lukashov@gmail.com>
---
 connect.c |   83 +++++++++++++++++++++---------------------------------------
 1 files changed, 29 insertions(+), 54 deletions(-)
diff --git a/connect.c b/connect.c
index 20054e4..616b312 100644
--- a/connect.c
+++ b/connect.c
@@ -152,6 +152,30 @@ static enum protocol get_protocol(const char *name)
 #define STR_(s)	# s
 #define STR(s)	STR_(s)
 
+static void get_host_and_port(char **host, const char **port, int set_port_none)
+{
+	char *colon, *end;
+
+	if (*host[0] == '[') {
+		end = strchr(*host + 1, ']');
+		if (end) {
+			*end = 0;
+			end++;
+			(*host)++;
+		} else
+			end = *host;
+	} else
+		end = *host;
+	colon = strchr(end, ':');
+
+	if (colon) {
+		*colon = 0;
+		*port = colon + 1;
+		if (set_port_none && !**port)
+			*port = "<none>";
+	}
+}
+
 #ifndef NO_IPV6
 
 static const char *ai_name(const struct addrinfo *ai)
@@ -170,30 +194,12 @@ static const char *ai_name(const struct addrinfo *ai)
 static int git_tcp_connect_sock(char *host, int flags)
 {
 	int sockfd = -1, saved_errno = 0;
-	char *colon, *end;
 	const char *port = STR(DEFAULT_GIT_PORT);
 	struct addrinfo hints, *ai0, *ai;
 	int gai;
 	int cnt = 0;
 
-	if (host[0] == '[') {
-		end = strchr(host + 1, ']');
-		if (end) {
-			*end = 0;
-			end++;
-			host++;
-		} else
-			end = host;
-	} else
-		end = host;
-	colon = strchr(end, ':');
-
-	if (colon) {
-		*colon = 0;
-		port = colon + 1;
-		if (!*port)
-			port = "<none>";
-	}
+	get_host_and_port(&host, &port, 1);
 
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_socktype = SOCK_STREAM;
@@ -251,30 +257,15 @@ static int git_tcp_connect_sock(char *host, int flags)
 static int git_tcp_connect_sock(char *host, int flags)
 {
 	int sockfd = -1, saved_errno = 0;
-	char *colon, *end;
-	char *port = STR(DEFAULT_GIT_PORT), *ep;
+	const char *port = STR(DEFAULT_GIT_PORT);
+	char *ep;
 	struct hostent *he;
 	struct sockaddr_in sa;
 	char **ap;
 	unsigned int nport;
 	int cnt;
 
-	if (host[0] == '[') {
-		end = strchr(host + 1, ']');
-		if (end) {
-			*end = 0;
-			end++;
-			host++;
-		} else
-			end = host;
-	} else
-		end = host;
-	colon = strchr(end, ':');
-
-	if (colon) {
-		*colon = 0;
-		port = colon + 1;
-	}
+	get_host_and_port(&host, &port, 0);
 
 	if (flags & CONNECT_VERBOSE)
 		fprintf(stderr, "Looking up %s ... ", host);
@@ -406,26 +397,10 @@ static int git_use_proxy(const char *host)
 static void git_proxy_connect(int fd[2], char *host)
 {
 	const char *port = STR(DEFAULT_GIT_PORT);
-	char *colon, *end;
 	const char *argv[4];
 	struct child_process proxy;
 
-	if (host[0] == '[') {
-		end = strchr(host + 1, ']');
-		if (end) {
-			*end = 0;
-			end++;
-			host++;
-		} else
-			end = host;
-	} else
-		end = host;
-	colon = strchr(end, ':');
-
-	if (colon) {
-		*colon = 0;
-		port = colon + 1;
-	}
+	get_host_and_port(&host, &port, 0);
 
 	argv[0] = git_proxy_command;
 	argv[1] = host;
-- 
1.7.0.1571.g856c2
Previous: Michael LukashovNext: Johannes Sixt
Message 2 of 12 in “Refactoring: remove duplicated code from transport.c and builtin-send-pack.c”
  1. 1/4 Refactoring: remove duplicated code from transport.c and builtin-send-pack.cMichael Lukashov, Feb 14, 2010
  2. 2/4 Refactoring: connect.c: move duplicated code to get_host_and_portMichael Lukashov, Feb 14, 2010
  3. Johannes SixtFeb 15, 2010
  4. 3/4 Refactoring: move duplicated code from builtin-pack-objects.c and fast-import.c to object.cMichael Lukashov, Feb 14, 2010
  5. 4/4 Refactoring: remove duplicated code from builtin-checkout.c and merge-recursive.cMichael Lukashov, Feb 14, 2010
  6. Tay Ray ChuanFeb 15, 2010
  7. Jeff KingFeb 15, 2010
  8. Junio C HamanoFeb 15, 2010
  9. Jeff KingFeb 15, 2010
  10. Ilari LiusvaaraFeb 15, 2010
  11. Daniel BarkalowFeb 15, 2010
  12. Larry D'AnnaFeb 15, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.