mirror of https://github.com/curusarn/resh
parent
ff62e55d1f
commit
99584049a3
@ -0,0 +1,28 @@ |
||||
package normalize |
||||
|
||||
import ( |
||||
"net/url" |
||||
"strings" |
||||
|
||||
giturls "github.com/whilp/git-urls" |
||||
"go.uber.org/zap" |
||||
) |
||||
|
||||
// GitRemote helper
|
||||
// Returns normalized git remote - valid even on error
|
||||
func GitRemote(sugar *zap.SugaredLogger, gitRemote string) string { |
||||
gitRemote = strings.TrimSuffix(gitRemote, ".git") |
||||
parsedURL, err := giturls.Parse(gitRemote) |
||||
if err != nil { |
||||
sugar.Errorw("Failed to parse git remote", zap.Error(err), |
||||
"gitRemote", gitRemote, |
||||
) |
||||
return gitRemote |
||||
} |
||||
if parsedURL.User == nil || parsedURL.User.Username() == "" { |
||||
parsedURL.User = url.User("git") |
||||
} |
||||
// TODO: figure out what scheme we want
|
||||
parsedURL.Scheme = "git+ssh" |
||||
return parsedURL.String() |
||||
} |
||||
@ -0,0 +1,46 @@ |
||||
package normalize_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/curusarn/resh/internal/normalize" |
||||
"go.uber.org/zap" |
||||
) |
||||
|
||||
// TestLeftCutPadString
|
||||
func TestGitRemote(t *testing.T) { |
||||
sugar := zap.NewNop().Sugar() |
||||
|
||||
data := [][]string{ |
||||
{ |
||||
"git@github.com:curusarn/resh.git", // git
|
||||
"git@github.com:curusarn/resh", // git no ".git"
|
||||
"http://github.com/curusarn/resh.git", // http
|
||||
"https://github.com/curusarn/resh.git", // https
|
||||
"ssh://git@github.com/curusarn/resh.git", // ssh
|
||||
"git+ssh://git@github.com/curusarn/resh.git", // git+ssh
|
||||
}, |
||||
{ |
||||
"git@host.example.com:org/user/repo.git", // git
|
||||
"git@host.example.com:org/user/repo", // git no ".git"
|
||||
"http://host.example.com/org/user/repo.git", // http
|
||||
"https://host.example.com/org/user/repo.git", // https
|
||||
"ssh://git@host.example.com/org/user/repo.git", // ssh
|
||||
"git+ssh://git@host.example.com/org/user/repo.git", // git+ssh
|
||||
}, |
||||
} |
||||
|
||||
for _, arr := range data { |
||||
n := len(arr) |
||||
for i := 0; i < n-1; i++ { |
||||
for j := i + 1; j < n; j++ { |
||||
one := normalize.GitRemote(sugar, arr[i]) |
||||
two := normalize.GitRemote(sugar, arr[j]) |
||||
if one != two { |
||||
t.Fatalf("Normalized git remotes should match for '%s' and '%s'\n -> got: '%s' != '%s'", |
||||
arr[i], arr[j], one, two) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue