add support for alternate remotes by grabbing it out of the git hawser push args

This commit is contained in:
Rick Olson 2015-02-02 15:39:03 -07:00
parent 4367c72822
commit df17f104c1
3 changed files with 46 additions and 6 deletions

@ -62,6 +62,8 @@ func pushCommand(cmd *cobra.Command, args []string) {
os.Exit(1) os.Exit(1)
} }
hawser.Config.CurrentRemote = args[0]
if useStdin { if useStdin {
refsData, err := ioutil.ReadAll(os.Stdin) refsData, err := ioutil.ReadAll(os.Stdin)
if err != nil { if err != nil {

@ -12,15 +12,17 @@ import (
) )
type Configuration struct { type Configuration struct {
gitConfig map[string]string CurrentRemote string
remotes []string gitConfig map[string]string
httpClient *http.Client remotes []string
httpClient *http.Client
} }
var ( var (
Config = &Configuration{} Config = &Configuration{CurrentRemote: defaultRemote}
httpPrefixRe = regexp.MustCompile("\\Ahttps?://")
RedirectError = fmt.Errorf("Unexpected redirection") RedirectError = fmt.Errorf("Unexpected redirection")
httpPrefixRe = regexp.MustCompile("\\Ahttps?://")
defaultRemote = "origin"
) )
func HttpClient() *http.Client { func HttpClient() *http.Client {
@ -47,10 +49,20 @@ func (c *Configuration) Endpoint() string {
return url return url
} }
return c.RemoteEndpoint("origin") if len(c.CurrentRemote) > 0 && c.CurrentRemote != defaultRemote {
if endpoint := c.RemoteEndpoint(c.CurrentRemote); len(endpoint) > 0 {
return endpoint
}
}
return c.RemoteEndpoint(defaultRemote)
} }
func (c *Configuration) RemoteEndpoint(remote string) string { func (c *Configuration) RemoteEndpoint(remote string) string {
if len(remote) == 0 {
remote = defaultRemote
}
if url, ok := c.GitConfig("remote." + remote + ".hawser"); ok { if url, ok := c.GitConfig("remote." + remote + ".hawser"); ok {
return url return url
} }

@ -26,6 +26,32 @@ func TestEndpointOverridesOrigin(t *testing.T) {
assert.Equal(t, "abc", config.Endpoint()) assert.Equal(t, "abc", config.Endpoint())
} }
func TestEndpointNoOverrideDefaultRemote(t *testing.T) {
config := &Configuration{
gitConfig: map[string]string{
"remote.origin.hawser": "abc",
"remote.other.hawser": "def",
},
remotes: []string{},
}
assert.Equal(t, "abc", config.Endpoint())
}
func TestEndpointUseAlternateRemote(t *testing.T) {
config := &Configuration{
gitConfig: map[string]string{
"remote.origin.hawser": "abc",
"remote.other.hawser": "def",
},
remotes: []string{},
}
config.CurrentRemote = "other"
assert.Equal(t, "def", config.Endpoint())
}
func TestEndpointAddsMediaSuffix(t *testing.T) { func TestEndpointAddsMediaSuffix(t *testing.T) {
config := &Configuration{ config := &Configuration{
gitConfig: map[string]string{"remote.origin.url": "https://example.com/foo/bar"}, gitConfig: map[string]string{"remote.origin.url": "https://example.com/foo/bar"},