forked from tylerjgarland/git2git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
173 lines (130 loc) · 4.31 KB
/
service.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"fmt"
"log"
"os"
"strings"
"sync"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/thoas/go-funk"
"github.com/tylerjgarland/git2git/repositories"
"github.com/AlecAivazis/survey/v2"
)
func SyncRepositories(originToken string, destinationToken string, origin func(token string) ([]repositories.GitRepository, bool), target func(token string) ([]repositories.GitRepository, bool), createRepositoryAsync func(token string, repoPtr *repositories.GitRepository) string) {
defer func() {
if err := recover(); err != nil {
log.Default().Print(err)
log.Default().Print("Failed to sync repositories. Exiting.")
}
}()
os.RemoveAll("./tmp")
os.Mkdir("./tmp", 0755)
var wg sync.WaitGroup
var originReposChan, targetReposChan chan []repositories.GitRepository = make(chan []repositories.GitRepository, 1), make(chan []repositories.GitRepository, 1)
go GetRepositories(originToken, wg, originReposChan, origin)
go GetRepositories(destinationToken, wg, targetReposChan, target)
wg.Wait()
copyFromRepos := <-originReposChan
if len(copyFromRepos) == 0 {
log.Default().Print("No repositories to copy.")
return
}
names := Checkboxes("Please select which repositories to sync", funk.Map(copyFromRepos, func(r repositories.GitRepository) string {
return r.Name
}).([]string))
reposToDownload := funk.Filter(copyFromRepos, func(item repositories.GitRepository) bool { return funk.Contains(names, item.Name) }).([]repositories.GitRepository)
//Limit to 3 concurrent git clones.
concurrencyLimit := make(chan struct{}, 3)
wg.Add(len(reposToDownload))
for _, repo := range reposToDownload {
func() {
concurrencyLimit <- struct{}{}
defer func() { <-concurrencyLimit }()
go syncRepository(repo, destinationToken, &wg, createRepositoryAsync)
}()
}
wg.Wait()
fmt.Println("Sync complete")
}
func Checkboxes(label string, opts []string) []string {
res := []string{}
prompt := &survey.MultiSelect{
Message: label,
Options: opts,
}
survey.AskOne(prompt, &res)
return res
}
func GetRepositories(token string, waitGroup sync.WaitGroup, reposChannel chan []repositories.GitRepository, getRepositories func(token string) ([]repositories.GitRepository, bool)) {
defer waitGroup.Done()
waitGroup.Add(1)
repos, _ := getRepositories(token)
reposChannel <- repos
}
func syncRepository(repo repositories.GitRepository, gitHubToken string, wgPtr *sync.WaitGroup, createRepositoryAsync func(token string, repoPtr *repositories.GitRepository) string) bool {
repositoryDownloadDir := fmt.Sprintf("./tmp/%s", repo.Name)
os.Mkdir(repositoryDownloadDir, 0755)
defer func() {
wgPtr.Done()
os.Remove(repositoryDownloadDir)
}()
gitRepo, err := git.PlainClone(repositoryDownloadDir, false, &git.CloneOptions{
URL: repo.HTTPUrlToRepo,
})
opts := &git.FetchOptions{
RefSpecs: []config.RefSpec{"refs/*:refs/*", "HEAD:refs/heads/HEAD"},
}
if err != nil {
log.Default().Print(err)
fmt.Printf("Failed to clone repository: %s : %s", err.Error(), repo.Name)
fmt.Println()
return false
}
remote, _ := gitRepo.Remote("origin")
if err := remote.Fetch(opts); err != nil {
fmt.Printf("Repo failed to create: %s", repo.Name)
fmt.Println()
return false
}
fmt.Printf("Downloaded: %s", repo.Name)
fmt.Println()
pushURL := createRepositoryAsync(gitHubToken, &repo)
if pushURL == "" {
fmt.Printf("Repo failed to create: %s", repo.Name)
fmt.Println()
return false
}
remote, err = gitRepo.CreateRemote(&config.RemoteConfig{
Name: "Destination",
URLs: []string{pushURL},
})
if err != nil {
fmt.Printf("Failed to create new remote: %s", repo.Name)
fmt.Println()
fmt.Println(err)
return false
}
refs := make([]config.RefSpec, 0)
refs = append(refs, config.RefSpec(fmt.Sprintf("%s:%s", "*", "*")))
err = remote.Push(&git.PushOptions{
RemoteName: "Destination",
RefSpecs: refs,
})
if err != nil {
errorString := err.Error()
if errorString == "authorization failed" {
fmt.Printf("Not allowed to access repository. Check permissions.: %s", repo.Name)
fmt.Println()
return false
} else if !strings.Contains(errorString, "deny updating a hidden ref") {
fmt.Printf("Failed to push repository: %s", repo.Name)
fmt.Println()
fmt.Println(err)
return false
}
}
fmt.Printf("Synced: %s", repo.Name)
fmt.Println()
return true
}