@@ -541,16 +541,13 @@ static struct child_process *git_proxy_connect(int fd[2], char *host)
541541 return proxy ;
542542}
543543
544- static char * get_port ( char * host )
544+ static const char * get_port_numeric ( const char * p )
545545{
546546 char * end ;
547- char * p = strchr (host , ':' );
548-
549547 if (p ) {
550548 long port = strtol (p + 1 , & end , 10 );
551549 if (end != p + 1 && * end == '\0' && 0 <= port && port < 65536 ) {
552- * p = '\0' ;
553- return p + 1 ;
550+ return p ;
554551 }
555552 }
556553
@@ -562,15 +559,14 @@ static char *get_port(char *host)
562559 * The caller must free() the returned strings.
563560 */
564561static enum protocol parse_connect_url (const char * url_orig , char * * ret_host ,
565- char * * ret_port , char * * ret_path )
562+ char * * ret_path )
566563{
567564 char * url ;
568565 char * host , * path ;
569566 char * end ;
570567 int separator ;
571568 enum protocol protocol = PROTO_LOCAL ;
572569 int free_path = 0 ;
573- char * port = NULL ;
574570
575571 if (is_url (url_orig ))
576572 url = url_decode (url_orig );
@@ -589,16 +585,12 @@ static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
589585 }
590586
591587 /*
592- * Don't do destructive transforms with git:// as that
593- * protocol code does '[]' unwrapping of its own.
588+ * Don't do destructive transforms as protocol code does
589+ * '[]' unwrapping in get_host_and_port()
594590 */
595591 if (host [0 ] == '[' ) {
596592 end = strchr (host + 1 , ']' );
597593 if (end ) {
598- if (protocol != PROTO_GIT ) {
599- * end = 0 ;
600- host ++ ;
601- }
602594 end ++ ;
603595 } else
604596 end = host ;
@@ -636,17 +628,7 @@ static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
636628 * ptr = '\0' ;
637629 }
638630
639- /*
640- * Add support for ssh port: ssh://host.xy:<port>/...
641- */
642- if (protocol == PROTO_SSH && separator == '/' )
643- port = get_port (end );
644-
645631 * ret_host = xstrdup (host );
646- if (port )
647- * ret_port = xstrdup (port );
648- else
649- * ret_port = NULL ;
650632 if (free_path )
651633 * ret_path = path ;
652634 else
@@ -674,7 +656,6 @@ struct child_process *git_connect(int fd[2], const char *url,
674656 char * host , * path ;
675657 struct child_process * conn = & no_fork ;
676658 enum protocol protocol ;
677- char * port ;
678659 const char * * arg ;
679660 struct strbuf cmd = STRBUF_INIT ;
680661
@@ -683,18 +664,13 @@ struct child_process *git_connect(int fd[2], const char *url,
683664 */
684665 signal (SIGCHLD , SIG_DFL );
685666
686- protocol = parse_connect_url (url , & host , & port , & path );
667+ protocol = parse_connect_url (url , & host , & path );
687668 if (flags & CONNECT_DIAG_URL ) {
688669 printf ("Diag: url=%s\n" , url ? url : "NULL" );
689670 printf ("Diag: protocol=%s\n" , prot_name (protocol ));
690- printf ("Diag: hostandport=%s" , host ? host : "NULL" );
691- if (port )
692- printf (":%s\n" , port );
693- else
694- printf ("\n" );
671+ printf ("Diag: hostandport=%s\n" , host ? host : "NULL" );
695672 printf ("Diag: path=%s\n" , path ? path : "NULL" );
696673 free (host );
697- free (port );
698674 free (path );
699675 return NULL ;
700676 }
@@ -721,7 +697,6 @@ struct child_process *git_connect(int fd[2], const char *url,
721697 target_host , 0 );
722698 free (target_host );
723699 free (host );
724- free (port );
725700 free (path );
726701 return conn ;
727702 }
@@ -737,6 +712,11 @@ struct child_process *git_connect(int fd[2], const char *url,
737712 if (protocol == PROTO_SSH ) {
738713 const char * ssh = getenv ("GIT_SSH" );
739714 int putty = ssh && strcasestr (ssh , "plink" );
715+ char * ssh_host = host ; /* keep host for the free() below */
716+ const char * port = NULL ;
717+ get_host_and_port (& ssh_host , & port );
718+ port = get_port_numeric (port );
719+
740720 if (!ssh ) ssh = "ssh" ;
741721
742722 * arg ++ = ssh ;
@@ -747,7 +727,7 @@ struct child_process *git_connect(int fd[2], const char *url,
747727 * arg ++ = putty ? "-P" : "-p" ;
748728 * arg ++ = port ;
749729 }
750- * arg ++ = host ;
730+ * arg ++ = ssh_host ;
751731 }
752732 else {
753733 /* remove repo-local variables from the environment */
@@ -764,7 +744,6 @@ struct child_process *git_connect(int fd[2], const char *url,
764744 fd [1 ] = conn -> in ; /* write to child's stdin */
765745 strbuf_release (& cmd );
766746 free (host );
767- free (port );
768747 free (path );
769748 return conn ;
770749}
0 commit comments