From 2c4c0132bf8c9e9819cc9a76d4a04ed4daeedb3b Mon Sep 17 00:00:00 2001 From: Sladyn Date: Sat, 25 May 2019 15:13:40 +0200 Subject: [PATCH] Add GetRemotes functionalities --- cache/repo_cache.go | 7 ++++++- repository/git.go | 22 ++++++++++++++++++++++ repository/mock_repo.go | 7 +++++++ repository/repo.go | 3 +++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/cache/repo_cache.go b/cache/repo_cache.go index dc1889b2b707f89d091f225ca2e09b6e8fe7dee6..b3b2ea1e7062ecac9af2a7944f23ce142bce202c 100644 --- a/cache/repo_cache.go +++ b/cache/repo_cache.go @@ -104,11 +104,16 @@ func (c *RepoCache) GetPath() string { return c.repo.GetPath() } -// GetPath returns the path to the repo. +// GetCoreEditor returns the name of the editor that the user has used to configure git. func (c *RepoCache) GetCoreEditor() (string, error) { return c.repo.GetCoreEditor() } +// GetRemotes returns the configured remotes repositories. +func (c *RepoCache) GetRemotes() (map[string]string, error) { + return c.repo.GetRemotes() +} + // GetUserName returns the name the the user has used to configure git func (c *RepoCache) GetUserName() (string, error) { return c.repo.GetUserName() diff --git a/repository/git.go b/repository/git.go index 4d6ca19a04310246aa2c2d82fa6229cb55f0b13d..801504f2e14b07dba1756971cf95d88261e7b4b2 100644 --- a/repository/git.go +++ b/repository/git.go @@ -162,6 +162,28 @@ func (repo *GitRepo) GetCoreEditor() (string, error) { return repo.runGitCommand("var", "GIT_EDITOR") } +// GetRemotes returns the configured remotes repositories. +func (repo *GitRepo) GetRemotes() (map[string]string, error) { + stdout, err := repo.runGitCommand("remote", "--verbose") + if err != nil { + return nil, err + } + + lines := strings.Split(stdout, "\n") + remotes := make(map[string]string, len(lines)) + + for _, line := range lines { + elements := strings.Fields(line) + if len(elements) != 3 { + return nil, fmt.Errorf("unexpected output format: %s", line) + } + + remotes[elements[0]] = elements[1] + } + + return remotes, nil +} + // StoreConfig store a single key/value pair in the config of the repo func (repo *GitRepo) StoreConfig(key string, value string) error { _, err := repo.runGitCommand("config", "--replace-all", key, value) diff --git a/repository/mock_repo.go b/repository/mock_repo.go index 14f5e7b5b4b998fcb4f6987a44dfb1931b691072..2dc4868ec91882a755354e0a5c13192dee658fa6 100644 --- a/repository/mock_repo.go +++ b/repository/mock_repo.go @@ -59,6 +59,13 @@ func (r *mockRepoForTest) GetCoreEditor() (string, error) { return "vi", nil } +// GetRemotes returns the configured remotes repositories. +func (r *mockRepoForTest) GetRemotes() (map[string]string, error) { + return map[string]string{ + "origin": "git://github.com/MichaelMure/git-bug", + }, nil +} + func (r *mockRepoForTest) StoreConfig(key string, value string) error { r.config[key] = value return nil diff --git a/repository/repo.go b/repository/repo.go index f3c2de6db3d2f26f5c0ad89b2983c693b4c25fdc..4420449325bdd5508490660d1a25fc1c7f21e5ae 100644 --- a/repository/repo.go +++ b/repository/repo.go @@ -27,6 +27,9 @@ type RepoCommon interface { // GetCoreEditor returns the name of the editor that the user has used to configure git. GetCoreEditor() (string, error) + // GetRemotes returns the configured remotes repositories. + GetRemotes() (map[string]string, error) + // StoreConfig store a single key/value pair in the config of the repo StoreConfig(key string, value string) error