@@ -3,6 +3,7 @@ package controllers
33import  (
44	"fmt" 
55	"regexp" 
6+ 	"slices" 
67	"strings" 
78
89	"github.com/jesseduffield/gocui" 
@@ -142,9 +143,11 @@ func (self *RemotesController) enter(remote *models.Remote) error {
142143	return  nil 
143144}
144145
145- func  (self  * RemotesController ) addRemoteHelper (remoteName  string , remoteUrl  string , branchToCheckout  string ) error  {
146+ // add remote and refresh the remotes list 
147+ func  (self  * RemotesController ) addRemote (remoteName  string , remoteUrl  string ) error  {
146148	self .c .LogAction (self .c .Tr .Actions .AddRemote )
147- 	if  err  :=  self .c .Git ().Remote .AddRemote (remoteName , remoteUrl ); err  !=  nil  {
149+ 	err  :=  self .c .Git ().Remote .AddRemote (remoteName , remoteUrl )
150+ 	if  err  !=  nil  {
148151		return  err 
149152	}
150153
@@ -155,19 +158,44 @@ func (self *RemotesController) addRemoteHelper(remoteName string, remoteUrl stri
155158		Scope : []types.RefreshableView {types .REMOTES },
156159		Mode :  types .SYNC ,
157160	})
161+ 	return  nil 
162+ }
158163
159- 	// Select the new remote 
164+ func  (self  * RemotesController ) selectRemoteAndCheckout (remoteName  string , remoteUrl  string , branchToCheckout  string ) error  {
165+ 	// Select the remote 
160166	for  idx , remote  :=  range  self .c .Model ().Remotes  {
161167		if  remote .Name  ==  remoteName  {
162168			self .c .Contexts ().Remotes .SetSelection (idx )
163169			break 
164170		}
165171	}
166172
167- 	// Fetch the new  remote 
173+ 	// Fetch the remote 
168174	return  self .fetchAndCheckout (self .c .Contexts ().Remotes .GetSelected (), branchToCheckout )
169175}
170176
177+ // add remote, then refresh and select it, and then fetch it and checkout the given branch if applicable 
178+ func  (self  * RemotesController ) addRemoteHelper (remoteName  string , remoteUrl  string , branchToCheckout  string ) error  {
179+ 	self .addRemote (remoteName , remoteUrl )
180+ 
181+ 	return  self .selectRemoteAndCheckout (remoteName , remoteUrl , branchToCheckout )
182+ }
183+ 
184+ // the same as addRemoteHelper but we don't error if the remote with the already exists (the remote has to contain the given URL though) 
185+ func  (self  * RemotesController ) addForkHelper (remoteName  string , remoteUrl  string , branchToCheckout  string ) error  {
186+ 	for  idx , remote  :=  range  self .c .Model ().Remotes  {
187+ 		if  remote .Name  ==  remoteName  {
188+ 			hasUrl  :=  slices .Contains (remote .Urls , remoteUrl )
189+ 			if  ! hasUrl  {
190+ 				return  fmt .Errorf ("a remote named '%s' already exists with a different URL" , remoteName )
191+ 			}
192+ 			self .c .Contexts ().Remotes .SetSelection (idx )
193+ 			return  self .fetchAndCheckout (remote , branchToCheckout )
194+ 		}
195+ 	}
196+ 	return  self .addRemoteHelper (remoteName , remoteUrl , branchToCheckout )
197+ }
198+ 
171199func  (self  * RemotesController ) add () error  {
172200	self .c .Prompt (types.PromptOpts {
173201		Title : self .c .Tr .NewRemoteName ,
@@ -236,7 +264,7 @@ func (self *RemotesController) addFork(baseRemote *models.Remote) error {
236264				return  err 
237265			}
238266
239- 			return  self .addRemoteHelper (forkUsername , remoteUrl , branchToCheckout )
267+ 			return  self .addForkHelper (forkUsername , remoteUrl , branchToCheckout )
240268		},
241269	})
242270
0 commit comments