Skip to content

Commit

Permalink
fix: match git remotes without trailing .git
Browse files Browse the repository at this point in the history
  • Loading branch information
mroth committed Sep 15, 2019
1 parent 7a5b362 commit 9d4710b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 5 additions & 3 deletions git.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"context"
"errors"
"os"
Expand Down Expand Up @@ -118,14 +119,15 @@ func _detectRemoteURL_GoGit(path string) (string, error) {
// uses os/exec from standard library, does not add a dependency
//
// os/exec adds 242KB to macOS binary size
// benchmarks at 4.2 ms/op
// bytes adds 218kb
// benchmarks at 5.1 ms/op
func _detectRemoteURL_LocalGit(path string) (string, error) {
cmd := exec.Command("git", "config", "--get", "remote.origin.url")
output, err := cmd.Output()
if err != nil {
return "", err
}
return string(output), nil
return string(bytes.TrimSpace(output)), nil
}

// parseGithubRemote parses string remoteURL against known patterns matching
Expand All @@ -136,7 +138,7 @@ func _detectRemoteURL_LocalGit(path string) (string, error) {
// https://github.com/mroth/bump.git
// git@github.com:mroth/bump.git
func parseGithubRemote(remoteURL string) (owner, repo string, ok bool) {
re := regexp.MustCompile(`^(?:https://|git@)github.com[:/](.*)/(.*)\.git`)
re := regexp.MustCompile(`^(?:https://|git@)github.com[:/](.*)/(.*?)(?:\.git$|$)`)
matches := re.FindStringSubmatch(remoteURL)
if matches == nil || len(matches) < 3 {
return
Expand Down
7 changes: 7 additions & 0 deletions git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ func Test_parseGithubRemote(t *testing.T) {
wantRepo: "bump",
wantOk: true,
},
{
name: "GitHub_HTTPS_noExtension",
remoteURL: "https://github.com/mroth/bump",
wantOwner: "mroth",
wantRepo: "bump",
wantOk: true,
},
{
name: "GitHub_SSH",
remoteURL: "git@github.com:mroth/bump.git",
Expand Down

0 comments on commit 9d4710b

Please sign in to comment.