diff --git a/Gopkg.lock b/Gopkg.lock index 9a564e42a..c7f37e045 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -57,6 +57,14 @@ pruneopts = "NUT" revision = "5c94acc5e6eb520f1bcd183974e01171cc4c23b3" +[[projects]] + digest = "1:cd5ffc5bda4e0296ab3e4de90dbb415259c78e45e7fab13694b14cde8ab74541" + name = "github.com/tcnksm/go-gitconfig" + packages = ["."] + pruneopts = "NUT" + revision = "d154598bacbf4501c095a309753c5d4af66caa81" + version = "v0.1.2" + [[projects]] branch = "master" digest = "1:4d8a79fbc6fa348fc94afa4235947c5196b7900ed71b94aa5fcbc7e273d150e1" @@ -72,6 +80,7 @@ "github.com/fatih/color", "github.com/golang-collections/collections/stack", "github.com/jesseduffield/gocui", + "github.com/tcnksm/go-gitconfig", ] solver-name = "gps-cdcl" solver-version = 1 diff --git a/gitcommands.go b/gitcommands.go index e4f500a8f..2c5a4f2dc 100644 --- a/gitcommands.go +++ b/gitcommands.go @@ -14,6 +14,7 @@ import ( "github.com/fatih/color" "github.com/jesseduffield/gocui" + gitconfig "github.com/tcnksm/go-gitconfig" ) var ( @@ -267,6 +268,7 @@ func runCommand(command string) (string, error) { commandStartTime := time.Now() commandLog(command) splitCmd := strings.Split(command, " ") + devLog(splitCmd) cmdOut, err := exec.Command(splitCmd[0], splitCmd[1:]...).CombinedOutput() devLog("run command time: ", time.Now().Sub(commandStartTime)) return sanitisedCommandOutput(cmdOut, err) @@ -308,14 +310,16 @@ func gitAddPatch(g *gocui.Gui, filename string) { } func editFile(g *gocui.Gui, filename string) (string, error) { - editor := os.Getenv("VISUAL") + editor, _ := gitconfig.Global("core.editor") + if editor == "" { + editor = os.Getenv("VISUAL") + } if editor == "" { editor = os.Getenv("EDITOR") } if editor == "" { - editor = "vi" + return "", createErrorPanel(g, "No editor defined in $VISUAL, $EDITOR, or git config.") } - runSubProcess(g, editor, filename) return "", nil } @@ -455,9 +459,9 @@ func removeFile(file GitFile) error { } func gitCommit(g *gocui.Gui, message string) (string, error) { - out, _ := runDirectCommand("git config --get commit.gpgsign") - if out != "" { - runSubProcess(g, "git", "commit", "-m", "\""+message+"\"") + gpgsign, _ := gitconfig.Global("commit.gpgsign") + if gpgsign != "" { + runSubProcess(g, "bash", "-c", "git commit -m \""+message+"\"") return "", nil } return runDirectCommand("git commit -m \"" + message + "\"") diff --git a/vendor/github.com/tcnksm/go-gitconfig/LICENSE b/vendor/github.com/tcnksm/go-gitconfig/LICENSE new file mode 100644 index 000000000..3457f2566 --- /dev/null +++ b/vendor/github.com/tcnksm/go-gitconfig/LICENSE @@ -0,0 +1,22 @@ +Copyright (c) 2014 tcnksm + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/vendor/github.com/tcnksm/go-gitconfig/gitconfig.go b/vendor/github.com/tcnksm/go-gitconfig/gitconfig.go new file mode 100644 index 000000000..46c6a10f6 --- /dev/null +++ b/vendor/github.com/tcnksm/go-gitconfig/gitconfig.go @@ -0,0 +1,113 @@ +// Package gitconfig enables you to use `~/.gitconfig` values in Golang. +// +// For a full guide visit http://github.com/tcnksm/go-gitconfig +// +// package main +// +// import ( +// "github.com/tcnksm/go-gitconfig" +// "fmt" +// ) +// +// func main() { +// user, err := gitconfig.Global("user.name") +// if err == nil { +// fmt.Println(user) +// } +// } +// +package gitconfig + +import ( + "bytes" + "fmt" + "io/ioutil" + "os/exec" + "regexp" + "strings" + "syscall" +) + +// Entire extracts configuration value from `$HOME/.gitconfig` file , +// `$GIT_CONFIG`, /etc/gitconfig or include.path files. +func Entire(key string) (string, error) { + return execGitConfig(key) +} + +// Global extracts configuration value from `$HOME/.gitconfig` file or `$GIT_CONFIG`. +func Global(key string) (string, error) { + return execGitConfig("--global", key) +} + +// Local extracts configuration value from current project repository. +func Local(key string) (string, error) { + return execGitConfig("--local", key) +} + +// GithubUser extracts github.user name from `Entire gitconfig` +// This is same as Entire("github.user") +func GithubUser() (string, error) { + return Entire("github.user") +} + +// Username extracts git user name from `Entire gitconfig`. +// This is same as Entire("user.name") +func Username() (string, error) { + return Entire("user.name") +} + +// Email extracts git user email from `$HOME/.gitconfig` file or `$GIT_CONFIG`. +// This is same as Global("user.email") +func Email() (string, error) { + return Entire("user.email") +} + +// OriginURL extract remote origin url from current project repository. +// This is same as Local("remote.origin.url") +func OriginURL() (string, error) { + return Local("remote.origin.url") +} + +// Repository extract repository name of current project repository. +func Repository() (string, error) { + url, err := OriginURL() + if err != nil { + return "", err + } + + repo := retrieveRepoName(url) + return repo, nil +} + +// Github extracts github token from `Entire gitconfig`. +// This is same as Entire("github.token") +func GithubToken() (string, error) { + return Entire("github.token") +} + +func execGitConfig(args ...string) (string, error) { + gitArgs := append([]string{"config", "--get", "--null"}, args...) + var stdout bytes.Buffer + cmd := exec.Command("git", gitArgs...) + cmd.Stdout = &stdout + cmd.Stderr = ioutil.Discard + + err := cmd.Run() + if exitError, ok := err.(*exec.ExitError); ok { + if waitStatus, ok := exitError.Sys().(syscall.WaitStatus); ok { + if waitStatus.ExitStatus() == 1 { + return "", fmt.Errorf("the key `%s` is not found", args[len(args)-1]) + } + } + return "", err + } + + return strings.TrimRight(stdout.String(), "\000"), nil +} + +var RepoNameRegexp = regexp.MustCompile(`.+/([^/]+)(\.git)?$`) + +func retrieveRepoName(url string) string { + matched := RepoNameRegexp.FindStringSubmatch(url) + return strings.TrimSuffix(matched[1], ".git") +}