@@ -541,16 +541,13 @@ static struct child_process *git_proxy_connect(int fd[2], char *host)
541
541
return proxy ;
542
542
}
543
543
544
- static char * get_port ( char * host )
544
+ static const char * get_port_numeric ( const char * p )
545
545
{
546
546
char * end ;
547
- char * p = strchr (host , ':' );
548
-
549
547
if (p ) {
550
548
long port = strtol (p + 1 , & end , 10 );
551
549
if (end != p + 1 && * end == '\0' && 0 <= port && port < 65536 ) {
552
- * p = '\0' ;
553
- return p + 1 ;
550
+ return p ;
554
551
}
555
552
}
556
553
@@ -562,15 +559,14 @@ static char *get_port(char *host)
562
559
* The caller must free() the returned strings.
563
560
*/
564
561
static enum protocol parse_connect_url (const char * url_orig , char * * ret_host ,
565
- char * * ret_port , char * * ret_path )
562
+ char * * ret_path )
566
563
{
567
564
char * url ;
568
565
char * host , * path ;
569
566
char * end ;
570
567
int separator ;
571
568
enum protocol protocol = PROTO_LOCAL ;
572
569
int free_path = 0 ;
573
- char * port = NULL ;
574
570
575
571
if (is_url (url_orig ))
576
572
url = url_decode (url_orig );
@@ -589,16 +585,12 @@ static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
589
585
}
590
586
591
587
/*
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()
594
590
*/
595
591
if (host [0 ] == '[' ) {
596
592
end = strchr (host + 1 , ']' );
597
593
if (end ) {
598
- if (protocol != PROTO_GIT ) {
599
- * end = 0 ;
600
- host ++ ;
601
- }
602
594
end ++ ;
603
595
} else
604
596
end = host ;
@@ -636,17 +628,7 @@ static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
636
628
* ptr = '\0' ;
637
629
}
638
630
639
- /*
640
- * Add support for ssh port: ssh://host.xy:<port>/...
641
- */
642
- if (protocol == PROTO_SSH && separator == '/' )
643
- port = get_port (end );
644
-
645
631
* ret_host = xstrdup (host );
646
- if (port )
647
- * ret_port = xstrdup (port );
648
- else
649
- * ret_port = NULL ;
650
632
if (free_path )
651
633
* ret_path = path ;
652
634
else
@@ -674,7 +656,6 @@ struct child_process *git_connect(int fd[2], const char *url,
674
656
char * host , * path ;
675
657
struct child_process * conn = & no_fork ;
676
658
enum protocol protocol ;
677
- char * port ;
678
659
const char * * arg ;
679
660
struct strbuf cmd = STRBUF_INIT ;
680
661
@@ -683,18 +664,13 @@ struct child_process *git_connect(int fd[2], const char *url,
683
664
*/
684
665
signal (SIGCHLD , SIG_DFL );
685
666
686
- protocol = parse_connect_url (url , & host , & port , & path );
667
+ protocol = parse_connect_url (url , & host , & path );
687
668
if (flags & CONNECT_DIAG_URL ) {
688
669
printf ("Diag: url=%s\n" , url ? url : "NULL" );
689
670
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" );
695
672
printf ("Diag: path=%s\n" , path ? path : "NULL" );
696
673
free (host );
697
- free (port );
698
674
free (path );
699
675
return NULL ;
700
676
}
@@ -721,7 +697,6 @@ struct child_process *git_connect(int fd[2], const char *url,
721
697
target_host , 0 );
722
698
free (target_host );
723
699
free (host );
724
- free (port );
725
700
free (path );
726
701
return conn ;
727
702
}
@@ -737,6 +712,11 @@ struct child_process *git_connect(int fd[2], const char *url,
737
712
if (protocol == PROTO_SSH ) {
738
713
const char * ssh = getenv ("GIT_SSH" );
739
714
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
+
740
720
if (!ssh ) ssh = "ssh" ;
741
721
742
722
* arg ++ = ssh ;
@@ -747,7 +727,7 @@ struct child_process *git_connect(int fd[2], const char *url,
747
727
* arg ++ = putty ? "-P" : "-p" ;
748
728
* arg ++ = port ;
749
729
}
750
- * arg ++ = host ;
730
+ * arg ++ = ssh_host ;
751
731
}
752
732
else {
753
733
/* remove repo-local variables from the environment */
@@ -764,7 +744,6 @@ struct child_process *git_connect(int fd[2], const char *url,
764
744
fd [1 ] = conn -> in ; /* write to child's stdin */
765
745
strbuf_release (& cmd );
766
746
free (host );
767
- free (port );
768
747
free (path );
769
748
return conn ;
770
749
}
0 commit comments