@@ -36,7 +36,8 @@ func Clone(repo, dir, mirrorDir, primaryBranch, gitopsPath string) (*Repo, error
36
36
if err := os .RemoveAll (dir ); err != nil {
37
37
return nil , fmt .Errorf ("Unable to clone repo: %w" , err )
38
38
}
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 }
40
41
if mirrorDir != "" {
41
42
args = append (args , "--reference" , mirrorDir )
42
43
}
@@ -48,9 +49,9 @@ func Clone(repo, dir, mirrorDir, primaryBranch, gitopsPath string) (*Repo, error
48
49
return nil , fmt .Errorf ("Unable to create .git/info/sparse-checkout: %w" , err )
49
50
}
50
51
exec .Mustex (dir , "git" , "checkout" , primaryBranch )
51
-
52
52
return & Repo {
53
- Dir : dir ,
53
+ Dir : dir ,
54
+ RemoteName : remoteName ,
54
55
}, nil
55
56
}
56
57
@@ -59,6 +60,8 @@ func Clone(repo, dir, mirrorDir, primaryBranch, gitopsPath string) (*Repo, error
59
60
type Repo struct {
60
61
// Dir is the location of the git repo.
61
62
Dir string
63
+ // RemoteName is the name of the remote that tracks upstream repository.
64
+ RemoteName string
62
65
}
63
66
64
67
// Clean cleans up the repo
@@ -67,9 +70,10 @@ func (r *Repo) Clean() error {
67
70
}
68
71
69
72
// 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.
70
74
func (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 )
73
77
}
74
78
75
79
// SwitchToBranch switch the repo to specified branch and checkout primaryBranch files over it.
@@ -124,6 +128,6 @@ func (r *Repo) IsClean() bool {
124
128
// Push pushes all local changes to the remote repository
125
129
// all changes should be already commited
126
130
func (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 ... )
128
132
exec .Mustex (r .Dir , "git" , args ... )
129
133
}
0 commit comments