diff options
| author | Paul Buetow <paul@buetow.org> | 2025-06-24 00:26:05 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-06-24 00:26:05 +0300 |
| commit | 16113b76309dcbae1a91f8420a0bbf10863c9675 (patch) | |
| tree | 243b2db64f1a64e2f89deda6eae0f052909709dc /internal/sync/branch_sync.go | |
| parent | e637f4fbb06b1c0661d2e77ce79d0d5149ac5c47 (diff) | |
refactor: break down large functions into smaller, focused ones
Major refactoring to improve code maintainability:
1. Split main.go (481 lines → 72 lines) into internal/cli package:
- flags.go: Command-line flag definitions and parsing
- handlers.go: General command handlers (version, config, list operations)
- sync_handlers.go: Sync-specific handlers for all sync operations
2. Refactored sync.go to extract logic into separate files:
- git_operations.go: Git command helpers (merge, push, fetch, etc.)
- repository_setup.go: Repository initialization and remote configuration
- branch_sync.go: Branch synchronization helpers
3. Reduced function sizes to meet 30-line guideline:
- syncBranch: 104 lines → 26 lines
- SyncRepository: 97 lines → 44 lines
- main(): 465 lines → 63 lines
- getAllBranches: 32 lines → 9 lines
All functionality remains the same, but the code is now more modular,
testable, and easier to understand. Each function has a single, clear
responsibility.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
Diffstat (limited to 'internal/sync/branch_sync.go')
| -rw-r--r-- | internal/sync/branch_sync.go | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/internal/sync/branch_sync.go b/internal/sync/branch_sync.go new file mode 100644 index 0000000..1bf8b79 --- /dev/null +++ b/internal/sync/branch_sync.go @@ -0,0 +1,68 @@ +package sync + +import ( + "fmt" + + "codeberg.org/snonux/gitsyncer/internal/config" +) + +// trackRemotesWithBranch finds which remotes have a specific branch +func (s *Syncer) trackRemotesWithBranch(branch string, remotes map[string]*config.Organization) map[string]bool { + remotesWithBranch := make(map[string]bool) + + for remoteName := range remotes { + if s.remoteBranchExists(remoteName, branch) { + remotesWithBranch[remoteName] = true + } + } + + return remotesWithBranch +} + +// mergeFromRemotes merges changes from all remotes that have the branch +func mergeFromRemotes(branch string, remotesWithBranch map[string]bool) error { + if len(remotesWithBranch) == 0 { + fmt.Printf(" Branch %s is local only, will push to all remotes\n", branch) + return nil + } + + // Merge changes from all remotes that have this branch + for remoteName := range remotesWithBranch { + if err := mergeBranch(remoteName, branch); err != nil { + return err + } + } + + return nil +} + +// pushToAllRemotes pushes the branch to all configured remotes +func pushToAllRemotes(branch string, remotes map[string]*config.Organization, remotesWithBranch map[string]bool) error { + for remoteName, org := range remotes { + // Check if this remote has the branch + remoteHasBranch := remotesWithBranch[remoteName] + + if !remoteHasBranch { + fmt.Printf(" Creating branch on %s (%s)...\n", remoteName, org.Host) + } else { + fmt.Printf(" Pushing to %s (%s)...\n", remoteName, org.Host) + } + + if err := pushBranch(remoteName, branch, remoteHasBranch); err != nil { + return err + } + } + + return nil +} + +// syncAllBranches synchronizes all branches across remotes +func (s *Syncer) syncAllBranches(branches []string, remotes map[string]*config.Organization) error { + 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) + } + } + return nil +}
\ No newline at end of file |
