feat(git): support for remotes

currently a hidden feature
This commit is contained in:
Jan De Dobbeleer 2023-09-23 22:26:55 +02:00 committed by Jan De Dobbeleer
parent bd3cd316c2
commit 31a51ff117
2 changed files with 79 additions and 0 deletions

View file

@ -778,6 +778,30 @@ func (g *Git) getRemoteURL() string {
return g.getGitCommandOutput("remote", "get-url", upstream)
}
func (g *Git) Remotes() map[string]string {
var remotes = make(map[string]string)
location := filepath.Join(g.rootDir, "config")
config := g.env.FileContent(location)
cfg, err := ini.Load([]byte(config))
if err != nil {
return remotes
}
for _, section := range cfg.Sections() {
if !strings.HasPrefix(section.Name(), "remote ") {
continue
}
name := strings.TrimPrefix(section.Name(), "remote ")
name = strings.Trim(name, "\"")
url := section.Key("url").String()
url = g.cleanUpstreamURL(url)
remotes[name] = url
}
return remotes
}
func (g *Git) getUntrackedFilesMode() string {
return g.getSwitchMode(UntrackedModes, "-u", "normal")
}

View file

@ -1050,3 +1050,58 @@ func TestGitCommit(t *testing.T) {
assert.Equal(t, tc.Expected, got, tc.Case)
}
}
func TestGitRemotes(t *testing.T) {
cases := []struct {
Case string
Expected int
Config string
}{
{
Case: "Empty config file",
Expected: 0,
},
{
Case: "Two remotes",
Expected: 2,
Config: `
[remote "origin"]
url = git@github.com:JanDeDobbeleer/test.git
fetch = +refs/heads/*:refs/remotes/origin/*
[remote "upstream"]
url = git@github.com:microsoft/test.git
fetch = +refs/heads/*:refs/remotes/upstream/*
`,
},
{
Case: "One remote",
Expected: 1,
Config: `
[remote "origin"]
url = git@github.com:JanDeDobbeleer/test.git
fetch = +refs/heads/*:refs/remotes/origin/*
`,
},
{
Case: "Broken config",
Expected: 0,
Config: "{{}}",
},
}
for _, tc := range cases {
env := new(mock.MockedEnvironment)
env.On("FileContent", "config").Return(tc.Config)
g := &Git{
scm: scm{
props: properties.Map{},
realDir: "foo",
env: env,
},
}
got := g.Remotes()
assert.Equal(t, tc.Expected, len(got), tc.Case)
}
}