2020-02-16 23:31:01 +01:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetRepo returns a repositories parts
|
2020-02-27 03:53:11 +01:00
|
|
|
func GetRepo(remoteName string) ([]string, error) {
|
2020-02-16 23:31:01 +01:00
|
|
|
cmd := exec.Command("git", "remote", "get-url", remoteName)
|
|
|
|
out, err := cmd.Output()
|
|
|
|
if err != nil {
|
2020-02-27 03:53:11 +01:00
|
|
|
return nil, err
|
2020-02-16 23:31:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
remote := strings.TrimSpace(string(out))
|
|
|
|
|
|
|
|
// SSH
|
|
|
|
if strings.Contains(remote, "@") {
|
|
|
|
remote = remote[strings.Index(remote, "@")+1:]
|
|
|
|
parts := strings.Split(remote, ":")
|
|
|
|
domain := "https://" + parts[0]
|
|
|
|
ownerRepo := strings.Split(parts[1], "/")
|
2020-02-27 03:53:11 +01:00
|
|
|
return []string{domain, ownerRepo[0], strings.TrimSuffix(ownerRepo[1], ".git")}, nil
|
2020-02-16 23:31:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// HTTP(S)
|
|
|
|
parts := strings.Split(remote, "/")
|
|
|
|
domain := parts[:len(parts)-2]
|
|
|
|
ownerRepo := parts[len(parts)-2:]
|
2020-02-27 03:53:11 +01:00
|
|
|
return []string{strings.Join(domain, "/"), ownerRepo[0], strings.TrimSuffix(ownerRepo[1], ".git")}, nil
|
2020-02-16 23:31:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Branches returns current branch
|
|
|
|
func Branch() string {
|
|
|
|
cmd := exec.Command("git", "branch", "--list", "--no-color")
|
|
|
|
out, err := cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, line := range strings.Split(string(out), "\n") {
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
if strings.HasPrefix(line, "*") {
|
|
|
|
return line[2:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|