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

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

From
Larry D'Anna <larry@elder-gods.org>
Date
Feb 16, 2010, 04:10 UTC
Message-ID
<20100216041004.GA7529@cthulhu>
In-Reply-To
<1266276411-5796-3-git-send-email-michael.lukashov@gmail.com>
* Michael Lukashov (michael.lukashov@gmail.com) [100215 18:33]:
Show 77 quoted lines
> The following functions:
> 
>   git_tcp_connect_sock (IPV6 version)
>   git_tcp_connect_sock (no IPV6 version),
>   git_proxy_connect
> 
> have common block of code. Move it to a new function '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..cd399f4 100644
> --- a/connect.c
> +++ b/connect.c
> @@ -152,6 +152,28 @@ 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)
> +{
> +	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;
> +	}
> +}
> +
>  #ifndef NO_IPV6
>  
>  static const char *ai_name(const struct addrinfo *ai)
> @@ -170,30 +192,14 @@ 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);
> +	if (!*port)
> +		*port = "<none>";
shouldn't that be 'port = "none";'?
          --larry
Previous: Michael LukashovNext: Michael Lukashov
Message 6 of 10 in “Refactoring: remove duplicated code”
  1. 0/4 Refactoring: remove duplicated codeMichael Lukashov, Feb 15, 2010
  2. 1/4 Refactoring: remove duplicated code from builtin-send-pack.c and transport.cMichael Lukashov, Feb 15, 2010
  3. Tay Ray ChuanFeb 16, 2010
  4. Jeff KingFeb 16, 2010
  5. 2/4 Refactoring: connect.c: move duplicated code to get_host_and_portMichael Lukashov, Feb 15, 2010
  6. Larry D'AnnaFeb 16, 2010
  7. 3/4 Refactoring: move duplicated code from builtin-pack-objects.c and fast-import.c to object.cMichael Lukashov, Feb 15, 2010
  8. Junio C HamanoFeb 16, 2010
  9. 4/4 Refactoring: remove duplicated code from builtin-checkout.c and merge-recursive.cMichael Lukashov, Feb 15, 2010
  10. Junio C HamanoFeb 16, 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.