@@ -10,6 +10,22 @@ import Utils from './Utils'
1010const exec = util . promisify ( childPross . exec )
1111
1212export default class GitHelper {
13+ static #SSH_PATH_RE = new RegExp (
14+ [
15+ / ^ \s * / ,
16+ / (?: (?< proto > [ a - z ] + ) : \/ \/ ) ? / ,
17+ / (?: (?< user > [ a - z _ ] [ a - z 0 - 9 _ - ] + ) @ ) ? / ,
18+ / (?< domain > [ ^ \s \/ \? # : ] + ) / ,
19+ / (?: : (?< port > [ 0 - 9 ] { 1 , 5 } ) ) ? / ,
20+ / (?: [ \/ : ] (?< owner > [ ^ \s \/ \? # : ] + ) ) ? / ,
21+ / (?: [ \/ : ] (?< repo > [ ^ \s \/ \? # : . ] + ) ) / ,
22+ / (?: .g i t ) ? \/ ? \s * $ / ,
23+ ]
24+ . map ( ( r ) => r . source )
25+ . join ( '' ) ,
26+ 'i'
27+ )
28+
1329 static getLastHash ( directory : string ) {
1430 return git ( directory ) //
1531 . silent ( true ) //
@@ -93,8 +109,7 @@ export default class GitHelper {
93109
94110 // input is like this: ssh://git@github .com:22/caprover/caprover-cli.git
95111 static getDomainFromSanitizedSshRepoPath ( input : string ) {
96- input = input . substring ( 10 )
97- return input . substring ( 0 , input . indexOf ( ':' ) )
112+ return GitHelper . sanitizeRepoPathSsh ( input ) . domain
98113 }
99114
100115 // It returns a string like this "github.com/username/repository.git"
@@ -112,40 +127,22 @@ export default class GitHelper {
112127
113128 // It returns a string like this "ssh://git@github.com:22/caprover/caprover-cli.git"
114129 static sanitizeRepoPathSsh ( input : string ) {
115- input = Utils . removeHttpHttps ( input )
116- if ( ! input . startsWith ( 'git@' ) ) {
117- // If we get here, we have something like github.com/username/repository.git
118- if ( input . indexOf ( ':' ) < 0 ) {
119- input = input . replace ( '/' , ':' )
120- }
121- input = `git@${ input } `
122- }
123-
124- // At this point we have one of the following:
125- // git@github .com:22/caprover/caprover
126- // git@github .com:caprover/caprover
127-
128- let port = '22'
129- const split = input . split ( ':' )
130- if ( split . length == 2 ) {
131- const secondSplit = split [ 1 ] . split ( '/' )
132- if ( `${ Number ( secondSplit [ 0 ] ) } ` === secondSplit [ 0 ] ) {
133- // input is already in this format: git@github .com:22/caprover/caprover
134- port = `${ Number ( secondSplit [ 0 ] ) } `
135- } else {
136- input = `${ split [ 0 ] } :22/${ split [ 1 ] } `
137- }
138- } else {
130+ const found = input . match ( GitHelper . #SSH_PATH_RE)
131+ if ( ! found ) {
139132 throw new Error ( `Malformatted SSH path: ${ input } ` )
140133 }
141134
142- if ( ! input . toLowerCase ( ) . startsWith ( 'ssh://' ) ) {
143- input = `ssh://${ input } `
144- }
145-
146135 return {
147- repoPath : input . replace ( / \/ $ / , '' ) ,
148- port : port ,
136+ user : found . groups ?. user ?? 'git' ,
137+ domain : found . groups ?. domain ,
138+ port : Number ( found . groups ?. port ?? 22 ) ,
139+ owner : found . groups ?. owner ?? '' ,
140+ repo : found . groups ?. repo ,
141+ get repoPath ( ) {
142+ return `ssh://${ this . user } @${ this . domain } :${ this . port } /${
143+ this . owner
144+ } ${ this . owner && '/' } ${ this . repo } .git`
145+ } ,
149146 }
150147 }
151148}
0 commit comments