Skip to content

Commit d6be7de

Browse files
committed
close #280 improve url handling regexp and add documentation
1 parent 8d0684d commit d6be7de

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

githandler/remote.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,38 @@ type RemoteInfo struct {
1313

1414
//OrgAndRepo ...
1515
//Expects input from LSRemote
16+
//Extracts the Organization and repository name from a remote URL returned from git ls-remote
17+
//this is only meant to work for github because they have a multi-tenant system where organization and repository
18+
//is used to query their API
1619
func OrgAndRepo(url string) *RemoteInfo {
17-
re := regexp.MustCompile(`.+:(\S+)\/(\S+)\.git`)
1820

1921
//Extracts repo and org from ssh url format
22+
re := regexp.MustCompile(`.+:(\S+)\/(\S+)\.git`)
23+
24+
//if there is no protocol prefix we the two capture groups will fetch
25+
//organization and repo
2026
if strings.HasPrefix(url, "git@") {
2127
match := re.FindStringSubmatch(url)
2228
return &RemoteInfo{match[1], match[2]}
2329
}
24-
//Extracts repo and org from http url format
30+
//Is the protocol prefix is http we split the urls backwards and remove .git
2531
if strings.HasPrefix(url, "http") {
2632
splitURL := strings.Split(strings.TrimSuffix(url, ".git"), "/")
2733
org := splitURL[len(splitURL)-2]
2834
repo := splitURL[len(splitURL)-1]
2935
return &RemoteInfo{org, repo}
3036
}
37+
38+
//If any protocol is given (apart from file:// that will fail) the capture groups
39+
//will fetch the organization and repo
40+
//There is a redundancy because this regex will also work in the first case,
41+
//but this has been added as a fix and will work even though it is not completely optimized
3142
if strings.Contains(url, "://") {
3243
protoExp := regexp.MustCompile(`\w*:\/\/[\w.@]+(?:\:\d+)?\/(\w+)\/(\w+)\.git`)
3344
match := protoExp.FindStringSubmatch(url)
3445
return &RemoteInfo{match[1], match[2]}
3546
}
3647

37-
//Clone from local repo
48+
//If for any case we miss an url specification we will return an empty RemoteInfo
3849
return &RemoteInfo{}
3950
}

0 commit comments

Comments
 (0)