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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
package sync
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/paul/gitsyncer/internal/config"
)
// Syncer handles repository synchronization between organizations
type Syncer struct {
config *config.Config
workDir string
repoName string
}
// New creates a new Syncer instance
func New(cfg *config.Config, workDir string) *Syncer {
return &Syncer{
config: cfg,
workDir: workDir,
}
}
// SyncRepository synchronizes a repository across all configured organizations
func (s *Syncer) SyncRepository(repoName string) error {
s.repoName = repoName
// Create work directory if it doesn't exist
if err := os.MkdirAll(s.workDir, 0755); err != nil {
return fmt.Errorf("failed to create work directory: %w", err)
}
// Get all remotes
remotes := make(map[string]*config.Organization)
for i := range s.config.Organizations {
org := &s.config.Organizations[i]
remoteName := s.getRemoteName(org)
remotes[remoteName] = org
}
// Clone or update the repository
repoPath := filepath.Join(s.workDir, repoName)
if _, err := os.Stat(repoPath); os.IsNotExist(err) {
// Clone from the first organization
if len(s.config.Organizations) == 0 {
return fmt.Errorf("no organizations configured")
}
firstOrg := &s.config.Organizations[0]
if err := s.cloneRepository(firstOrg, repoPath); err != nil {
return fmt.Errorf("failed to clone repository: %w", err)
}
// Rename origin to the proper remote name
firstRemoteName := s.getRemoteName(firstOrg)
cmd := exec.Command("git", "-C", repoPath, "remote", "rename", "origin", firstRemoteName)
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to rename origin remote: %w", err)
}
// Add other organizations as remotes
for i := 1; i < len(s.config.Organizations); i++ {
org := &s.config.Organizations[i]
if err := s.addRemote(repoPath, org); err != nil {
return fmt.Errorf("failed to add remote %s: %w", s.getRemoteName(org), err)
}
}
} else {
// Repository exists, ensure all remotes are configured
fmt.Printf("Using existing repository at %s\n", repoPath)
// Check and add any missing remotes
for i := range s.config.Organizations {
org := &s.config.Organizations[i]
remoteName := s.getRemoteName(org)
// Check if remote exists
cmd := exec.Command("git", "-C", repoPath, "remote", "get-url", remoteName)
if err := cmd.Run(); err != nil {
// Remote doesn't exist, add it
if err := s.addRemote(repoPath, org); err != nil {
return fmt.Errorf("failed to add remote %s: %w", remoteName, err)
}
}
}
}
// Change to repository directory
originalDir, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get current directory: %w", err)
}
defer os.Chdir(originalDir)
if err := os.Chdir(repoPath); err != nil {
return fmt.Errorf("failed to change to repository directory: %w", err)
}
// Fetch all remotes
fmt.Printf("Fetching updates from all remotes...\n")
if err := s.fetchAll(); err != nil {
return fmt.Errorf("failed to fetch remotes: %w", err)
}
// Get all branches
branches, err := s.getAllBranches()
if err != nil {
return fmt.Errorf("failed to get branches: %w", err)
}
// Sync each branch
for _, branch := range branches {
fmt.Printf("\nSyncing branch: %s\n", branch)
if err := s.syncBranch(branch, remotes); err != nil {
return fmt.Errorf("failed to sync branch %s: %w", branch, err)
}
}
fmt.Printf("\nRepository %s synchronized successfully!\n", repoName)
return nil
}
// cloneRepository clones a repository from an organization
func (s *Syncer) cloneRepository(org *config.Organization, repoPath string) error {
// For file:// URLs, we need special handling
var cloneURL string
if strings.HasPrefix(org.Host, "file://") {
// For local file paths, the format is: file:///path/to/repo.git
cloneURL = fmt.Sprintf("%s/%s.git", org.Host, s.repoName)
} else {
// For SSH URLs, the format is: git@host:org/repo.git
cloneURL = fmt.Sprintf("%s/%s.git", org.GetGitURL(), s.repoName)
}
fmt.Printf("Cloning from %s...\n", cloneURL)
cmd := exec.Command("git", "clone", cloneURL, repoPath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}
return nil
}
// addRemote adds a remote to the repository
func (s *Syncer) addRemote(repoPath string, org *config.Organization) error {
remoteName := s.getRemoteName(org)
// For file:// URLs, we need special handling
var remoteURL string
if strings.HasPrefix(org.Host, "file://") {
remoteURL = fmt.Sprintf("%s/%s.git", org.Host, s.repoName)
} else {
remoteURL = fmt.Sprintf("%s/%s.git", org.GetGitURL(), s.repoName)
}
fmt.Printf("Adding remote %s: %s\n", remoteName, remoteURL)
cmd := exec.Command("git", "-C", repoPath, "remote", "add", remoteName, remoteURL)
if err := cmd.Run(); err != nil {
return err
}
return nil
}
// fetchAll fetches from all remotes
func (s *Syncer) fetchAll() error {
// First, check which remotes actually exist
cmd := exec.Command("git", "remote", "-v")
output, err := cmd.Output()
if err != nil {
return fmt.Errorf("failed to list remotes: %w", err)
}
// Try to fetch from each remote individually to handle missing repos
remotes := make(map[string]bool)
lines := strings.Split(string(output), "\n")
for _, line := range lines {
if line == "" {
continue
}
parts := strings.Fields(line)
if len(parts) >= 1 {
remotes[parts[0]] = true
}
}
// Fetch from each remote
for remote := range remotes {
fmt.Printf("Fetching %s\n", remote)
cmd := exec.Command("git", "fetch", remote, "--prune")
output, err := cmd.CombinedOutput()
if err != nil {
// Check if it's because the repository doesn't exist
if strings.Contains(string(output), "does not appear to be a git repository") ||
strings.Contains(string(output), "Could not read from remote repository") {
fmt.Printf(" Warning: Remote repository %s does not exist yet\n", remote)
continue
}
return fmt.Errorf("failed to fetch from %s: %w\n%s", remote, err, string(output))
}
}
return nil
}
// getAllBranches gets all unique branches from all remotes
func (s *Syncer) getAllBranches() ([]string, error) {
cmd := exec.Command("git", "branch", "-r")
output, err := cmd.Output()
if err != nil {
return nil, err
}
branchMap := make(map[string]bool)
lines := strings.Split(string(output), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" || strings.Contains(line, "->") {
continue
}
// Extract branch name from remote/branch format
parts := strings.SplitN(line, "/", 2)
if len(parts) == 2 {
branch := parts[1]
branchMap[branch] = true
}
}
// Convert map to slice
branches := make([]string, 0, len(branchMap))
for branch := range branchMap {
branches = append(branches, branch)
}
return branches, nil
}
// syncBranch synchronizes a specific branch across all remotes
func (s *Syncer) syncBranch(branch string, remotes map[string]*config.Organization) error {
// Create or checkout the branch
if err := s.checkoutBranch(branch); err != nil {
return fmt.Errorf("failed to checkout branch %s: %w", branch, err)
}
// Track which remotes have this branch
remotesWithBranch := make(map[string]bool)
// Check which remotes have this branch
for remoteName := range remotes {
if s.remoteBranchExists(remoteName, branch) {
remotesWithBranch[remoteName] = true
}
}
// If no remotes have this branch, skip it
if len(remotesWithBranch) == 0 {
fmt.Printf(" Branch %s not found on any remote, skipping\n", branch)
return nil
}
// Merge changes from all remotes that have this branch
for remoteName := range remotesWithBranch {
fmt.Printf(" Merging from %s/%s...\n", remoteName, branch)
cmd := exec.Command("git", "merge", fmt.Sprintf("%s/%s", remoteName, branch), "--no-edit")
output, err := cmd.CombinedOutput()
if err != nil {
// Check if it's a merge conflict
if strings.Contains(string(output), "CONFLICT") {
return fmt.Errorf("merge conflict detected when merging %s/%s. Please resolve manually", remoteName, branch)
}
return fmt.Errorf("failed to merge %s/%s: %w\n%s", remoteName, branch, err, string(output))
}
}
// Push to all remotes
for remoteName, org := range remotes {
fmt.Printf(" Pushing to %s (%s)...\n", remoteName, org.Host)
cmd := exec.Command("git", "push", remoteName, branch)
output, err := cmd.CombinedOutput()
if err != nil {
outputStr := string(output)
// Check if it's because the repository doesn't exist
if strings.Contains(outputStr, "does not appear to be a git repository") ||
strings.Contains(outputStr, "Could not read from remote repository") {
fmt.Printf(" Note: Remote repository %s does not exist - creating it first would be needed\n", remoteName)
fmt.Printf(" Skipping push to %s (repository must be created manually)\n", remoteName)
continue
}
// Check if it's because the branch doesn't exist on the remote
if strings.Contains(outputStr, "error: src refspec") {
fmt.Printf(" Creating new branch on %s\n", remoteName)
// Try again with -u flag to set upstream
cmd = exec.Command("git", "push", "-u", remoteName, branch)
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to push to %s: %w", remoteName, err)
}
} else {
return fmt.Errorf("failed to push to %s: %w\n%s", remoteName, err, outputStr)
}
}
}
return nil
}
// checkoutBranch checks out a branch, creating it if necessary
func (s *Syncer) checkoutBranch(branch string) error {
// First try to checkout existing branch
cmd := exec.Command("git", "checkout", branch)
if err := cmd.Run(); err == nil {
return nil
}
// If that fails, create a new branch tracking the first remote that has it
for i := range s.config.Organizations {
org := &s.config.Organizations[i]
remoteName := s.getRemoteName(org)
if s.remoteBranchExists(remoteName, branch) {
cmd = exec.Command("git", "checkout", "-b", branch, fmt.Sprintf("%s/%s", remoteName, branch))
return cmd.Run()
}
}
return fmt.Errorf("branch %s not found on any remote", branch)
}
// remoteBranchExists checks if a branch exists on a remote
func (s *Syncer) remoteBranchExists(remoteName, branch string) bool {
cmd := exec.Command("git", "branch", "-r", "--list", fmt.Sprintf("%s/%s", remoteName, branch))
output, err := cmd.Output()
if err != nil {
return false
}
return strings.TrimSpace(string(output)) != ""
}
// getRemoteName generates a remote name for an organization
func (s *Syncer) getRemoteName(org *config.Organization) string {
// Use the host without git@ or file:// prefix as remote name
host := org.Host
host = strings.TrimPrefix(host, "git@")
host = strings.TrimPrefix(host, "file://")
host = strings.ReplaceAll(host, ":", "_")
host = strings.ReplaceAll(host, ".", "_")
host = strings.ReplaceAll(host, "/", "_")
// For file URLs, create a simpler name
if strings.HasPrefix(org.Host, "file://") {
// Get the last part of the path
parts := strings.Split(strings.TrimPrefix(org.Host, "file://"), "/")
if len(parts) > 0 {
return parts[len(parts)-1]
}
}
return host
}
|