@@ -36,7 +36,8 @@ func Clone(repo, dir, mirrorDir, primaryBranch, gitopsPath string) (*Repo, error
3636 if err := os .RemoveAll (dir ); err != nil {
3737 return nil , fmt .Errorf ("Unable to clone repo: %w" , err )
3838 }
39- args := []string {"clone" , "--no-checkout" , "--single-branch" , "--branch" , primaryBranch , "--filter=blob:none" , "--no-tags" }
39+ remoteName := "origin"
40+ args := []string {"clone" , "--no-checkout" , "--single-branch" , "--branch" , primaryBranch , "--filter=blob:none" , "--no-tags" , "--origin" , remoteName }
4041 if mirrorDir != "" {
4142 args = append (args , "--reference" , mirrorDir )
4243 }
@@ -48,9 +49,9 @@ func Clone(repo, dir, mirrorDir, primaryBranch, gitopsPath string) (*Repo, error
4849 return nil , fmt .Errorf ("Unable to create .git/info/sparse-checkout: %w" , err )
4950 }
5051 exec .Mustex (dir , "git" , "checkout" , primaryBranch )
51-
5252 return & Repo {
53- Dir : dir ,
53+ Dir : dir ,
54+ RemoteName : remoteName ,
5455 }, nil
5556}
5657
@@ -59,6 +60,8 @@ func Clone(repo, dir, mirrorDir, primaryBranch, gitopsPath string) (*Repo, error
5960type Repo struct {
6061 // Dir is the location of the git repo.
6162 Dir string
63+ // RemoteName is the name of the remote that tracks upstream repository.
64+ RemoteName string
6265}
6366
6467// Clean cleans up the repo
@@ -67,9 +70,10 @@ func (r *Repo) Clean() error {
6770}
6871
6972// Fetch branches from the remote repository based on a specified pattern.
73+ // The branches will be be added to the list tracked remote branches ready to be pushed.
7074func (r * Repo ) Fetch (pattern string ) {
71- refSpec := fmt . Sprintf ( "+refs/heads/%s:refs/remotes/origin/%s " , pattern , pattern )
72- exec .Mustex (r .Dir , "git" , "fetch" , "--force" , "--filter=blob:none" , "--no-tags" , "origin" , refSpec )
75+ exec . Mustex ( r . Dir , "git " , "remote" , "set-branches" , "--add" , r . RemoteName , pattern )
76+ exec .Mustex (r .Dir , "git" , "fetch" , "--force" , "--filter=blob:none" , "--no-tags" , r . RemoteName )
7377}
7478
7579// SwitchToBranch switch the repo to specified branch and checkout primaryBranch files over it.
@@ -124,6 +128,6 @@ func (r *Repo) IsClean() bool {
124128// Push pushes all local changes to the remote repository
125129// all changes should be already commited
126130func (r * Repo ) Push (branches []string ) {
127- args := append ([]string {"push" , "origin" , "-f" , "--set-upstream" }, branches ... )
131+ args := append ([]string {"push" , r . RemoteName , "-f" , "--set-upstream" }, branches ... )
128132 exec .Mustex (r .Dir , "git" , args ... )
129133}
0 commit comments