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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| package main
import ( "errors" "fmt" "github.com/kballard/go-shellquote" "github.com/spf13/cobra" "os" "os/exec" "runtime" "strings" )
func main() { if err := rootCmd.Execute(); err != nil { os.Exit(1) } }
var rootCmd = &cobra.Command{ Use: "git-open", Run: openCurrentRepo, }
func openCurrentRepo(cmd *cobra.Command, args []string) { var remote string if len(args) > 0 { remote = args[0] } _, err := CurrentGitRepo() if err != nil { Err("not a git repository") } if remote == "" { branch := CurrentBranch() remote = CurrentRemote(branch) } gitURL := RemoteURL(remote) url := TransferToURL(gitURL) _ = OpenBrowser(url) }
func GitCommand(args ...string) (string, error) { cmd := exec.Command("git", args...) cmd.Stderr = os.Stderr output, err := cmd.Output() return string(output), err }
func CurrentGitRepo() (string, error) { output, err := GitCommand("rev-parse", "-q", "--show-toplevel") return output, err }
func CurrentBranch() string { branch, err := SymbolicRef("HEAD", true) if branch == "" || err != nil { branch = "master" } return branch }
func SymbolicRef(ref string, short bool) (string, error) { args := []string{"symbolic-ref"} if short { args = append(args, "--short") } args = append(args, ref) output, err := GitCommand(args...) return firstLine(output), err }
func CurrentRemote(branch string) string { remote, err := GitCommand("config", fmt.Sprintf("branch.%s.remote", branch)) remote = firstLine(remote) if remote == "" || err != nil { remote = "origin" } return remote }
func RemoteURL(remote string) string { gitURL, err := GitCommand("ls-remote", "--get-url", remote) if err != nil { Err("git remote is not set for", remote) } gitURL = firstLine(gitURL) if gitURL == remote { Err(remote, "is a wrong remote") } return gitURL }
func firstLine(output string) string { if i := strings.Index(output, "\n"); i >= 0 { return output[0:i] } return output }
func TransferToURL(gitURL string) string { var url string if strings.HasPrefix(gitURL, "https://") { url = gitURL[:len(gitURL)-4] } if strings.HasPrefix(gitURL, "git@") { url = gitURL[:len(gitURL)-4] url = strings.Replace(url, ":", "/", 1) url = strings.Replace(url, "git@", "https://", 1) } return url }
func Err(msg ...interface{}) { fmt.Println(msg...) os.Exit(1) }
func OpenBrowser(url string) error { launcher, err := browserLauncher() if err != nil { return err } args := append(launcher, url) cmd := exec.Command(args[0], args[1:]...) return cmd.Run() }
func browserLauncher() ([]string, error) { browser := os.Getenv("BROWSER") if browser == "" { browser = searchBrowserLauncher() } else { browser = os.ExpandEnv(browser) }
if browser == "" { return nil, errors.New("please set $BROWSER to a web launcher") } return shellquote.Split(browser) }
func searchBrowserLauncher() (browser string) { switch runtime.GOOS { case "darwin": browser = "open" case "windows": browser = "cmd /c start" case "linux": browser = "xdg-open" default: browser = "" } return browser }
|