@@ -15,15 +15,56 @@ func getBaseDirectoryPath(repoOwner, repoName string, config config.Config) stri
15
15
return filepath .Join (config .BackupDir , repoOwner , repoName )
16
16
}
17
17
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
+
18
58
func CloneOrUpdateRepo (repoOwner , repoName string , config config.Config ) {
19
59
repoFullName := fmt .Sprintf ("%s/%s" , repoOwner , repoName )
20
60
repoURL := fmt .Sprintf ("%s://%s:%s@%s/%s.git" , config .Server .Protocol , config .Username , config .Token , config .Server .Domain , repoFullName )
21
61
repoPath := filepath .Join (getBaseDirectoryPath (repoOwner , repoName , config ), repoName + ".git" )
22
62
23
63
if _ , err := os .Stat (repoPath ); os .IsNotExist (err ) {
24
64
logger .Info ("Cloning repo: " , repoFullName )
65
+ command := getGitCloneCommand (config .CloneType , repoPath , repoURL )
25
66
26
- output , err := exec . Command ( "git" , "clone" , "--bare" , repoURL , repoPath ) .CombinedOutput ()
67
+ output , err := command .CombinedOutput ()
27
68
logger .Debugf ("Output: %s\n " , output )
28
69
if err != nil {
29
70
logger .Fatalf ("Error cloning repo %s: %v\n " , repoFullName , err )
@@ -32,8 +73,9 @@ func CloneOrUpdateRepo(repoOwner, repoName string, config config.Config) {
32
73
}
33
74
} else {
34
75
logger .Info ("Updating repo: " , repoFullName )
76
+ command := getGitFetchCommand (config .CloneType , repoPath )
35
77
36
- output , err := exec . Command ( "git" , "--git-dir" , repoPath , "fetch" , "--prune" , "origin" , "+*:*" ) .CombinedOutput ()
78
+ output , err := command .CombinedOutput ()
37
79
logger .Debugf ("Output: %s\n " , output )
38
80
if err != nil {
39
81
logger .Fatalf ("Error updating repo %s: %v\n " , repoFullName , err )
0 commit comments