Skip to content

Commit

Permalink
feat: ✨ allow different clone type options
Browse files Browse the repository at this point in the history
  • Loading branch information
AkashRajpurohit committed Oct 1, 2024
1 parent 643f0c9 commit 9a3e9fe
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
6 changes: 6 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var rootCmd = &cobra.Command{
Workspace: "",
Cron: "",
BackupDir: config.GetBackupDir(backupDir),
CloneType: "bare",
}

err = config.SaveConfig(cfg, cfgFile)
Expand All @@ -70,6 +71,11 @@ var rootCmd = &cobra.Command{
cfg.Cron = cron
}

// If no clone_type is not set in the config file, set it to bare
if cfg.CloneType == "" {
cfg.CloneType = "bare"
}

logger.Info("Config loaded from: ", config.GetConfigFile(cfgFile))
logger.Info("Validating config ⏳")

Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Config struct {
BackupDir string `mapstructure:"backup_dir"`
Workspace string `mapstructure:"workspace"`
Cron string `mapstructure:"cron"`
CloneType string `mapstructure:"clone_type"`
}

func expandPath(path string) string {
Expand Down Expand Up @@ -105,6 +106,7 @@ func SaveConfig(config Config, cfgFile string) error {
viper.Set("server", config.Server)
viper.Set("workspace", config.Workspace)
viper.Set("cron", config.Cron)
viper.Set("clone_type", config.CloneType)

return viper.WriteConfig()
}
5 changes: 5 additions & 0 deletions pkg/config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,10 @@ func ValidateConfig(cfg Config) error {
}
}

// validate that clone_type can only be `bare`, `full`, `mirror` or `shallow`
if cfg.CloneType != "bare" && cfg.CloneType != "full" && cfg.CloneType != "mirror" && cfg.CloneType != "shallow" {
return fmt.Errorf("clone_type can only be `bare`, `full`, `mirror` or `shallow`")
}

return nil
}
46 changes: 44 additions & 2 deletions pkg/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,56 @@ func getBaseDirectoryPath(repoOwner, repoName string, config config.Config) stri
return filepath.Join(config.BackupDir, repoOwner, repoName)
}

func getGitCloneCommand(CloneType, repoPath, repoURL string) *exec.Cmd {
switch CloneType {
case "bare":
logger.Debugf("Cloning repo with bare clone type: %s", repoURL)
return exec.Command("git", "clone", "--bare", repoURL, repoPath)
case "full":
logger.Debugf("Cloning repo with full clone type: %s", repoURL)
return exec.Command("git", "clone", repoURL, repoPath)
case "mirror":
logger.Debugf("Cloning repo with mirror clone type: %s", repoURL)
return exec.Command("git", "clone", "--mirror", repoURL, repoPath)
case "shallow":
logger.Debugf("Cloning repo with shallow clone type: %s", repoURL)
return exec.Command("git", "clone", "--depth", "1", repoURL, repoPath)
default:
logger.Debugf("[Default] Cloning repo with bare clone type: %s", repoURL)
return exec.Command("git", "clone", "--bare", repoURL, repoPath)
}
}

func getGitFetchCommand(CloneType, repoPath string) *exec.Cmd {
switch CloneType {
case "bare":
logger.Debugf("Updating repo with bare clone type: %s", repoPath)
return exec.Command("git", "--git-dir", repoPath, "fetch", "--prune", "origin", "+*:*")
case "full":
logger.Debugf("Updating repo with full clone type: %s", repoPath)
return exec.Command("git", "-C", repoPath, "pull", "--prune")
case "mirror":
logger.Debugf("Updating repo with mirror clone type: %s", repoPath)
return exec.Command("git", "-C", repoPath, "fetch", "--prune", "origin", "+*:*")
case "shallow":
logger.Debugf("Updating repo with shallow clone type: %s", repoPath)
return exec.Command("git", "-C", repoPath, "pull", "--prune")
default:
logger.Debugf("[Default] Updating repo with bare clone type: %s", repoPath)
return exec.Command("git", "--git-dir", repoPath, "fetch", "--prune", "origin", "+*:*")
}
}

func CloneOrUpdateRepo(repoOwner, repoName string, config config.Config) {
repoFullName := fmt.Sprintf("%s/%s", repoOwner, repoName)
repoURL := fmt.Sprintf("%s://%s:%s@%s/%s.git", config.Server.Protocol, config.Username, config.Token, config.Server.Domain, repoFullName)
repoPath := filepath.Join(getBaseDirectoryPath(repoOwner, repoName, config), repoName+".git")

if _, err := os.Stat(repoPath); os.IsNotExist(err) {
logger.Info("Cloning repo: ", repoFullName)
command := getGitCloneCommand(config.CloneType, repoPath, repoURL)

output, err := exec.Command("git", "clone", "--bare", repoURL, repoPath).CombinedOutput()
output, err := command.CombinedOutput()
logger.Debugf("Output: %s\n", output)
if err != nil {
logger.Fatalf("Error cloning repo %s: %v\n", repoFullName, err)
Expand All @@ -32,8 +73,9 @@ func CloneOrUpdateRepo(repoOwner, repoName string, config config.Config) {
}
} else {
logger.Info("Updating repo: ", repoFullName)
command := getGitFetchCommand(config.CloneType, repoPath)

output, err := exec.Command("git", "--git-dir", repoPath, "fetch", "--prune", "origin", "+*:*").CombinedOutput()
output, err := command.CombinedOutput()
logger.Debugf("Output: %s\n", output)
if err != nil {
logger.Fatalf("Error updating repo %s: %v\n", repoFullName, err)
Expand Down

0 comments on commit 9a3e9fe

Please sign in to comment.