@@ -35,10 +35,14 @@ func NewGit() (ret *Git) {
3535 return
3636}
3737
38- func (g * Git ) SetRemote (protocol , host , repoPath string ) {
38+ func (g * Git ) SetRemote (name , protocol , host , repoPath string ) {
3939 if g == nil {
4040 return
4141 }
42+ g .remoteName = name
43+ g .protocol = protocol
44+ g .host = host
45+ g .repoPath = repoPath
4246}
4347
4448// OpenRepo open the GIT repo
@@ -118,36 +122,30 @@ func (g *Git) CreateTag(name string) (err error) {
118122}
119123
120124// CreateRemote create or update a remote.
121- func (g * Git ) CreateRemote (options map [string ]string ) (_ error ) {
122- if v , found := options ["remote-name" ]; found {
123- g .remoteName = v
124- }
125+ func (g * Git ) CreateRemote (options GitRemoteConfig ) (_ error ) {
126+ g .remoteName = options .Get ("remote-name" , g .remoteName , "ci-upstream" )
125127
126128 if g .host == "" || g .repoPath == "" {
127129 return errors .New ("Unable to create a remote without host/repo-path setup in 'release-mgt.yaml'" )
128130 }
129131
130132 if r , err := g .repo .Remote (g .remoteName ); r == nil && err .Error () != git .ErrRemoteNotFound .Error () {
131133 return err
134+ } else if r != nil {
135+ g .removeRemote = false
136+ gotrace .Trace ("Remote '%s' already exist. Not recreated." , g .remoteName )
137+ return nil
132138 }
133139
134140 if v , found := options ["auto-remove-remote" ]; ! found || v == "true" {
135141 g .removeRemote = true
136142 }
137- if g .remoteName == "" {
138- g .remoteName = "ci-upstream"
139- }
140- protocol := "https"
141- if v , found := options ["protocol" ]; found {
142- protocol = v
143- }
144- if protocol == "" {
145- protocol = "https"
146- }
143+
147144 remoteConfig := gitconfig.RemoteConfig {
148145 Name : g .remoteName ,
149146 }
150- switch protocol {
147+
148+ switch protocol := options .Get ("protocol" , g .protocol , "https" ); protocol {
151149 case "https" , "http" :
152150 var user * url.Userinfo
153151 if u , foundUser := options ["user" ]; foundUser {
@@ -189,6 +187,7 @@ func (g *Git) CreateRemote(options map[string]string) (_ error) {
189187 if _ , err := g .repo .CreateRemote (& remoteConfig ); err != nil {
190188 return err
191189 }
190+ gotrace .Trace ("Remote '%s' created." , g .remoteName )
192191
193192 return
194193}
@@ -205,6 +204,7 @@ func (g *Git) CleanRemote() (_ error) {
205204 r , err := g .repo .Remote (g .remoteName )
206205 if r != nil {
207206 g .repo .DeleteRemote (g .remoteName )
207+ gotrace .Trace ("Remote '%s' removed." , g .remoteName )
208208 }
209209
210210 return err
0 commit comments