diff options
-rw-r--r-- | config/config.go | 12 | ||||
-rw-r--r-- | config/config_test.go | 4 | ||||
-rw-r--r-- | plumbing/transport/ssh/auth_method.go | 26 |
3 files changed, 32 insertions, 10 deletions
diff --git a/config/config.go b/config/config.go index cb10738..475045e 100644 --- a/config/config.go +++ b/config/config.go @@ -5,6 +5,7 @@ import ( "bytes" "errors" "fmt" + "sort" format "gopkg.in/src-d/go-git.v4/plumbing/format/config" ) @@ -168,9 +169,16 @@ func (c *Config) marshalRemotes() { } } - for name, remote := range c.Remotes { + remoteNames := make([]string, 0, len(c.Remotes)) + for name := range c.Remotes { + remoteNames = append(remoteNames, name) + } + + sort.Strings(remoteNames) + + for _, name := range remoteNames { if !added[name] { - newSubsections = append(newSubsections, remote.marshal()) + newSubsections = append(newSubsections, c.Remotes[name].marshal()) } } diff --git a/config/config_test.go b/config/config_test.go index 97f4bbf..c27ee26 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -51,13 +51,13 @@ func (s *ConfigSuite) TestMarshall(c *C) { output := []byte(`[core] bare = true worktree = bar -[remote "origin"] - url = git@github.com:mcuadros/go-git.git [remote "alt"] url = git@github.com:mcuadros/go-git.git url = git@github.com:src-d/go-git.git fetch = +refs/heads/*:refs/remotes/origin/* fetch = +refs/pull/*:refs/remotes/origin/pull/* +[remote "origin"] + url = git@github.com:mcuadros/go-git.git [submodule "qux"] url = https://github.com/foo/qux.git `) diff --git a/plumbing/transport/ssh/auth_method.go b/plumbing/transport/ssh/auth_method.go index f95235b..baae181 100644 --- a/plumbing/transport/ssh/auth_method.go +++ b/plumbing/transport/ssh/auth_method.go @@ -3,6 +3,7 @@ package ssh import ( "crypto/x509" "encoding/pem" + "errors" "fmt" "io/ioutil" "os" @@ -11,6 +12,7 @@ import ( "gopkg.in/src-d/go-git.v4/plumbing/transport" + "github.com/mitchellh/go-homedir" "github.com/xanzy/ssh-agent" "golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh/knownhosts" @@ -164,6 +166,19 @@ func (a *PublicKeys) clientConfig() *ssh.ClientConfig { } } +func username() (string, error) { + var username string + if user, err := user.Current(); err == nil { + username = user.Username + } else { + username = os.Getenv("USER") + } + if username == "" { + return "", errors.New("failed to get username") + } + return username, nil +} + // PublicKeysCallback implements AuthMethod by asking a // ssh.agent.Agent to act as a signer. type PublicKeysCallback struct { @@ -176,13 +191,12 @@ type PublicKeysCallback struct { // a pipe with the SSH agent and uses the pipe as the implementer of the public // key callback function. func NewSSHAgentAuth(u string) (AuthMethod, error) { + var err error if u == "" { - usr, err := user.Current() + u, err = username() if err != nil { - return nil, fmt.Errorf("error getting current user: %q", err) + return nil, err } - - u = usr.Username } a, _, err := sshagent.New() @@ -241,13 +255,13 @@ func getDefaultKnownHostsFiles() ([]string, error) { return files, nil } - user, err := user.Current() + homeDirPath, err := homedir.Dir() if err != nil { return nil, err } return []string{ - filepath.Join(user.HomeDir, "/.ssh/known_hosts"), + filepath.Join(homeDirPath, "/.ssh/known_hosts"), "/etc/ssh/ssh_known_hosts", }, nil } |