Merge branch 'ml/connect-refactor'
authorJunio C Hamano <gitster@pobox.com>
Tue, 2 Mar 2010 20:44:09 +0000 (12:44 -0800)
committerJunio C Hamano <gitster@pobox.com>
Tue, 2 Mar 2010 20:44:09 +0000 (12:44 -0800)
* ml/connect-refactor:
connect.c: move duplicated code to a new function 'get_host_and_port'

1  2 
connect.c
diff --combined connect.c
index a37cf6af0428023d2d3ef01ab2e04fc462981af4,f4563f951ac48dcc359071b7c1d68b795b617012..9f39038f6f0f0cd4faca81a9451623466f197318
+++ b/connect.c
@@@ -152,6 -152,28 +152,28 @@@ static enum protocol get_protocol(cons
  #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)
  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>";
  
        memset(&hints, 0, sizeof(hints));
        hints.ai_socktype = SOCK_STREAM;
  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);
  
        if (flags & CONNECT_VERBOSE)
                fprintf(stderr, "Looking up %s ... ", host);
@@@ -406,26 -397,10 +397,10 @@@ static int git_use_proxy(const char *ho
  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);
  
        argv[0] = git_proxy_command;
        argv[1] = host;
@@@ -504,7 -479,7 +479,7 @@@ struct child_process *git_connect(int f
  
        /*
         * Don't do destructive transforms with git:// as that
 -       * protocol code does '[]' dewrapping of its own.
 +       * protocol code does '[]' unwrapping of its own.
         */
        if (host[0] == '[') {
                end = strchr(host + 1, ']');