Skip to content

Commit

Permalink
fix regexp for ssh
Browse files Browse the repository at this point in the history
Signed-off-by: cleverhu <[email protected]>
  • Loading branch information
cleverhu authored and tekton-robot committed Sep 16, 2022
1 parent 75ac2fb commit 34274fb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
11 changes: 6 additions & 5 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ const (
sshMissingKnownHostsSSHCommand = "ssh -o StrictHostKeyChecking=accept-new"
)

var (
// sshURLRegexFormat matches the url of SSH git repository
sshURLRegexFormat = regexp.MustCompile(`(ssh://[\w\d\.]+|.+@?.+\..+:)(:[\d]+){0,1}/*(.*)`)
)
// sshURLRegexFormat matches the url of SSH git repository
var sshURLRegexFormat = regexp.MustCompile(`(ssh://[\w\d\.]+|.+@?.+\..+:)(:[\d]+){0,1}/*(.*)`)

func run(logger *zap.SugaredLogger, dir string, args ...string) (string, error) {
c := exec.Command("git", args...)
Expand Down Expand Up @@ -235,7 +233,7 @@ func ensureHomeEnv(logger *zap.SugaredLogger, homepath string) {
}
}

func ensureHomeEnvSSHLinkedFromPath(logger *zap.SugaredLogger, homeenv string, homepath string) {
func ensureHomeEnvSSHLinkedFromPath(logger *zap.SugaredLogger, homeenv, homepath string) {
if filepath.Clean(homeenv) != filepath.Clean(homepath) {
homeEnvSSH := filepath.Join(homeenv, ".ssh")
homePathSSH := filepath.Join(homepath, ".ssh")
Expand Down Expand Up @@ -276,6 +274,9 @@ func validateGitAuth(logger *zap.SugaredLogger, credsDir, url string) {

// validateGitSSHURLFormat validates the given URL format is SSH or not
func validateGitSSHURLFormat(url string) bool {
if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
return false
}
return sshURLRegexFormat.MatchString(url)
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func TestValidateGitSSHURLFormat(t *testing.T) {
url: "https://host.xz/path/to/repo.git/",
want: false,
},
{
url: "https://host.xz:1443/path/to/repo.git/",
want: false,
},
{
url: "ssh://[email protected]:port/path/to/repo.git/",
want: true,
Expand Down Expand Up @@ -260,7 +264,6 @@ func TestEnsureHomeEnv(t *testing.T) {
if _, err := os.Stat(filepath.Join(homedir, ".ssh")); os.IsNotExist(err) {
t.Errorf("SSH creds not present in homedir %s", homedir)
}

})
}
}
Expand Down

0 comments on commit 34274fb

Please sign in to comment.