-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitsync.go
277 lines (215 loc) · 6.45 KB
/
gitsync.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
)
var BuildVersion string
var BuildDate string
var GitRevision string
var GitDate string
var BuildUser string
type GitsyncConfiguration struct {
Sync []struct {
Source string `json:"source_remote"`
Target string `json:"target_remote"`
Branches []string `json:"branches"`
} `json:"sync"`
}
const gsStartupBanner string = "gitsync version %s built on %s by %s (git %s %s)\n"
const gsConfigFile string = ".gitsync.conf"
const gsConfigPathBanner string = "config path: %s\n"
const gsEndOfSync string = "gitsync has finished processing"
type GitsyncError string
const (
gsFatalErrorCwd GitsyncError = "can't get current working directory, Exiting..."
gsFatalErrorDirNotExist GitsyncError = "directory to work in does not exist. Exiting..."
gsFatalErrorConfigNotExist GitsyncError = "config file does not exist. Exiting..."
gsFatalErrorConfigStat GitsyncError = "could not stat config file. Exiting..."
gsFatalErrorInsecureConfig GitsyncError = "config file is not read only (r------). Exiting..."
gsFatalErrorUnreadableConfig GitsyncError = "could not read config file records. Exiting..."
gsFatalErrorInvalidJSON GitsyncError = "could not process config file. Invalid JSON? Exiting..."
)
var gitsyncConfig GitsyncConfiguration
var repoRemotes = map[string]string{}
var repoBranches = map[string]string{}
var pathToRepo string = ""
var debug bool = false
func debugPrintln(msg string) {
if debug {
log.Println(msg)
}
}
func debugPrintf(format string, args ...interface{}) {
if debug {
log.Printf(format, args...)
}
}
// Utility functions taken from go-git and lightly modified
// CheckArgs should be used to ensure the right command line arguments are
// passed before executing an example.
func CheckArgs(arg ...string) {
if len(os.Args) < len(arg)+1 {
debugPrintf("Usage: %s %s", os.Args[0], strings.Join(arg, " "))
os.Exit(1)
}
}
// CheckIfError should be used to naively panics if an error is not nil.
func CheckIfError(err error) {
if err == nil {
return
}
debugPrintf("error: %s", err)
os.Exit(1)
}
// End of utility functions taken from go-git and lightly modified
func checkSyncs() bool {
for _, sync := range gitsyncConfig.Sync {
if len(sync.Branches) >= 1 &&
len(sync.Source) > 1 &&
len(sync.Target) > 1 {
} else {
return false
}
}
return true
}
func getCwd() string {
cwd, err := os.Getwd()
if err != nil {
log.Fatal(gsFatalErrorCwd)
}
return cwd
}
func openRepoAtPath() *git.Repository {
repo, err := git.PlainOpen(pathToRepo)
CheckIfError(err)
return repo
}
func collectRepoInfo() {
repo := openRepoAtPath()
branches, err := repo.Branches()
CheckIfError(err)
err = branches.ForEach(func(b *plumbing.Reference) error {
repoBranches[b.Name().Short()] = b.Name().String()
return nil
})
CheckIfError(err)
remotes, err := repo.Remotes()
CheckIfError(err)
for _, remote := range remotes {
repoRemotes[remote.Config().Name] = remote.Config().Name
}
if debug {
log.Println("Repository branches:")
log.Println(repoBranches)
log.Println("Repository remotes:")
log.Println(repoRemotes)
}
}
func remoteExists(remote string) bool {
_, exists := repoRemotes[remote]
return exists
}
func branchExists(branch string) bool {
_, exists := repoBranches[branch]
return exists
}
func processSyncs() {
for _, sync := range gitsyncConfig.Sync {
var wouldFail = false
debugPrintf("syncing %d branches between %s and %s\n", len(sync.Branches), sync.Source, sync.Target)
if !remoteExists(sync.Source) {
debugPrintf("%s source remote doesn't exist\n", sync.Source)
wouldFail = true
}
if !remoteExists(sync.Target) {
debugPrintf("%s target remote doesn't exist\n", sync.Source)
wouldFail = true
}
for _, branch := range sync.Branches {
if !branchExists(branch) {
debugPrintf("%s branch doesn't exist\n", branch)
wouldFail = true
}
}
if wouldFail {
debugPrintln("Attempting this sync would fail, skipping...")
continue
}
debugPrintln("Processing sync")
repo := openRepoAtPath()
worktree, err := repo.Worktree()
CheckIfError(err)
for _, branch := range sync.Branches {
var branchRef = plumbing.NewBranchReferenceName(branch)
debugPrintf("checking out %s as %s\n", branch, branchRef)
worktree.Checkout(&git.CheckoutOptions{Branch: branchRef})
CheckIfError(err)
debugPrintf("pulling changes on %s from %s\n", branch, sync.Source)
worktree.Pull(&git.PullOptions{RemoteName: sync.Source, ReferenceName: branchRef, SingleBranch: true})
CheckIfError(err)
debugPrintf("pushing changes on %s to %s\n", branch, sync.Target)
repo.Push(&git.PushOptions{
RemoteName: sync.Target,
RefSpecs: []config.RefSpec{config.RefSpec(branchRef + ":" + branchRef)}})
CheckIfError(err)
}
}
}
func main() {
log.SetOutput(os.Stdout)
var configFile string
var printVersion bool
var allowInsecureConfig bool
flag.StringVar(&configFile, "config", gsConfigFile, "config file path")
flag.BoolVar(&printVersion, "version", false, "print version and build information and exit")
flag.BoolVar(&debug, "debug", false, "print debug information to stdout")
flag.BoolVar(&allowInsecureConfig, "insecure", false, "allow reading an insecure config file")
flag.StringVar(&pathToRepo, "repodir", getCwd(), "path to the git repository checkout you want to sync")
flag.Parse()
if printVersion {
fmt.Printf(gsStartupBanner, BuildVersion, BuildDate, BuildUser, GitRevision, GitDate)
os.Exit(0)
}
fmt.Printf(gsStartupBanner, BuildVersion, BuildDate, BuildUser, GitRevision, GitDate)
log.Printf(gsConfigPathBanner, configFile)
if _, err := os.ReadDir(pathToRepo); os.IsNotExist(err) {
log.Fatal(gsFatalErrorDirNotExist)
}
if _, err := os.Stat(configFile); os.IsNotExist(err) {
log.Fatal(gsFatalErrorConfigNotExist)
}
f, err := os.Lstat(configFile)
if err != nil {
log.Fatal(gsFatalErrorConfigStat)
}
if f.Mode() != 0400 {
if !allowInsecureConfig {
log.Fatal(gsFatalErrorInsecureConfig)
}
}
tuples, err := ioutil.ReadFile(configFile)
if err != nil {
log.Fatal(gsFatalErrorUnreadableConfig)
}
err = json.Unmarshal(tuples, &gitsyncConfig)
if err != nil {
debugPrintln(err.Error())
log.Fatal(gsFatalErrorInvalidJSON)
}
if checkSyncs() {
collectRepoInfo()
processSyncs()
log.Println(gsEndOfSync)
os.Exit(0)
}
os.Exit(1)
}