diff --git a/git/ssh_config.go b/git/ssh_config.go index e06024c8f9cda2959286f292c0cf6e517b924a9f..2df36d81354e32848fc7280a1844465e16e78659 100644 --- a/git/ssh_config.go +++ b/git/ssh_config.go @@ -71,6 +71,14 @@ func (r *SSHConfigReader) readFile(c SSHConfig, re *regexp.Regexp, f string) err } func expandTokens(text, host string) string { - expanded := strings.Replace(text, "%h", host, -1) - return strings.Replace(expanded, "%%", "%", -1) + re := regexp.MustCompile(`%[%h]`) + return re.ReplaceAllStringFunc(text, func(match string) string { + switch match { + case "%h": + return host + case "%%": + return "%" + } + return "" + }) } diff --git a/git/ssh_config_test.go b/git/ssh_config_test.go index b4bf298afab859c4af2e30b8ed78b2b20dff3721..f9d89d1551d02c718920c603cea886c5cad72f50 100644 --- a/git/ssh_config_test.go +++ b/git/ssh_config_test.go @@ -28,13 +28,13 @@ func TestSSHConfigReader_Read(t *testing.T) { func TestSSHConfigReader_ExpandTokens(t *testing.T) { f, _ := ioutil.TempFile("", "ssh-config") c := `Host github.com example.org - Hostname 1-%h-2-%%-3-%h-%% + Hostname 1-%h-2-%%h-3-%h-%% ` ioutil.WriteFile(f.Name(), []byte(c), os.ModePerm) r := &SSHConfigReader{[]string{f.Name()}} sc := r.Read() - assert.Equal(t, "1-github.com-2-%-3-github.com-%", sc["github.com"]) - assert.Equal(t, "1-example.org-2-%-3-example.org-%", sc["example.org"]) + assert.Equal(t, "1-github.com-2-%h-3-github.com-%", sc["github.com"]) + assert.Equal(t, "1-example.org-2-%h-3-example.org-%", sc["example.org"]) }