Skip to content

Commit 326c0ea

Browse files
authored
Ensure git remotes for local repositories (#22)
Issue N/A Description of changes: This patch adds more logic to `pkg/repository.Manager` to ensure that the local repositories remotes point to the right URLs. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent f83b560 commit 326c0ea

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

pkg/repository/manager.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,44 @@ func (m *Manager) EnsureRepository(ctx context.Context, name string) error {
299299
return err
300300
}
301301

302+
err = m.EnsureRemotes(ctx, repo)
303+
if err != nil {
304+
return err
305+
}
306+
307+
return nil
308+
}
309+
310+
// EnsureRemotes ensures that the local repositories have both origin and upstream
311+
// remotes setup and point to the correct URLs.
312+
func (m *Manager) EnsureRemotes(ctx context.Context, repo *Repository) error {
313+
remotes, err := util.GetRepositoryRemotes(repo.gitRepo)
314+
if err != nil {
315+
return err
316+
}
317+
318+
expecetedOriginURL := m.urlBuilder(m.cfg.Github.Username, repo.ExpectedForkName)
319+
// First check that one fo the origin URLs points to the fork url
320+
originURLs, ok := remotes[originRemoteName]
321+
if !ok || !util.InStrings(expecetedOriginURL, originURLs) {
322+
originURLs = append(originURLs, expecetedOriginURL)
323+
err = util.UpdateRepositoryRemotes(repo.gitRepo, originRemoteName, originURLs)
324+
if err != nil {
325+
return fmt.Errorf("error updating origin URL: %v", err)
326+
}
327+
}
328+
329+
expectedUpstreamURL := m.urlBuilder(github.ACKOrg, repo.Name)
330+
// Then check that one of the upstream URLs points to the original
331+
// repository
332+
upstreamURLs, ok := remotes[upstreamRemoteName]
333+
if !ok || !util.InStrings(expectedUpstreamURL, upstreamURLs) {
334+
upstreamURLs = append(upstreamURLs, expectedUpstreamURL)
335+
err = util.UpdateRepositoryRemotes(repo.gitRepo, upstreamRemoteName, upstreamURLs)
336+
if err != nil {
337+
return fmt.Errorf("error updating origin URL: %v", err)
338+
}
339+
}
302340
return nil
303341
}
304342

@@ -314,6 +352,11 @@ func (m *Manager) EnsureAll(ctx context.Context) error {
314352
if err != nil {
315353
return err
316354
}
355+
356+
err = m.EnsureRemotes(ctx, repo)
357+
if err != nil {
358+
return err
359+
}
317360
}
318361
return nil
319362
}

pkg/util/git.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package util
2+
3+
import (
4+
"fmt"
5+
6+
git "gopkg.in/src-d/go-git.v4"
7+
gitconfig "gopkg.in/src-d/go-git.v4/config"
8+
)
9+
10+
// GetRepositoryRemotes returns a map containing the remote names and the URLs they
11+
// point to.
12+
func GetRepositoryRemotes(repo *git.Repository) (map[string][]string, error) {
13+
gitRemotes, err := repo.Remotes()
14+
if err != nil {
15+
return nil, fmt.Errorf("cannot list remotes: %v", err)
16+
}
17+
18+
remotes := map[string][]string{}
19+
for _, remote := range gitRemotes {
20+
remoteCfg := remote.Config()
21+
remotes[remoteCfg.Name] = remoteCfg.URLs
22+
}
23+
return remotes, nil
24+
}
25+
26+
// UpdateRepositoryRemotes updates the URLs list for a specific remote.
27+
func UpdateRepositoryRemotes(repo *git.Repository, name string, URLs []string) error {
28+
cfg, err := repo.Storer.Config()
29+
if err != nil {
30+
return err
31+
}
32+
33+
cfg.Remotes[name] = &gitconfig.RemoteConfig{
34+
Name: name,
35+
URLs: URLs,
36+
}
37+
return repo.Storer.SetConfig(cfg)
38+
}

0 commit comments

Comments
 (0)