Skip to content

Commit 9a3e9fe

Browse files
feat: ✨ allow different clone type options
1 parent 643f0c9 commit 9a3e9fe

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

cmd/root.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ var rootCmd = &cobra.Command{
5252
Workspace: "",
5353
Cron: "",
5454
BackupDir: config.GetBackupDir(backupDir),
55+
CloneType: "bare",
5556
}
5657

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

74+
// If no clone_type is not set in the config file, set it to bare
75+
if cfg.CloneType == "" {
76+
cfg.CloneType = "bare"
77+
}
78+
7379
logger.Info("Config loaded from: ", config.GetConfigFile(cfgFile))
7480
logger.Info("Validating config ⏳")
7581

pkg/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Config struct {
2828
BackupDir string `mapstructure:"backup_dir"`
2929
Workspace string `mapstructure:"workspace"`
3030
Cron string `mapstructure:"cron"`
31+
CloneType string `mapstructure:"clone_type"`
3132
}
3233

3334
func expandPath(path string) string {
@@ -105,6 +106,7 @@ func SaveConfig(config Config, cfgFile string) error {
105106
viper.Set("server", config.Server)
106107
viper.Set("workspace", config.Workspace)
107108
viper.Set("cron", config.Cron)
109+
viper.Set("clone_type", config.CloneType)
108110

109111
return viper.WriteConfig()
110112
}

pkg/config/validate.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,10 @@ func ValidateConfig(cfg Config) error {
3838
}
3939
}
4040

41+
// validate that clone_type can only be `bare`, `full`, `mirror` or `shallow`
42+
if cfg.CloneType != "bare" && cfg.CloneType != "full" && cfg.CloneType != "mirror" && cfg.CloneType != "shallow" {
43+
return fmt.Errorf("clone_type can only be `bare`, `full`, `mirror` or `shallow`")
44+
}
45+
4146
return nil
4247
}

pkg/sync/sync.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,56 @@ func getBaseDirectoryPath(repoOwner, repoName string, config config.Config) stri
1515
return filepath.Join(config.BackupDir, repoOwner, repoName)
1616
}
1717

18+
func getGitCloneCommand(CloneType, repoPath, repoURL string) *exec.Cmd {
19+
switch CloneType {
20+
case "bare":
21+
logger.Debugf("Cloning repo with bare clone type: %s", repoURL)
22+
return exec.Command("git", "clone", "--bare", repoURL, repoPath)
23+
case "full":
24+
logger.Debugf("Cloning repo with full clone type: %s", repoURL)
25+
return exec.Command("git", "clone", repoURL, repoPath)
26+
case "mirror":
27+
logger.Debugf("Cloning repo with mirror clone type: %s", repoURL)
28+
return exec.Command("git", "clone", "--mirror", repoURL, repoPath)
29+
case "shallow":
30+
logger.Debugf("Cloning repo with shallow clone type: %s", repoURL)
31+
return exec.Command("git", "clone", "--depth", "1", repoURL, repoPath)
32+
default:
33+
logger.Debugf("[Default] Cloning repo with bare clone type: %s", repoURL)
34+
return exec.Command("git", "clone", "--bare", repoURL, repoPath)
35+
}
36+
}
37+
38+
func getGitFetchCommand(CloneType, repoPath string) *exec.Cmd {
39+
switch CloneType {
40+
case "bare":
41+
logger.Debugf("Updating repo with bare clone type: %s", repoPath)
42+
return exec.Command("git", "--git-dir", repoPath, "fetch", "--prune", "origin", "+*:*")
43+
case "full":
44+
logger.Debugf("Updating repo with full clone type: %s", repoPath)
45+
return exec.Command("git", "-C", repoPath, "pull", "--prune")
46+
case "mirror":
47+
logger.Debugf("Updating repo with mirror clone type: %s", repoPath)
48+
return exec.Command("git", "-C", repoPath, "fetch", "--prune", "origin", "+*:*")
49+
case "shallow":
50+
logger.Debugf("Updating repo with shallow clone type: %s", repoPath)
51+
return exec.Command("git", "-C", repoPath, "pull", "--prune")
52+
default:
53+
logger.Debugf("[Default] Updating repo with bare clone type: %s", repoPath)
54+
return exec.Command("git", "--git-dir", repoPath, "fetch", "--prune", "origin", "+*:*")
55+
}
56+
}
57+
1858
func CloneOrUpdateRepo(repoOwner, repoName string, config config.Config) {
1959
repoFullName := fmt.Sprintf("%s/%s", repoOwner, repoName)
2060
repoURL := fmt.Sprintf("%s://%s:%s@%s/%s.git", config.Server.Protocol, config.Username, config.Token, config.Server.Domain, repoFullName)
2161
repoPath := filepath.Join(getBaseDirectoryPath(repoOwner, repoName, config), repoName+".git")
2262

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

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

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

0 commit comments

Comments
 (0)