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/repository_setup.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/repository_setup.go')
| -rw-r--r-- | internal/sync/repository_setup.go | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/internal/sync/repository_setup.go b/internal/sync/repository_setup.go new file mode 100644 index 0000000..cddc022 --- /dev/null +++ b/internal/sync/repository_setup.go @@ -0,0 +1,94 @@ +package sync + +import ( + "fmt" + "os" + "os/exec" + "path/filepath" + + "codeberg.org/snonux/gitsyncer/internal/config" +) + +// setupRepository ensures the repository exists and all remotes are configured +func (s *Syncer) setupRepository(repoPath string) error { + if _, err := os.Stat(repoPath); os.IsNotExist(err) { + return s.setupNewRepository(repoPath) + } + return s.setupExistingRepository(repoPath) +} + +// setupNewRepository clones and configures a new repository +func (s *Syncer) setupNewRepository(repoPath string) error { + 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) + } + } + + return nil +} + +// setupExistingRepository ensures all remotes are configured for an existing repository +func (s *Syncer) setupExistingRepository(repoPath string) error { + 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) + } + } + } + + return nil +} + +// changeToRepoDirectory changes to the repository directory and returns a function to restore the original directory +func changeToRepoDirectory(repoPath string) (func(), error) { + originalDir, err := os.Getwd() + if err != nil { + return nil, fmt.Errorf("failed to get current directory: %w", err) + } + + if err := os.Chdir(repoPath); err != nil { + return nil, fmt.Errorf("failed to change to repository directory: %w", err) + } + + return func() { os.Chdir(originalDir) }, nil +} + +// getRemotesMap creates a map of remote names to organizations +func (s *Syncer) getRemotesMap() map[string]*config.Organization { + remotes := make(map[string]*config.Organization) + for i := range s.config.Organizations { + org := &s.config.Organizations[i] + remoteName := s.getRemoteName(org) + remotes[remoteName] = org + } + return remotes +}
\ No newline at end of file |
