@@ -143,8 +143,8 @@ func (self *RemotesController) enter(remote *models.Remote) error {
143143	return  nil 
144144}
145145
146- // add  remote and refresh  the remotes  list 
147- func  (self  * RemotesController ) addRemote (remoteName  string , remoteUrl  string ) error  {
146+ // Adds a new  remote and refreshes  the list of remotes.  
147+ func  (self  * RemotesController ) addRemoteAndRefresh (remoteName  string , remoteUrl  string ) error  {
148148	self .c .LogAction (self .c .Tr .Actions .AddRemote )
149149	err  :=  self .c .Git ().Remote .AddRemote (remoteName , remoteUrl )
150150	if  err  !=  nil  {
@@ -161,6 +161,7 @@ func (self *RemotesController) addRemote(remoteName string, remoteUrl string) er
161161	return  nil 
162162}
163163
164+ // Selects the given remote in the UI, fetches it, and checks out the specified branch if profided. 
164165func  (self  * RemotesController ) selectRemoteAndCheckout (remoteName  string , remoteUrl  string , branchToCheckout  string ) error  {
165166	// Select the remote 
166167	for  idx , remote  :=  range  self .c .Model ().Remotes  {
@@ -174,15 +175,17 @@ func (self *RemotesController) selectRemoteAndCheckout(remoteName string, remote
174175	return  self .fetchAndCheckout (self .c .Contexts ().Remotes .GetSelected (), branchToCheckout )
175176}
176177
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 )
178+ // Adds a new  remote, refreshes  and selects  it, then fetches  and checks out  the specified  branch if provided.  
179+ func  (self  * RemotesController ) addAndCheckoutRemote (remoteName  string , remoteUrl  string , branchToCheckout  string ) error  {
180+ 	self .addRemoteAndRefresh (remoteName , remoteUrl )
180181
181182	return  self .selectRemoteAndCheckout (remoteName , remoteUrl , branchToCheckout )
182183}
183184
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  {
185+ // Ensures the fork remote exists (matching the given URL). 
186+ // If it exists and matches, it’s selected and fetched; otherwise, it’s created and then fetched and checked out. 
187+ // If it does exist but with a different URL, an error is returned. 
188+ func  (self  * RemotesController ) ensureForkRemoteAndCheckout (remoteName  string , remoteUrl  string , branchToCheckout  string ) error  {
186189	for  idx , remote  :=  range  self .c .Model ().Remotes  {
187190		if  remote .Name  ==  remoteName  {
188191			hasTheSameUrl  :=  slices .Contains (remote .Urls , remoteUrl )
@@ -198,7 +201,7 @@ func (self *RemotesController) addForkHelper(remoteName string, remoteUrl string
198201			return  self .fetchAndCheckout (remote , branchToCheckout )
199202		}
200203	}
201- 	return  self .addRemoteHelper (remoteName , remoteUrl , branchToCheckout )
204+ 	return  self .addAndCheckoutRemote (remoteName , remoteUrl , branchToCheckout )
202205}
203206
204207func  (self  * RemotesController ) add () error  {
@@ -208,7 +211,7 @@ func (self *RemotesController) add() error {
208211			self .c .Prompt (types.PromptOpts {
209212				Title : self .c .Tr .NewRemoteUrl ,
210213				HandleConfirm : func (remoteUrl  string ) error  {
211- 					return  self .addRemoteHelper (remoteName , remoteUrl , "" )
214+ 					return  self .addAndCheckoutRemote (remoteName , remoteUrl , "" )
212215				},
213216			})
214217
@@ -230,7 +233,7 @@ var (
230233	httpRegex  =  regexp .MustCompile (`^(https?://[^/]+/)([^/]+(?:/[^/]+)*)/([^/]+?)(\.git)?$` )
231234)
232235
233- // replaceForkUsername rewrites  a Git remote URL to use the given fork username, 
236+ // Rewrites  a Git remote URL to use the given fork username, 
234237// keeping the repo name and host intact. Supports SCP-like SSH, SSH URL style, and HTTPS. 
235238func  replaceForkUsername (remoteUrl , forkUsername  string ) (string , error ) {
236239	if  forkUsername  ==  ""  {
@@ -269,7 +272,7 @@ func (self *RemotesController) addFork(baseRemote *models.Remote) error {
269272				return  err 
270273			}
271274
272- 			return  self .addForkHelper (forkUsername , remoteUrl , branchToCheckout )
275+ 			return  self .ensureForkRemoteAndCheckout (forkUsername , remoteUrl , branchToCheckout )
273276		},
274277	})
275278
0 commit comments