Skip to content

Commit 9ddda01

Browse files
committed
fixup! feat: add fork remote command
Combine the three regex's into one. While it makes the regex itself more complex, it simplifies the code.
1 parent db80a75 commit 9ddda01

File tree

1 file changed

+9
-19
lines changed

1 file changed

+9
-19
lines changed

pkg/gui/controllers/remotes_controller.go

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,11 @@ func (self *RemotesController) add() error {
212212
return nil
213213
}
214214

215-
var (
216-
// 1. SCP-like SSH: git@host:owner[/subgroups]/repo(.git)
217-
sshScpRegex = regexp.MustCompile(`^(git@[^:]+:)([^/]+(?:/[^/]+)*)/([^/]+?)(\.git)?$`)
218-
219-
// 2. SSH URL style: ssh://user@host[:port]/owner[/subgroups]/repo(.git)
220-
sshUrlRegex = regexp.MustCompile(`^(ssh://[^/]+/)([^/]+(?:/[^/]+)*)/([^/]+?)(\.git)?$`)
221-
222-
// 3. HTTPS: https://host/owner[/subgroups]/repo(.git)
223-
httpRegex = regexp.MustCompile(`^(https?://[^/]+/)([^/]+(?:/[^/]+)*)/([^/]+?)(\.git)?$`)
224-
)
215+
// Regex to match and capture parts of a Git remote URL. Supports the following formats:
216+
// 1. SCP-like SSH: git@host:owner[/subgroups]/repo(.git)
217+
// 2. SSH URL style: ssh://user@host[:port]/owner[/subgroups]/repo(.git)
218+
// 3. HTTPS: https://host/owner[/subgroups]/repo(.git)
219+
var urlRegex = regexp.MustCompile(`^(git@[^:]+:|ssh://[^/]+/|https?://[^/]+/)([^/]+(?:/[^/]+)*)/([^/]+?)(\.git)?$`)
225220

226221
// Rewrites a Git remote URL to use the given fork username,
227222
// keeping the repo name and host intact. Supports SCP-like SSH, SSH URL style, and HTTPS.
@@ -233,16 +228,11 @@ func replaceForkUsername(remoteUrl, forkUsername string) (string, error) {
233228
return "", errors.New("remote URL cannot be empty")
234229
}
235230

236-
switch {
237-
case sshScpRegex.MatchString(remoteUrl):
238-
return sshScpRegex.ReplaceAllString(remoteUrl, "${1}"+forkUsername+"/$3$4"), nil
239-
case sshUrlRegex.MatchString(remoteUrl):
240-
return sshUrlRegex.ReplaceAllString(remoteUrl, "${1}"+forkUsername+"/$3$4"), nil
241-
case httpRegex.MatchString(remoteUrl):
242-
return httpRegex.ReplaceAllString(remoteUrl, "${1}"+forkUsername+"/$3$4"), nil
243-
default:
244-
return "", fmt.Errorf("unsupported or invalid remote URL: %s", remoteUrl)
231+
if urlRegex.MatchString(remoteUrl) {
232+
return urlRegex.ReplaceAllString(remoteUrl, "${1}"+forkUsername+"/$3$4"), nil
245233
}
234+
235+
return "", fmt.Errorf("unsupported or invalid remote URL: %s", remoteUrl)
246236
}
247237

248238
func (self *RemotesController) addFork(baseRemote *models.Remote) error {

0 commit comments

Comments
 (0)