summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-06-24 00:26:05 +0300
committerPaul Buetow <paul@buetow.org>2025-06-24 00:26:05 +0300
commit16113b76309dcbae1a91f8420a0bbf10863c9675 (patch)
tree243b2db64f1a64e2f89deda6eae0f052909709dc /internal
parente637f4fbb06b1c0661d2e77ce79d0d5149ac5c47 (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')
-rw-r--r--internal/cli/flags.go55
-rw-r--r--internal/cli/handlers.go150
-rw-r--r--internal/cli/sync_handlers.go318
-rw-r--r--internal/sync/branch_sync.go68
-rw-r--r--internal/sync/git_operations.go186
-rw-r--r--internal/sync/repository_setup.go94
-rw-r--r--internal/sync/sync.go268
7 files changed, 923 insertions, 216 deletions
diff --git a/internal/cli/flags.go b/internal/cli/flags.go
new file mode 100644
index 0000000..c0320c5
--- /dev/null
+++ b/internal/cli/flags.go
@@ -0,0 +1,55 @@
+package cli
+
+import "flag"
+
+// Flags holds all command-line flag values
+type Flags struct {
+ VersionFlag bool
+ ConfigPath string
+ ListOrgs bool
+ ListRepos bool
+ SyncRepo string
+ SyncAll bool
+ SyncCodebergPublic bool
+ SyncGitHubPublic bool
+ FullSync bool
+ CreateGitHubRepos bool
+ CreateCodebergRepos bool
+ DryRun bool
+ WorkDir string
+ TestGitHubToken bool
+}
+
+// ParseFlags parses command-line flags and returns the flags struct
+func ParseFlags() *Flags {
+ f := &Flags{}
+
+ flag.BoolVar(&f.VersionFlag, "version", false, "print version information")
+ flag.BoolVar(&f.VersionFlag, "v", false, "print version information (short)")
+ flag.StringVar(&f.ConfigPath, "config", "", "path to configuration file")
+ flag.StringVar(&f.ConfigPath, "c", "", "path to configuration file (short)")
+ flag.BoolVar(&f.ListOrgs, "list-orgs", false, "list configured organizations")
+ flag.BoolVar(&f.ListRepos, "list-repos", false, "list configured repositories")
+ flag.StringVar(&f.SyncRepo, "sync", "", "repository name to sync")
+ flag.BoolVar(&f.SyncAll, "sync-all", false, "sync all configured repositories")
+ flag.BoolVar(&f.SyncCodebergPublic, "sync-codeberg-public", false, "sync all public Codeberg repositories to GitHub")
+ flag.BoolVar(&f.SyncGitHubPublic, "sync-github-public", false, "sync all public GitHub repositories to Codeberg")
+ flag.BoolVar(&f.FullSync, "full", false, "full bidirectional sync (enables --sync-codeberg-public --sync-github-public --create-github-repos --create-codeberg-repos)")
+ flag.BoolVar(&f.CreateGitHubRepos, "create-github-repos", false, "automatically create missing GitHub repositories")
+ flag.BoolVar(&f.CreateCodebergRepos, "create-codeberg-repos", false, "automatically create missing Codeberg repositories")
+ flag.BoolVar(&f.DryRun, "dry-run", false, "show what would be synced without actually syncing")
+ flag.StringVar(&f.WorkDir, "work-dir", ".gitsyncer-work", "working directory for cloning repositories")
+ flag.BoolVar(&f.TestGitHubToken, "test-github-token", false, "test GitHub token authentication")
+
+ flag.Parse()
+
+ // Handle --full flag by enabling all sync operations
+ if f.FullSync {
+ f.SyncCodebergPublic = true
+ f.SyncGitHubPublic = true
+ f.CreateGitHubRepos = true
+ f.CreateCodebergRepos = true
+ }
+
+ return f
+} \ No newline at end of file
diff --git a/internal/cli/handlers.go b/internal/cli/handlers.go
new file mode 100644
index 0000000..6ff6c4c
--- /dev/null
+++ b/internal/cli/handlers.go
@@ -0,0 +1,150 @@
+package cli
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "strings"
+
+ "codeberg.org/snonux/gitsyncer/internal/config"
+ "codeberg.org/snonux/gitsyncer/internal/github"
+ "codeberg.org/snonux/gitsyncer/internal/version"
+)
+
+// HandleVersion prints version information
+func HandleVersion() int {
+ fmt.Println(version.GetVersion())
+ return 0
+}
+
+// HandleTestGitHubToken tests GitHub token authentication
+func HandleTestGitHubToken() int {
+ fmt.Println("Testing GitHub token authentication...")
+ client := github.NewClient("", "snonux") // Empty token to trigger loading from env/file
+ if !client.HasToken() {
+ fmt.Println("ERROR: No GitHub token found!")
+ fmt.Println("Please set GITHUB_TOKEN environment variable or create ~/.gitsyncer_github_token file")
+ return 1
+ }
+
+ // Test the token by checking a known repo
+ exists, err := client.RepoExists("gitsyncer")
+ if err != nil {
+ fmt.Printf("ERROR: Token test failed: %v\n", err)
+ if strings.Contains(err.Error(), "401") {
+ fmt.Println("\nThe token is invalid or expired. Please check:")
+ fmt.Println("1. Token has not expired")
+ fmt.Println("2. Token has 'repo' scope")
+ fmt.Println("3. Token was not revoked")
+ }
+ return 1
+ }
+
+ fmt.Printf("SUCCESS: Token is valid! Repository check returned: %v\n", exists)
+ return 0
+}
+
+// LoadConfig loads configuration from the specified path or default locations
+func LoadConfig(configPath string) (*config.Config, error) {
+ if configPath == "" {
+ configPath = findDefaultConfigPath()
+ if configPath == "" {
+ return nil, fmt.Errorf("no configuration file found")
+ }
+ }
+
+ fmt.Printf("Loaded configuration from: %s\n", configPath)
+ return config.Load(configPath)
+}
+
+// findDefaultConfigPath searches for config file in default locations
+func findDefaultConfigPath() string {
+ home, err := os.UserHomeDir()
+ if err != nil {
+ return ""
+ }
+
+ // Check common config locations
+ configLocations := []string{
+ filepath.Join(".", "gitsyncer.json"),
+ filepath.Join(home, ".config", "gitsyncer", "config.json"),
+ filepath.Join(home, ".gitsyncer.json"),
+ }
+
+ for _, loc := range configLocations {
+ if _, err := os.Stat(loc); err == nil {
+ return loc
+ }
+ }
+
+ return ""
+}
+
+// ShowConfigHelp displays help for creating a configuration file
+func ShowConfigHelp() {
+ home, _ := os.UserHomeDir()
+
+ fmt.Println("No configuration file found. Please create one of:")
+ fmt.Printf(" - ./gitsyncer.json\n")
+ fmt.Printf(" - %s/.config/gitsyncer/config.json\n", home)
+ fmt.Printf(" - %s/.gitsyncer.json\n", home)
+ fmt.Println("\nOr specify a config file with --config flag")
+ fmt.Println("\nExample configuration:")
+ fmt.Println(`{
+ "organizations": [
+ {"host": "git@github.com", "name": "myorg"},
+ {"host": "git@codeberg.org", "name": "myorg"}
+ ],
+ "repositories": [
+ "repo1",
+ "repo2"
+ ]
+}`)
+}
+
+// HandleListOrgs lists configured organizations
+func HandleListOrgs(cfg *config.Config) int {
+ fmt.Println("\nConfigured organizations:")
+ for _, org := range cfg.Organizations {
+ fmt.Printf(" - %s\n", org.GetGitURL())
+ }
+ return 0
+}
+
+// HandleListRepos lists configured repositories
+func HandleListRepos(cfg *config.Config) int {
+ fmt.Println("\nConfigured repositories:")
+ if len(cfg.Repositories) == 0 {
+ fmt.Println(" (none configured)")
+ } else {
+ for _, repo := range cfg.Repositories {
+ fmt.Printf(" - %s\n", repo)
+ }
+ }
+ return 0
+}
+
+// ShowUsage displays the usage information
+func ShowUsage(cfg *config.Config) {
+ fmt.Println("\ngitsyncer - Git repository synchronization tool")
+ fmt.Printf("Configured with %d organization(s) and %d repository(ies)\n",
+ len(cfg.Organizations), len(cfg.Repositories))
+ fmt.Println("\nUsage:")
+ fmt.Println(" gitsyncer --sync <repo-name> Sync a specific repository")
+ fmt.Println(" gitsyncer --sync-all Sync all configured repositories")
+ fmt.Println(" gitsyncer --sync-codeberg-public Sync all public Codeberg repositories to GitHub")
+ fmt.Println(" gitsyncer --sync-github-public Sync all public GitHub repositories to Codeberg")
+ fmt.Println(" gitsyncer --full Full bidirectional sync of all public repos")
+ fmt.Println(" gitsyncer --list-orgs List configured organizations")
+ fmt.Println(" gitsyncer --list-repos List configured repositories")
+ fmt.Println(" gitsyncer --test-github-token Test GitHub token authentication")
+ fmt.Println(" gitsyncer --version Show version information")
+ fmt.Println("\nOptions:")
+ fmt.Println(" --config <path> Path to configuration file")
+ fmt.Println(" --work-dir <path> Working directory for operations (default: .gitsyncer-work)")
+ fmt.Println(" --create-github-repos Create missing GitHub repositories automatically")
+ fmt.Println(" --create-codeberg-repos Create missing Codeberg repositories (not yet implemented)")
+ fmt.Println(" --dry-run Show what would be done without doing it")
+ fmt.Println("\nGitHub Token:")
+ fmt.Println(" Set via: config file, GITHUB_TOKEN env var, or ~/.gitsyncer_github_token file")
+} \ No newline at end of file
diff --git a/internal/cli/sync_handlers.go b/internal/cli/sync_handlers.go
new file mode 100644
index 0000000..d14255e
--- /dev/null
+++ b/internal/cli/sync_handlers.go
@@ -0,0 +1,318 @@
+package cli
+
+import (
+ "fmt"
+ "log"
+ "strings"
+
+ "codeberg.org/snonux/gitsyncer/internal/codeberg"
+ "codeberg.org/snonux/gitsyncer/internal/config"
+ "codeberg.org/snonux/gitsyncer/internal/github"
+ "codeberg.org/snonux/gitsyncer/internal/sync"
+)
+
+// HandleSync handles syncing a single repository
+func HandleSync(cfg *config.Config, flags *Flags) int {
+ // If create-github-repos is enabled, create the repo if needed
+ if flags.CreateGitHubRepos {
+ if err := createGitHubRepoIfNeeded(cfg, flags.SyncRepo); err != nil {
+ fmt.Printf("ERROR: %v\n", err)
+ return 1
+ }
+ }
+
+ syncer := sync.New(cfg, flags.WorkDir)
+ if err := syncer.SyncRepository(flags.SyncRepo); err != nil {
+ log.Fatal("Sync failed:", err)
+ return 1
+ }
+ return 0
+}
+
+// HandleSyncAll handles syncing all configured repositories
+func HandleSyncAll(cfg *config.Config, flags *Flags) int {
+ if len(cfg.Repositories) == 0 {
+ fmt.Println("No repositories configured. Add repositories to the config file.")
+ return 1
+ }
+
+ // Initialize GitHub client if needed
+ var githubClient *github.Client
+ if flags.CreateGitHubRepos {
+ githubClient = initGitHubClient(cfg)
+ }
+
+ syncer := sync.New(cfg, flags.WorkDir)
+ successCount := 0
+
+ for i, repo := range cfg.Repositories {
+ fmt.Printf("\n[%d/%d] Syncing %s...\n", i+1, len(cfg.Repositories), repo)
+
+ // Create GitHub repo if needed
+ if githubClient != nil {
+ if err := createRepoWithClient(githubClient, repo, fmt.Sprintf("Mirror of %s", repo)); err != nil {
+ fmt.Printf("ERROR: Failed to create GitHub repo %s: %v\n", repo, err)
+ fmt.Printf("Stopping sync due to error.\n")
+ return 1
+ }
+ }
+
+ if err := syncer.SyncRepository(repo); err != nil {
+ fmt.Printf("ERROR: Failed to sync %s: %v\n", repo, err)
+ fmt.Printf("Stopping sync due to error.\n")
+ return 1
+ }
+ successCount++
+ }
+
+ fmt.Printf("\nSuccessfully synced all %d repositories!\n", successCount)
+ return 0
+}
+
+// HandleSyncCodebergPublic handles syncing all public Codeberg repositories
+func HandleSyncCodebergPublic(cfg *config.Config, flags *Flags) int {
+ codebergOrg := cfg.FindCodebergOrg()
+ if codebergOrg == nil {
+ fmt.Println("No Codeberg organization found in configuration")
+ return 1
+ }
+
+ fmt.Printf("Fetching public repositories from Codeberg user/org: %s...\n", codebergOrg.Name)
+
+ client := codeberg.NewClient(codebergOrg.Name)
+
+ // Try fetching as organization first, then as user
+ repos, err := client.ListPublicRepos()
+ if err != nil {
+ fmt.Println("Trying as user account...")
+ repos, err = client.ListUserPublicRepos()
+ if err != nil {
+ log.Fatal("Failed to fetch repositories:", err)
+ }
+ }
+
+ repoNames := codeberg.GetRepoNames(repos)
+ fmt.Printf("Found %d public repositories on Codeberg\n", len(repoNames))
+
+ if len(repoNames) == 0 {
+ fmt.Println("No public repositories found")
+ return 0
+ }
+
+ // Show the repositories that will be synced
+ showReposToSync(repoNames)
+
+ if flags.DryRun {
+ fmt.Printf("\n[DRY RUN] Would sync %d repositories from Codeberg to GitHub\n", len(repoNames))
+ if flags.CreateGitHubRepos {
+ fmt.Println("Would create missing GitHub repositories")
+ }
+ if !flags.SyncGitHubPublic {
+ return 0
+ }
+ }
+
+ if !flags.DryRun {
+ return syncCodebergRepos(cfg, flags, repos, repoNames)
+ }
+
+ return 0
+}
+
+// HandleSyncGitHubPublic handles syncing all public GitHub repositories
+func HandleSyncGitHubPublic(cfg *config.Config, flags *Flags) int {
+ githubOrg := cfg.FindGitHubOrg()
+ if githubOrg == nil {
+ fmt.Println("No GitHub organization found in configuration")
+ return 1
+ }
+
+ fmt.Printf("Fetching public repositories from GitHub user/org: %s...\n", githubOrg.Name)
+
+ client := github.NewClient(githubOrg.GitHubToken, githubOrg.Name)
+ if !client.HasToken() {
+ fmt.Println("ERROR: GitHub token required to list repositories")
+ fmt.Println("Set GITHUB_TOKEN env var or create ~/.gitsyncer_github_token file")
+ return 1
+ }
+
+ repos, err := client.ListPublicRepos()
+ if err != nil {
+ log.Fatal("Failed to fetch repositories:", err)
+ }
+
+ repoNames := github.GetRepoNames(repos)
+ fmt.Printf("Found %d public repositories on GitHub\n", len(repoNames))
+
+ if len(repoNames) == 0 {
+ fmt.Println("No public repositories found")
+ return 0
+ }
+
+ // Show the repositories that will be synced
+ showReposToSync(repoNames)
+
+ if flags.DryRun {
+ fmt.Printf("\n[DRY RUN] Would sync %d repositories from GitHub to Codeberg\n", len(repoNames))
+ if flags.CreateCodebergRepos {
+ fmt.Println("Would create missing Codeberg repositories")
+ }
+ return 0
+ }
+
+ if !flags.DryRun {
+ return syncGitHubRepos(cfg, flags, repos, repoNames)
+ }
+
+ return 0
+}
+
+// Helper functions
+
+func createGitHubRepoIfNeeded(cfg *config.Config, repoName string) error {
+ githubOrg := cfg.FindGitHubOrg()
+ if githubOrg == nil {
+ return nil
+ }
+
+ fmt.Printf("Initializing GitHub client for organization: %s\n", githubOrg.Name)
+ githubClient := github.NewClient(githubOrg.GitHubToken, githubOrg.Name)
+ if !githubClient.HasToken() {
+ fmt.Println("Warning: No GitHub token found. Cannot create repository.")
+ return nil
+ }
+
+ fmt.Println("Checking/creating GitHub repository...")
+ return githubClient.CreateRepo(repoName, fmt.Sprintf("Mirror of %s", repoName), false)
+}
+
+func initGitHubClient(cfg *config.Config) *github.Client {
+ githubOrg := cfg.FindGitHubOrg()
+ if githubOrg == nil {
+ fmt.Println("Warning: --create-github-repos specified but no GitHub organization found in config")
+ return nil
+ }
+
+ fmt.Printf("Initializing GitHub client for organization: %s\n", githubOrg.Name)
+ githubClient := github.NewClient(githubOrg.GitHubToken, githubOrg.Name)
+ if !githubClient.HasToken() {
+ fmt.Println("Warning: No GitHub token found. Cannot create repositories.")
+ return nil
+ }
+
+ fmt.Println("GitHub client initialized successfully with token")
+ return githubClient
+}
+
+func createRepoWithClient(client *github.Client, repoName, description string) error {
+ fmt.Printf("Checking/creating GitHub repository %s...\n", repoName)
+ return client.CreateRepo(repoName, description, false)
+}
+
+func showReposToSync(repoNames []string) {
+ fmt.Println("\nRepositories to sync:")
+ for _, name := range repoNames {
+ fmt.Printf(" - %s\n", name)
+ }
+}
+
+func printFullSyncSeparator() {
+ fmt.Println("\n" + strings.Repeat("=", 70))
+ fmt.Println("=== Continuing with GitHub to Codeberg sync ===")
+ fmt.Println(strings.Repeat("=", 70) + "\n")
+}
+
+func syncCodebergRepos(cfg *config.Config, flags *Flags, repos []codeberg.Repository, repoNames []string) int {
+ // Initialize GitHub client if needed
+ var githubClient *github.Client
+ if flags.CreateGitHubRepos {
+ githubClient = initGitHubClient(cfg)
+ }
+
+ fmt.Printf("\nStarting sync of %d repositories...\n", len(repoNames))
+
+ syncer := sync.New(cfg, flags.WorkDir)
+ successCount := 0
+
+ // Create map for descriptions
+ repoMap := make(map[string]codeberg.Repository)
+ for _, repo := range repos {
+ repoMap[repo.Name] = repo
+ }
+
+ for i, repoName := range repoNames {
+ fmt.Printf("\n[%d/%d] Syncing %s...\n", i+1, len(repoNames), repoName)
+
+ // Create GitHub repo if needed
+ if githubClient != nil && flags.CreateGitHubRepos {
+ codebergRepo := repoMap[repoName]
+ description := codebergRepo.Description
+ if description == "" {
+ description = fmt.Sprintf("Mirror of %s from Codeberg", repoName)
+ }
+
+ fmt.Printf("Checking/creating GitHub repository %s...\n", repoName)
+ err := githubClient.CreateRepo(repoName, description, false)
+ if err != nil {
+ fmt.Printf("Warning: Failed to create GitHub repo %s: %v\n", repoName, err)
+ }
+ }
+
+ if err := syncer.SyncRepository(repoName); err != nil {
+ fmt.Printf("ERROR: Failed to sync %s: %v\n", repoName, err)
+ fmt.Printf("Stopping sync due to error.\n")
+ return 1
+ }
+ successCount++
+ }
+
+ fmt.Printf("\n=== Summary ===\n")
+ fmt.Printf("Successfully synced: %d repositories\n", successCount)
+
+ if !flags.SyncGitHubPublic {
+ return 0
+ }
+
+ // Print separator for full sync
+ printFullSyncSeparator()
+ return 0
+}
+
+func syncGitHubRepos(cfg *config.Config, flags *Flags, repos []github.Repository, repoNames []string) int {
+ // TODO: Add Codeberg API client for repo creation
+ if flags.CreateCodebergRepos {
+ fmt.Println("WARNING: --create-codeberg-repos is not yet implemented")
+ fmt.Println(" Repositories must exist on Codeberg before syncing")
+ }
+
+ fmt.Printf("\nStarting sync of %d repositories...\n", len(repoNames))
+
+ syncer := sync.New(cfg, flags.WorkDir)
+ successCount := 0
+
+ for i, repoName := range repoNames {
+ fmt.Printf("\n[%d/%d] Syncing %s...\n", i+1, len(repoNames), repoName)
+
+ if err := syncer.SyncRepository(repoName); err != nil {
+ fmt.Printf("ERROR: Failed to sync %s: %v\n", repoName, err)
+ fmt.Printf("Stopping sync due to error.\n")
+ return 1
+ }
+ successCount++
+ }
+
+ fmt.Printf("\n=== Summary ===\n")
+ fmt.Printf("Successfully synced: %d repositories\n", successCount)
+
+ return 0
+}
+
+// ShowFullSyncMessage displays the full sync mode message
+func ShowFullSyncMessage() {
+ fmt.Println("Full sync mode enabled:")
+ fmt.Println(" - Sync all public Codeberg repos to GitHub")
+ fmt.Println(" - Sync all public GitHub repos to Codeberg")
+ fmt.Println(" - Create missing GitHub repositories")
+ fmt.Println(" - Create missing Codeberg repositories (when implemented)")
+ fmt.Println()
+} \ No newline at end of file
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