-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncer.go
66 lines (59 loc) · 1.31 KB
/
syncer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package cs
import (
"fmt"
"net/url"
"github.com/google/go-github/v57/github"
)
type repoSyncer struct {
gitPath string
github *github.Client
githubUserInfo *url.Userinfo
repos []RepoConfig
sources RepoSourceConfig
}
func newRepoSyncer(
gitPath string,
githubToken string,
repos []RepoConfig,
sources RepoSourceConfig,
) *repoSyncer {
s := &repoSyncer{
gitPath: gitPath,
github: github.NewClient(nil),
repos: repos,
sources: sources,
}
if githubToken != "" {
s.github = s.github.WithAuthToken(githubToken)
s.githubUserInfo = url.UserPassword("git", githubToken)
}
return s
}
func (s *repoSyncer) Refresh() ([]Repo, error) {
repos := s.repos
ghRepos, err := ResolveFetchSpecs(s.github, s.sources.GitHub, s.githubUserInfo)
if err != nil {
return nil, err
}
repos = append(repos, ghRepos...)
git, err := openGitRepo(s.gitPath)
if err != nil {
return nil, fmt.Errorf("opening git repo %q: %w", s.gitPath, err)
}
var res []Repo
for _, rc := range repos {
res = append(res, &gitRepo{
repo: git,
remoteURL: rc.RemoteURL,
remoteRef: rc.RemoteRef,
localRef: rc.Name,
})
}
// We could parallelize this... but go-git storage isn't thread safe.
for _, repo := range res {
if _, err := repo.Refresh(); err != nil {
return nil, err
}
}
return res, nil
}