Skip to content
This repository has been archived by the owner on Jun 16, 2020. It is now read-only.

Commit

Permalink
If possible, use local git executable to get remote URLs
Browse files Browse the repository at this point in the history
The local git executable will rewrite remote URLs based
on 'insteadOf' and 'pushInsteadOf' configuration options.
This is not yet supported by go-git. See issue #7.
  • Loading branch information
nbedos committed Dec 26, 2019
1 parent 7e57984 commit fa4bf20
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 182 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* Feature: Monitor all remotes of local repositories instead of just 'origin' ([issue #3](https://github.com/nbedos/cistern/issues/3))
* Bugfix: Lookup path if BROWSER is not a path itself ([issue #8](https://github.com/nbedos/cistern/issues/8))
* Bugfix: Add support for 'insteadOf' and 'pushInsteadOf' configuration for remote URLs ([issue #7](https://github.com/nbedos/cistern/issues/7))
* Chore: Rewrite build script in go for improved maintainability
* Chore: Rename repository

Expand Down
22 changes: 20 additions & 2 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,26 @@ func RemotesAndCommit(path string, ref string) ([]string, Commit, error) {
if err != nil {
return nil, Commit{}, err
}
remoteURLs := make([]string, 0, len(remotes))
remoteURLs := make([]string, 0)
for _, remote := range remotes {
remoteURLs = append(remoteURLs, remote.Config().URLs...)
// Try calling the local git binary to get the list of push URLs for
// this remote. For now this is better than what go-git offers since
// it takes into account insteadOf and pushInsteadOf configuration
// options. If it fails, use the URL list provided by go-git.
//
// Remove this once the following issue is fixed:
// https://github.com/src-d/go-git/issues/1266/
cmd := exec.Command("git", "remote", "get-url", "--push", "--all", remote.Config().Name)
cmd.Dir = path
if bs, err := cmd.Output(); err == nil {
for _, u := range strings.Split(string(bs), "\n") {
if u = strings.TrimSuffix(u, "\r"); u != "" {
remoteURLs = append(remoteURLs, u)
}
}
} else {
remoteURLs = append(remoteURLs, remote.Config().URLs...)
}
}

return remoteURLs, c, nil
Expand Down Expand Up @@ -657,6 +674,7 @@ func (c *Cache) broadcastMonitorRefStatus(ctx context.Context, repo string, ref
ctx, cancel := context.WithCancel(ctx)
wg := sync.WaitGroup{}
requestCount := 0
// FIXME Deduplicate repositoryURLs
for _, u := range repositoryURLs {
for _, p := range c.sourceProviders {
requestCount++
Expand Down
46 changes: 40 additions & 6 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"sort"
"strings"
Expand Down Expand Up @@ -532,7 +533,7 @@ func createRepository(t *testing.T, remotes []config.RemoteConfig) (string, stri
return tmpDir, sha.String()
}

func TestGitOriginURL(t *testing.T) {
func TestRemotesAndCommit(t *testing.T) {
t.Run("invalid path", func(t *testing.T) {
_, _, err := RemotesAndCommit("invalid path", "HEAD")
if err != ErrUnknownRepositoryURL {
Expand All @@ -554,25 +555,57 @@ func TestGitOriginURL(t *testing.T) {
remotes := []config.RemoteConfig{
{
Name: "origin",
URLs: []string{"url1", "url2"},
URLs: []string{"pushfetch1"},
Fetch: nil,
},
{
Name: "other",
URLs: []string{"url3", "url4"},
Name: "other1",
URLs: []string{"pushfetch2", "push1"},
Fetch: nil,
},
{
Name: "other2",
URLs: []string{"pushfetch3", "push2", "push3"},
Fetch: nil,
},
{
Name: "other3",
URLs: []string{"pushfetch3", "push4"},
Fetch: nil,
},
}
repositoryPath, _ := createRepository(t, remotes)
defer os.RemoveAll(repositoryPath)
//defer os.RemoveAll(repositoryPath)

// Setup insteadOf configuration
cmd := exec.Command("git", "config", "url.push5.insteadOf", "push3")
cmd.Dir = repositoryPath
if _, err := cmd.Output(); err != nil {
t.Fatal(err)
}
cmd = exec.Command("git", "config", "url.push6.pushInsteadOf", "push4")
cmd.Dir = repositoryPath
if _, err := cmd.Output(); err != nil {
t.Fatal(err)
}

urls, _, err := RemotesAndCommit(repositoryPath, "HEAD")
if err != nil {
t.Fatal(err)
}

expectedURLs := []string{
"push1",
"push2",
"push5",
"push6",
"pushfetch1",
"pushfetch2",
"pushfetch3",
}
sort.Strings(urls)
if diff := cmp.Diff(urls, []string{"url1", "url2", "url3", "url4"}); len(diff) > 0 {
sort.Strings(expectedURLs)
if diff := cmp.Diff(expectedURLs, urls); len(diff) > 0 {
t.Fatal(diff)
}
})
Expand Down Expand Up @@ -696,6 +729,7 @@ func TestCache_broadcastMonitorRefStatus(t *testing.T) {
URLs: []string{"other1"},
},
})
defer os.RemoveAll(repositoryPath)

commitc := make(chan Commit)
errc := make(chan error)
Expand Down
Loading

0 comments on commit fa4bf20

Please sign in to comment.