From 384d2e3c835fde68b5b896632cf4564c7ad51c76 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Tue, 7 Aug 2018 13:44:00 +1000 Subject: [PATCH 1/5] use sh instead of bash --- gitcommands.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gitcommands.go b/gitcommands.go index 71e737a6e..31615a346 100644 --- a/gitcommands.go +++ b/gitcommands.go @@ -116,7 +116,7 @@ func runDirectCommand(command string) (string, error) { commandLog(command) cmdOut, err := exec. - Command("bash", "-c", command). + Command("sh", "-c", command). CombinedOutput() devLog("run direct command time for command: ", command, time.Now().Sub(timeStart)) return sanitisedCommandOutput(cmdOut, err) @@ -174,7 +174,7 @@ func branchFromLine(line string, index int) Branch { func getGitBranches() []Branch { branches := make([]Branch, 0) // check if there are any branches - branchCheck, _ := runDirectCommand("git branch") + branchCheck, _ := runCommand("git branch") if branchCheck == "" { return append(branches, branchFromLine("master", 0)) } From ab03902d08a0e5dcf1530714c3e87ed533b10549 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Tue, 7 Aug 2018 14:08:56 +1000 Subject: [PATCH 2/5] return on error when merging branches --- gitcommands.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/gitcommands.go b/gitcommands.go index 31615a346..2de3cbe9a 100644 --- a/gitcommands.go +++ b/gitcommands.go @@ -205,7 +205,10 @@ func branchAlreadyStored(branchLine string, branches []Branch) bool { // directory i.e. things we've fetched but haven't necessarily checked out. // Worth mentioning this has nothing to do with the 'git merge' operation func getAndMergeFetchedBranches(branches []Branch) []Branch { - rawString, _ := runDirectCommand(getHeadsCommand) + rawString, err := runDirectCommand(getHeadsCommand) + if err != nil { + return branches + } branchLines := splitLines(rawString) for _, line := range branchLines { if branchAlreadyStored(line, branches) { From e23ed80eaae8a1fde099bbf3bad46ebcf3797b85 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Tue, 7 Aug 2018 14:10:15 +1000 Subject: [PATCH 3/5] use git branch when merging branches --- gitcommands.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitcommands.go b/gitcommands.go index 2de3cbe9a..dfb91b914 100644 --- a/gitcommands.go +++ b/gitcommands.go @@ -205,7 +205,7 @@ func branchAlreadyStored(branchLine string, branches []Branch) bool { // directory i.e. things we've fetched but haven't necessarily checked out. // Worth mentioning this has nothing to do with the 'git merge' operation func getAndMergeFetchedBranches(branches []Branch) []Branch { - rawString, err := runDirectCommand(getHeadsCommand) + rawString, err := runDirectCommand("git branch") if err != nil { return branches } From 46fb4c5f0a968adc6650bfad0ef7a3fdb3568837 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Tue, 7 Aug 2018 14:23:10 +1000 Subject: [PATCH 4/5] swap out bash command for a git one --- gitcommands.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/gitcommands.go b/gitcommands.go index dfb91b914..2dce88f8d 100644 --- a/gitcommands.go +++ b/gitcommands.go @@ -205,12 +205,14 @@ func branchAlreadyStored(branchLine string, branches []Branch) bool { // directory i.e. things we've fetched but haven't necessarily checked out. // Worth mentioning this has nothing to do with the 'git merge' operation func getAndMergeFetchedBranches(branches []Branch) []Branch { - rawString, err := runDirectCommand("git branch") + rawString, err := runDirectCommand("git branch --sort=-committerdate --no-color") if err != nil { return branches } branchLines := splitLines(rawString) for _, line := range branchLines { + line = strings.Replace(line, "* ", "", -1) + line = strings.TrimSpace(line) if branchAlreadyStored(line, branches) { continue } @@ -564,11 +566,3 @@ git reflog -n100 --pretty='%cr|%gs' --grep-reflog='checkout: moving' HEAD | { | tr -d ' ' } ` - -const getHeadsCommand = `git show-ref \ -| grep 'refs/heads/\|refs/remotes/origin/' \ -| sed 's/.*refs\/heads\///g' \ -| sed 's/.*refs\/remotes\/origin\///g' \ -| grep -v '^HEAD$' \ -| sort \ -| uniq` From a70753364cd3949e419ea0c0a4f73619479a3594 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Tue, 7 Aug 2018 15:21:50 +1000 Subject: [PATCH 5/5] platform specific shell usage --- gitcommands.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/gitcommands.go b/gitcommands.go index 2dce88f8d..50c1f2a9f 100644 --- a/gitcommands.go +++ b/gitcommands.go @@ -7,6 +7,7 @@ import ( "fmt" "os" "os/exec" + "runtime" "strings" "time" @@ -111,12 +112,20 @@ func mergeGitStatusFiles(oldGitFiles, newGitFiles []GitFile) []GitFile { return result } +func platformShell() (string, string) { + if runtime.GOOS == "windows" { + return "cmd", "/c" + } + return "sh", "-c" +} + func runDirectCommand(command string) (string, error) { timeStart := time.Now() - commandLog(command) + + shell, shellArg := platformShell() cmdOut, err := exec. - Command("sh", "-c", command). + Command(shell, shellArg, command). CombinedOutput() devLog("run direct command time for command: ", command, time.Now().Sub(timeStart)) return sanitisedCommandOutput(cmdOut, err)