-
Notifications
You must be signed in to change notification settings - Fork 290
/
remote.go
46 lines (35 loc) · 929 Bytes
/
remote.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package git
import (
"os/exec"
"strings"
giturls "github.com/whilp/git-urls"
)
type GitRemote string
func (gr GitRemote) String() string {
return string(gr)
}
func ProvideGitRemote() GitRemote {
return GitRemote(normalizeGitRemote(gitOrigin(".")))
}
func gitOrigin(fromDir string) string {
cmd := exec.Command("git", "-C", fromDir, "remote", "get-url", "origin")
b, err := cmd.Output()
if err != nil {
return ""
}
return strings.TrimRight(string(b), "\n")
}
func normalizeGitRemote(s string) string {
u, err := giturls.Parse(s)
if err != nil {
return s
}
// treat "http://", "https://", "git://", "ssh://", etc as equiv
u.Scheme = ""
u.User = nil
// github.com/tilt-dev/tilt is the same as github.com/tilt-dev/tilt/
u.Path = strings.TrimSuffix(u.Path, "/")
// github.com/tilt-dev/tilt is the same as github.com/tilt-dev/tilt.git
u.Path = strings.TrimSuffix(u.Path, ".git")
return u.String()
}