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 | |
| 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')
| -rw-r--r-- | internal/sync/branch_sync.go | 68 | ||||
| -rw-r--r-- | internal/sync/git_operations.go | 186 | ||||
| -rw-r--r-- | internal/sync/repository_setup.go | 94 | ||||
| -rw-r--r-- | internal/sync/sync.go | 268 |
4 files changed, 400 insertions, 216 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 diff --git a/internal/sync/git_operations.go b/internal/sync/git_operations.go new file mode 100644 index 0000000..dab573f --- /dev/null +++ b/internal/sync/git_operations.go @@ -0,0 +1,186 @@ +package sync + +import ( + "fmt" + "os/exec" + "strings" +) + +// checkForMergeConflicts checks if the repository has merge conflicts +func checkForMergeConflicts() (bool, string, error) { + cmd := exec.Command("git", "status", "--porcelain") + output, err := cmd.Output() + if err != nil { + return false, "", err + } + + statusStr := string(output) + hasConflicts := strings.Contains(statusStr, "UU ") || + strings.Contains(statusStr, "AA ") || + strings.Contains(statusStr, "DD ") + + return hasConflicts, statusStr, nil +} + +// stashChanges stashes uncommitted changes +func stashChanges() error { + fmt.Println(" Stashing uncommitted changes...") + return exec.Command("git", "stash", "push", "-m", "gitsyncer-auto-stash").Run() +} + +// popStash attempts to pop the stash (used in defer) +func popStash() { + exec.Command("git", "stash", "pop").Run() +} + +// mergeBranch merges a branch from a remote +func mergeBranch(remoteName, branch string) error { + 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)) + } + + return nil +} + +// pushBranch pushes a branch to a remote +func pushBranch(remoteName, branch string, remoteHasBranch bool) error { + 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 isRepositoryMissing(outputStr) { + fmt.Printf(" Note: Remote repository %s does not exist - must be created manually\n", remoteName) + fmt.Printf(" Skipping push to %s\n", remoteName) + return nil // Not an error, just skip + } + + // Check if it's because the branch doesn't exist on the remote + if isBranchMissing(outputStr) { + 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) + } + return nil + } + + return fmt.Errorf("failed to push to %s: %w\n%s", remoteName, err, outputStr) + } + + if !remoteHasBranch { + fmt.Printf(" Successfully created branch %s on %s\n", branch, remoteName) + } + + return nil +} + +// isRepositoryMissing checks if the error indicates a missing repository +func isRepositoryMissing(output string) bool { + return strings.Contains(output, "does not appear to be a git repository") || + strings.Contains(output, "Could not read from remote repository") +} + +// isBranchMissing checks if the error indicates a missing branch +func isBranchMissing(output string) bool { + return strings.Contains(output, "error: src refspec") +} + +// getRemotesList extracts unique remote names from git remote -v output +func getRemotesList() (map[string]bool, error) { + cmd := exec.Command("git", "remote", "-v") + output, err := cmd.Output() + if err != nil { + return nil, fmt.Errorf("failed to list remotes: %w", err) + } + + 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 + } + } + + return remotes, nil +} + +// fetchRemote fetches from a single remote with error handling +func fetchRemote(remote string) error { + 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 isRepositoryMissing(string(output)) { + fmt.Printf(" Warning: Remote repository %s does not exist yet\n", remote) + return nil // Not an error, just skip + } + return fmt.Errorf("failed to fetch from %s: %w\n%s", remote, err, string(output)) + } + return nil +} + +// checkoutExistingBranch tries to checkout an existing branch +func checkoutExistingBranch(branch string) error { + cmd := exec.Command("git", "checkout", branch) + output, err := cmd.CombinedOutput() + if err != nil { + fmt.Printf(" Initial checkout failed: %s\n", strings.TrimSpace(string(output))) + return err + } + return nil +} + +// createTrackingBranch creates a new branch tracking a remote branch +func createTrackingBranch(branch, remoteName string) error { + cmd := exec.Command("git", "checkout", "-b", branch, fmt.Sprintf("%s/%s", remoteName, branch)) + output, err := cmd.CombinedOutput() + if err != nil { + return fmt.Errorf("failed to create tracking branch: %s", string(output)) + } + return nil +} + +// getAllUniqueBranches extracts unique branch names from git branch -r output +func getAllUniqueBranches(output []byte) []string { + 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 +}
\ No newline at end of file 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 diff --git a/internal/sync/sync.go b/internal/sync/sync.go index e34a142..c7f17e0 100644 --- a/internal/sync/sync.go +++ b/internal/sync/sync.go @@ -35,71 +35,18 @@ func (s *Syncer) SyncRepository(repoName string) error { 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 + // Setup repository (clone or ensure remotes are configured) 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) - } - } - } + if err := s.setupRepository(repoPath); err != nil { + return err } // Change to repository directory - originalDir, err := os.Getwd() + restoreDir, err := changeToRepoDirectory(repoPath) 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) + return err } + defer restoreDir() // Fetch all remotes fmt.Printf("Fetching updates from all remotes...\n") @@ -113,12 +60,12 @@ func (s *Syncer) SyncRepository(repoName string) error { 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) - } + // Get remotes map + remotes := s.getRemotesMap() + + // Sync all branches + if err := s.syncAllBranches(branches, remotes); err != nil { + return err } fmt.Printf("\nRepository %s synchronized successfully!\n", repoName) @@ -175,39 +122,16 @@ func (s *Syncer) addRemote(repoPath string, org *config.Organization) error { // fetchAll fetches from all remotes // Note: We use individual fetches instead of --all to handle missing repositories gracefully func (s *Syncer) fetchAll() error { - // First, check which remotes actually exist - cmd := exec.Command("git", "remote", "-v") - output, err := cmd.Output() + // Get list of remotes + remotes, err := getRemotesList() 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 - } + return err } // 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)) + if err := fetchRemote(remote); err != nil { + return err } } @@ -222,57 +146,18 @@ func (s *Syncer) getAllBranches() ([]string, error) { 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 + return getAllUniqueBranches(output), nil } // syncBranch synchronizes a specific branch across all remotes func (s *Syncer) syncBranch(branch string, remotes map[string]*config.Organization) error { - // First check if we have unresolved merge conflicts - cmd := exec.Command("git", "status", "--porcelain") - output, err := cmd.Output() - if err == nil && len(output) > 0 { - // Check for merge conflicts - statusStr := string(output) - if strings.Contains(statusStr, "UU ") || strings.Contains(statusStr, "AA ") || strings.Contains(statusStr, "DD ") { - // Get absolute path for clarity - absPath, err := filepath.Abs(s.workDir) - if err != nil { - absPath = s.workDir - } - return fmt.Errorf("repository has unresolved merge conflicts\nPlease resolve conflicts in: %s\nOr delete the directory to start fresh: rm -rf %s", absPath, absPath) - } - // If we have uncommitted changes but no conflicts, try to stash them - fmt.Println(" Stashing uncommitted changes...") - if err := exec.Command("git", "stash", "push", "-m", "gitsyncer-auto-stash").Run(); err != nil { - return fmt.Errorf("failed to stash changes: %w", err) - } - defer func() { - // Try to pop the stash at the end - exec.Command("git", "stash", "pop").Run() - }() + // Handle merge conflicts and uncommitted changes + stashed, err := s.handleWorkingDirectoryState() + if err != nil { + return err + } + if stashed { + defer popStash() } // Create or checkout the branch @@ -281,91 +166,47 @@ func (s *Syncer) syncBranch(branch string, remotes map[string]*config.Organizati } // Track which remotes have this branch - remotesWithBranch := make(map[string]bool) + remotesWithBranch := s.trackRemotesWithBranch(branch, remotes) - // Check which remotes have this branch - for remoteName := range remotes { - if s.remoteBranchExists(remoteName, branch) { - remotesWithBranch[remoteName] = true - } - } - - // If no remotes have this branch, it means it's a local branch that needs to be pushed - if len(remotesWithBranch) == 0 { - fmt.Printf(" Branch %s is local only, will push to all remotes\n", branch) - } else { - // 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)) - } - } + // Merge changes from remotes + if err := mergeFromRemotes(branch, remotesWithBranch); err != nil { + return err } // Push to all remotes - 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) - } - - cmd := exec.Command("git", "push", remoteName, branch) - output, err := cmd.CombinedOutput() + return pushToAllRemotes(branch, remotes, remotesWithBranch) +} +// handleWorkingDirectoryState checks for conflicts and stashes changes if needed +// Returns true if changes were stashed +func (s *Syncer) handleWorkingDirectoryState() (bool, error) { + hasConflicts, statusStr, err := checkForMergeConflicts() + if err != nil || statusStr == "" { + return false, nil + } + + if hasConflicts { + // Get absolute path for clarity + absPath, err := filepath.Abs(s.workDir) 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 - must be created manually\n", remoteName) - fmt.Printf(" Skipping push to %s\n", remoteName) - continue - } - // Check if it's because the branch doesn't exist on the remote - // This shouldn't happen with our logic, but keep it as a fallback - 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) - } - } else if !remoteHasBranch { - fmt.Printf(" Successfully created branch %s on %s\n", branch, remoteName) + absPath = s.workDir } + return false, fmt.Errorf("repository has unresolved merge conflicts\nPlease resolve conflicts in: %s\nOr delete the directory to start fresh: rm -rf %s", absPath, absPath) } - - return nil + + // If we have uncommitted changes but no conflicts, try to stash them + if err := stashChanges(); err != nil { + return false, fmt.Errorf("failed to stash changes: %w", err) + } + return true, 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) - output, err := cmd.CombinedOutput() - if err == nil { + if err := checkoutExistingBranch(branch); err == nil { return nil } - - // If checkout failed, check the error - outputStr := string(output) - fmt.Printf(" Initial checkout failed: %s\n", strings.TrimSpace(outputStr)) // If that fails, create a new branch tracking the first remote that has it for i := range s.config.Organizations { @@ -373,12 +214,7 @@ func (s *Syncer) checkoutBranch(branch string) error { remoteName := s.getRemoteName(org) if s.remoteBranchExists(remoteName, branch) { - cmd = exec.Command("git", "checkout", "-b", branch, fmt.Sprintf("%s/%s", remoteName, branch)) - output, err := cmd.CombinedOutput() - if err != nil { - return fmt.Errorf("failed to create tracking branch: %s", string(output)) - } - return nil + return createTrackingBranch(branch, remoteName) } } |
