package git import ( "errors" "fmt" "regexp" "strings" "cnb.cool/looc/git-cnb/cmd" ) var GlobalFlags []string func gitCmd(args ...string) *cmd.Cmd { c := cmd.New("git") for _, v := range GlobalFlags { c.WithArg(v) } for _, a := range args { c.WithArg(a) } return c } func isGitDir() error { dirCmd := gitCmd("rev-parse", "-q", "--git-dir") dirCmd.Stderr = nil _, err := dirCmd.Output() if err != nil { return fmt.Errorf("Not a git repository (or any of the parent directories): .git") } return nil } func Remote() (string, error) { err := isGitDir() if err != nil { return "", err } remoteCmd := gitCmd("remote", "-v") remoteCmd.Stderr = nil output, _ := remoteCmd.Output() lines := outputLines(output) if len(lines) == 0 { return "", errors.New("have no remote url") } re := regexp.MustCompile(`\s+`) return re.Split(lines[0], -1)[1], nil } func outputLines(output string) []string { output = strings.TrimSuffix(output, "\n") if output == "" { return []string{} } return strings.Split(output, "\n") }