From 9a3b565806689a35f1d330c233dbbc725bb7ebe8 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Mon, 6 Aug 2018 13:49:15 +1000 Subject: [PATCH 01/10] add vscode keybinding --- files_panel.go | 6 ++++-- gitcommands.go | 6 +++++- gui.go | 3 +++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/files_panel.go b/files_panel.go index dd0a3f2be..52d4137e3 100644 --- a/files_panel.go +++ b/files_panel.go @@ -180,8 +180,10 @@ func genericFileOpen(g *gocui.Gui, v *gocui.View, open func(string) (string, err } return nil } - _, err = open(file.Name) - return err + if output, err := open(file.Name); err != nil { + return createErrorPanel(g, output) + } + return nil } func handleFileOpen(g *gocui.Gui, v *gocui.View) error { diff --git a/gitcommands.go b/gitcommands.go index e0f5bacfe..719a8ceb0 100644 --- a/gitcommands.go +++ b/gitcommands.go @@ -305,7 +305,11 @@ func runCommand(command string) (string, error) { splitCmd := strings.Split(command, " ") cmdOut, err := exec.Command(splitCmd[0], splitCmd[1:]...).CombinedOutput() devLog("run command time: ", time.Now().Sub(commandStartTime)) - return string(cmdOut), err + outputString := string(cmdOut) + if outputString == "" && err != nil { + return err.Error(), err + } + return outputString, err } func openFile(filename string) (string, error) { diff --git a/gui.go b/gui.go index 1c55c91ba..040cfdafd 100644 --- a/gui.go +++ b/gui.go @@ -124,6 +124,9 @@ func keybindings(g *gocui.Gui) error { if err := g.SetKeybinding("files", 's', gocui.ModNone, handleSublimeFileOpen); err != nil { return err } + if err := g.SetKeybinding("files", 'v', gocui.ModNone, handleVsCodeFileOpen); err != nil { + return err + } if err := g.SetKeybinding("files", 'i', gocui.ModNone, handleIgnoreFile); err != nil { return err } From 7b7621c18c437368d6796302fe576e665aded722 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Mon, 6 Aug 2018 07:37:14 +0200 Subject: [PATCH 02/10] apply gofmt -s -w --- commits_panel.go | 218 +++++++++++++++++++++++------------------------ status_panel.go | 68 +++++++-------- 2 files changed, 143 insertions(+), 143 deletions(-) diff --git a/commits_panel.go b/commits_panel.go index 767e49bcd..048807ca9 100644 --- a/commits_panel.go +++ b/commits_panel.go @@ -1,138 +1,138 @@ package main import ( - "errors" + "errors" - "github.com/fatih/color" - "github.com/jesseduffield/gocui" + "github.com/fatih/color" + "github.com/jesseduffield/gocui" ) var ( - // ErrNoCommits : When no commits are found for the branch - ErrNoCommits = errors.New("No commits for this branch") + // ErrNoCommits : When no commits are found for the branch + ErrNoCommits = errors.New("No commits for this branch") ) func refreshCommits(g *gocui.Gui) error { - g.Update(func(*gocui.Gui) error { - state.Commits = getCommits() - v, err := g.View("commits") - if err != nil { - panic(err) - } - v.Clear() - red := color.New(color.FgRed) - yellow := color.New(color.FgYellow) - white := color.New(color.FgWhite) - shaColor := white - for _, commit := range state.Commits { - if commit.Pushed { - shaColor = red - } else { - shaColor = yellow - } - shaColor.Fprint(v, commit.Sha+" ") - white.Fprintln(v, commit.Name) - } - refreshStatus(g) - return nil - }) - return nil + g.Update(func(*gocui.Gui) error { + state.Commits = getCommits() + v, err := g.View("commits") + if err != nil { + panic(err) + } + v.Clear() + red := color.New(color.FgRed) + yellow := color.New(color.FgYellow) + white := color.New(color.FgWhite) + shaColor := white + for _, commit := range state.Commits { + if commit.Pushed { + shaColor = red + } else { + shaColor = yellow + } + shaColor.Fprint(v, commit.Sha+" ") + white.Fprintln(v, commit.Name) + } + refreshStatus(g) + return nil + }) + return nil } func handleResetToCommit(g *gocui.Gui, commitView *gocui.View) error { - return createConfirmationPanel(g, commitView, "Reset To Commit", "Are you sure you want to reset to this commit?", func(g *gocui.Gui, v *gocui.View) error { - commit, err := getSelectedCommit(g) - devLog(commit) - if err != nil { - panic(err) - } - if output, err := gitResetToCommit(commit.Sha); err != nil { - return createErrorPanel(g, output) - } - if err := refreshCommits(g); err != nil { - panic(err) - } - if err := refreshFiles(g); err != nil { - panic(err) - } - resetOrigin(commitView) - return handleCommitSelect(g, nil) - }, nil) + return createConfirmationPanel(g, commitView, "Reset To Commit", "Are you sure you want to reset to this commit?", func(g *gocui.Gui, v *gocui.View) error { + commit, err := getSelectedCommit(g) + devLog(commit) + if err != nil { + panic(err) + } + if output, err := gitResetToCommit(commit.Sha); err != nil { + return createErrorPanel(g, output) + } + if err := refreshCommits(g); err != nil { + panic(err) + } + if err := refreshFiles(g); err != nil { + panic(err) + } + resetOrigin(commitView) + return handleCommitSelect(g, nil) + }, nil) } func renderCommitsOptions(g *gocui.Gui) error { - return renderOptionsMap(g, map[string]string{ - "s": "squash down", - "r": "rename", - "g": "reset to this commit", - "← → ↑ ↓": "navigate", - }) + return renderOptionsMap(g, map[string]string{ + "s": "squash down", + "r": "rename", + "g": "reset to this commit", + "← → ↑ ↓": "navigate", + }) } func handleCommitSelect(g *gocui.Gui, v *gocui.View) error { - if err := renderCommitsOptions(g); err != nil { - return err - } - commit, err := getSelectedCommit(g) - if err != nil { - if err != ErrNoCommits { - return err - } - return renderString(g, "main", "No commits for this branch") - } - commitText := gitShow(commit.Sha) - return renderString(g, "main", commitText) + if err := renderCommitsOptions(g); err != nil { + return err + } + commit, err := getSelectedCommit(g) + if err != nil { + if err != ErrNoCommits { + return err + } + return renderString(g, "main", "No commits for this branch") + } + commitText := gitShow(commit.Sha) + return renderString(g, "main", commitText) } func handleCommitSquashDown(g *gocui.Gui, v *gocui.View) error { - if getItemPosition(v) != 0 { - return createErrorPanel(g, "Can only squash topmost commit") - } - if len(state.Commits) == 1 { - return createErrorPanel(g, "You have no commits to squash with") - } - commit, err := getSelectedCommit(g) - if err != nil { - return err - } - if output, err := gitSquashPreviousTwoCommits(commit.Name); err != nil { - return createErrorPanel(g, output) - } - if err := refreshCommits(g); err != nil { - panic(err) - } - refreshStatus(g) - return handleCommitSelect(g, v) + if getItemPosition(v) != 0 { + return createErrorPanel(g, "Can only squash topmost commit") + } + if len(state.Commits) == 1 { + return createErrorPanel(g, "You have no commits to squash with") + } + commit, err := getSelectedCommit(g) + if err != nil { + return err + } + if output, err := gitSquashPreviousTwoCommits(commit.Name); err != nil { + return createErrorPanel(g, output) + } + if err := refreshCommits(g); err != nil { + panic(err) + } + refreshStatus(g) + return handleCommitSelect(g, v) } func handleRenameCommit(g *gocui.Gui, v *gocui.View) error { - if getItemPosition(v) != 0 { - return createErrorPanel(g, "Can only rename topmost commit") - } - createPromptPanel(g, v, "Rename Commit", func(g *gocui.Gui, v *gocui.View) error { - if output, err := gitRenameCommit(v.Buffer()); err != nil { - return createErrorPanel(g, output) - } - if err := refreshCommits(g); err != nil { - panic(err) - } - return handleCommitSelect(g, v) - }) - return nil + if getItemPosition(v) != 0 { + return createErrorPanel(g, "Can only rename topmost commit") + } + createPromptPanel(g, v, "Rename Commit", func(g *gocui.Gui, v *gocui.View) error { + if output, err := gitRenameCommit(v.Buffer()); err != nil { + return createErrorPanel(g, output) + } + if err := refreshCommits(g); err != nil { + panic(err) + } + return handleCommitSelect(g, v) + }) + return nil } func getSelectedCommit(g *gocui.Gui) (Commit, error) { - v, err := g.View("commits") - if err != nil { - panic(err) - } - if len(state.Commits) == 0 { - return Commit{}, ErrNoCommits - } - lineNumber := getItemPosition(v) - if lineNumber > len(state.Commits)-1 { - colorLog(color.FgRed, "potential error in getSelected Commit (mismatched ui and state)", state.Commits, lineNumber) - return state.Commits[len(state.Commits)-1], nil - } - return state.Commits[lineNumber], nil + v, err := g.View("commits") + if err != nil { + panic(err) + } + if len(state.Commits) == 0 { + return Commit{}, ErrNoCommits + } + lineNumber := getItemPosition(v) + if lineNumber > len(state.Commits)-1 { + colorLog(color.FgRed, "potential error in getSelected Commit (mismatched ui and state)", state.Commits, lineNumber) + return state.Commits[len(state.Commits)-1], nil + } + return state.Commits[lineNumber], nil } diff --git a/status_panel.go b/status_panel.go index cbc6956a8..6e3ec35e8 100644 --- a/status_panel.go +++ b/status_panel.go @@ -1,43 +1,43 @@ package main import ( - "fmt" - "time" + "fmt" + "time" - "github.com/fatih/color" - "github.com/jesseduffield/gocui" + "github.com/fatih/color" + "github.com/jesseduffield/gocui" ) func refreshStatus(g *gocui.Gui) error { - v, err := g.View("status") - if err != nil { - panic(err) - } - // for some reason if this isn't wrapped in an update the clear seems to - // be applied after the other things or something like that; the panel's - // contents end up cleared - g.Update(func(*gocui.Gui) error { - v.Clear() - pushables, pullables := gitUpstreamDifferenceCount() - fmt.Fprint(v, "↑"+pushables+"↓"+pullables) - branches := state.Branches - if err := updateHasMergeConflictStatus(); err != nil { - return err - } - if state.HasMergeConflicts { - colour := color.New(color.FgYellow) - fmt.Fprint(v, coloredString(" (merging)", colour)) - } - if len(branches) == 0 { - return nil - } - branch := branches[0] - // utilising the fact these all have padding to only grab the name - // from the display string with the existing coloring applied - fmt.Fprint(v, " "+branch.DisplayString[4:]) - colorLog(color.FgCyan, time.Now().Sub(startTime)) - return nil - }) + v, err := g.View("status") + if err != nil { + panic(err) + } + // for some reason if this isn't wrapped in an update the clear seems to + // be applied after the other things or something like that; the panel's + // contents end up cleared + g.Update(func(*gocui.Gui) error { + v.Clear() + pushables, pullables := gitUpstreamDifferenceCount() + fmt.Fprint(v, "↑"+pushables+"↓"+pullables) + branches := state.Branches + if err := updateHasMergeConflictStatus(); err != nil { + return err + } + if state.HasMergeConflicts { + colour := color.New(color.FgYellow) + fmt.Fprint(v, coloredString(" (merging)", colour)) + } + if len(branches) == 0 { + return nil + } + branch := branches[0] + // utilising the fact these all have padding to only grab the name + // from the display string with the existing coloring applied + fmt.Fprint(v, " "+branch.DisplayString[4:]) + colorLog(color.FgCyan, time.Now().Sub(startTime)) + return nil + }) - return nil + return nil } From 684e8b2e80087f1ae019ca887b16c6f9d6b73d2b Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Mon, 6 Aug 2018 07:41:29 +0200 Subject: [PATCH 03/10] remove useless condition --- confirmation_panel.go | 6 ++---- files_panel.go | 5 +---- gui.go | 6 ++---- 3 files changed, 5 insertions(+), 12 deletions(-) diff --git a/confirmation_panel.go b/confirmation_panel.go index 16f9ac0c9..521361dfd 100644 --- a/confirmation_panel.go +++ b/confirmation_panel.go @@ -101,10 +101,8 @@ func setKeyBindings(g *gocui.Gui, handleYes, handleNo func(*gocui.Gui, *gocui.Vi if err := g.SetKeybinding("confirmation", gocui.KeyEnter, gocui.ModNone, wrappedConfirmationFunction(handleYes)); err != nil { return err } - if err := g.SetKeybinding("confirmation", gocui.KeyEsc, gocui.ModNone, wrappedConfirmationFunction(handleNo)); err != nil { - return err - } - return nil + + return g.SetKeybinding("confirmation", gocui.KeyEsc, gocui.ModNone, wrappedConfirmationFunction(handleNo)) } func createMessagePanel(g *gocui.Gui, currentView *gocui.View, title, prompt string) error { diff --git a/files_panel.go b/files_panel.go index 52d4137e3..ad4f9e809 100644 --- a/files_panel.go +++ b/files_panel.go @@ -59,11 +59,8 @@ func handleFilePress(g *gocui.Gui, v *gocui.View) error { if err := refreshFiles(g); err != nil { return err } - if err := handleFileSelect(g, v); err != nil { - return err - } - return nil + return handleFileSelect(g, v) } func getSelectedFile(g *gocui.Gui) (GitFile, error) { diff --git a/gui.go b/gui.go index 040cfdafd..98654d5a0 100644 --- a/gui.go +++ b/gui.go @@ -196,10 +196,8 @@ func keybindings(g *gocui.Gui) error { if err := g.SetKeybinding("stash", 'k', gocui.ModNone, handleStashPop); err != nil { return err } - if err := g.SetKeybinding("stash", 'd', gocui.ModNone, handleStashDrop); err != nil { - return err - } - return nil + + return g.SetKeybinding("stash", 'd', gocui.ModNone, handleStashDrop) } func layout(g *gocui.Gui) error { From 4836e6c9060538b2394d3b8ad7f5140f7439dae7 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Mon, 6 Aug 2018 07:41:59 +0200 Subject: [PATCH 04/10] remove useless else --- gui.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gui.go b/gui.go index 98654d5a0..ccb3ec8f2 100644 --- a/gui.go +++ b/gui.go @@ -226,10 +226,10 @@ func layout(g *gocui.Gui) error { v.Wrap = true } return nil - } else { - g.DeleteView("limit") } + g.DeleteView("limit") + optionsTop := height - 2 // hiding options if there's not enough space if height < 30 { From 6b5c08d44907c96643bf2dde32f675cf7c4a5576 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Mon, 6 Aug 2018 07:42:41 +0200 Subject: [PATCH 05/10] add goreportcard --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9fdddb0a4..07a07f139 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ -# lazygit +# lazygit [![Go Report Card](https://goreportcard.com/badge/github.com/jesseduffield/lazygit)](https://goreportcard.com/report/github.com/jesseduffield/lazygit) + A simple terminal UI for git commands, written in Go with the [gocui](https://github.com/jroimartin/gocui "gocui") library. are YOU tired of typing every git command directly into the terminal, but you're too stubborn to use Sourcetree because you'll never forgive Atlassian for making Jira? This is the app for you! @@ -20,7 +21,7 @@ If you want, you can also add an alias for this with `echo "alias lg='lazygit'" - Adding files easily - Resolving merge conflicts - Easily check out recent branches -- Scroll through logs/diffs of branches/commits/stash +- Scroll through logs/diffs of branches/commits/stash - Quick pushing/pulling - Squash down and rename commits From b1e6735d8dec036c486ae80b5cdffef86cec2396 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Mon, 6 Aug 2018 16:11:29 +1000 Subject: [PATCH 06/10] better contrast --- confirmation_panel.go | 4 +++- gui.go | 13 ++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/confirmation_panel.go b/confirmation_panel.go index 16f9ac0c9..a773c0059 100644 --- a/confirmation_panel.go +++ b/confirmation_panel.go @@ -63,10 +63,11 @@ func createPromptPanel(g *gocui.Gui, currentView *gocui.View, title string, hand return err } - confirmationView.Editable = true g.Cursor = true + confirmationView.Editable = true confirmationView.Title = title + confirmationView.FgColor = gocui.ColorWhite switchFocus(g, currentView, confirmationView) return setKeyBindings(g, handleYes, nil) } @@ -87,6 +88,7 @@ func createConfirmationPanel(g *gocui.Gui, currentView *gocui.View, title, promp return err } confirmationView.Title = title + confirmationView.FgColor = gocui.ColorWhite renderString(g, "confirmation", prompt) switchFocus(g, currentView, confirmationView) return setKeyBindings(g, handleYes, handleNo) diff --git a/gui.go b/gui.go index 040cfdafd..12d6eb07d 100644 --- a/gui.go +++ b/gui.go @@ -10,6 +10,7 @@ import ( "time" // "strings" + "github.com/golang-collections/collections/stack" "github.com/jesseduffield/gocui" ) @@ -204,7 +205,8 @@ func keybindings(g *gocui.Gui) error { func layout(g *gocui.Gui) error { g.Highlight = true - g.SelFgColor = gocui.AttrBold + g.SelFgColor = gocui.ColorWhite | gocui.AttrBold + g.FgColor = gocui.ColorBlack width, height := g.Size() leftSideWidth := width / 3 statusFilesBoundary := 2 @@ -245,6 +247,7 @@ func layout(g *gocui.Gui) error { } v.Title = "Diff" v.Wrap = true + v.FgColor = gocui.ColorWhite } if v, err := g.SetView("status", 0, 0, leftSideWidth, statusFilesBoundary, gocui.BOTTOM|gocui.RIGHT); err != nil { @@ -252,6 +255,7 @@ func layout(g *gocui.Gui) error { return err } v.Title = "Status" + v.FgColor = gocui.ColorWhite } filesView, err := g.SetView("files", 0, statusFilesBoundary+panelSpacing, leftSideWidth, filesBranchesBoundary, gocui.TOP|gocui.BOTTOM) @@ -261,6 +265,7 @@ func layout(g *gocui.Gui) error { } filesView.Highlight = true filesView.Title = "Files" + v.FgColor = gocui.ColorWhite } if v, err := g.SetView("branches", 0, filesBranchesBoundary+panelSpacing, leftSideWidth, commitsBranchesBoundary, gocui.TOP|gocui.BOTTOM); err != nil { @@ -268,7 +273,7 @@ func layout(g *gocui.Gui) error { return err } v.Title = "Branches" - + v.FgColor = gocui.ColorWhite } if v, err := g.SetView("commits", 0, commitsBranchesBoundary+panelSpacing, leftSideWidth, commitsStashBoundary, gocui.TOP|gocui.BOTTOM); err != nil { @@ -276,7 +281,7 @@ func layout(g *gocui.Gui) error { return err } v.Title = "Commits" - + v.FgColor = gocui.ColorWhite } if v, err := g.SetView("stash", 0, commitsStashBoundary+panelSpacing, leftSideWidth, optionsTop, gocui.TOP|gocui.RIGHT); err != nil { @@ -284,6 +289,7 @@ func layout(g *gocui.Gui) error { return err } v.Title = "Stash" + v.FgColor = gocui.ColorWhite } if v, err := g.SetView("options", -1, optionsTop, width, optionsTop+2, 0); err != nil { @@ -291,6 +297,7 @@ func layout(g *gocui.Gui) error { return err } v.BgColor = gocui.ColorBlue + v.FgColor = gocui.ColorWhite v.Frame = false v.Title = "Options" From e5f0a89bf16055b682e64102993c9414892b86b7 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Mon, 6 Aug 2018 16:31:24 +1000 Subject: [PATCH 07/10] better contrast for bottom options banner --- gui.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gui.go b/gui.go index 12d6eb07d..70f12beca 100644 --- a/gui.go +++ b/gui.go @@ -296,8 +296,8 @@ func layout(g *gocui.Gui) error { if err != gocui.ErrUnknownView { return err } - v.BgColor = gocui.ColorBlue - v.FgColor = gocui.ColorWhite + v.BgColor = gocui.ColorDefault + v.FgColor = gocui.ColorBlue v.Frame = false v.Title = "Options" From 61815013cb32566631dac5f84bcd233a8488a31b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Borbo=C3=ABn?= Date: Mon, 6 Aug 2018 09:05:16 +0200 Subject: [PATCH 08/10] Update README.md * punctuation added where missing; * capitalized first letter of sentences; * PR/Issue links. --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9fdddb0a4..61ede7d09 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # lazygit A simple terminal UI for git commands, written in Go with the [gocui](https://github.com/jroimartin/gocui "gocui") library. -are YOU tired of typing every git command directly into the terminal, but you're too stubborn to use Sourcetree because you'll never forgive Atlassian for making Jira? This is the app for you! +Are YOU tired of typing every git command directly into the terminal, but you're too stubborn to use Sourcetree because you'll never forgive Atlassian for making Jira? This is the app for you! [Tutorial](https://www.youtube.com/watch?v=VDXvbHZYeKY) @@ -10,11 +10,11 @@ are YOU tired of typing every git command directly into the terminal, but you're ## Installation In a terminal call this command: `go get github.com/jesseduffield/lazygit` -(if you don't have Go installed, you can follow the installation guide [Here](https://golang.org/doc/install) +(if you don't have Go installed, you can follow the installation guide [here](https://golang.org/doc/install). -then just call `lazygit` in your terminal inside a git repository +Then just call `lazygit` in your terminal inside a git repository. -If you want, you can also add an alias for this with `echo "alias lg='lazygit'" >> ~/.zshrc` (or whichever rc file you're using) +If you want, you can also add an alias for this with `echo "alias lg='lazygit'" >> ~/.zshrc` (or whichever rc file you're using). ## Cool features - Adding files easily @@ -31,4 +31,4 @@ If you want, you can also add an alias for this with `echo "alias lg='lazygit'" ![Viewing Commit Diffs](https://image.ibb.co/gPD02o/capture.png) ## Work in progress -This is still a work in progress so there's still bugs to iron out and as this is my first project in Go the code could no doubt use an increase in quality, but I'll be improving on it whenever I find the time. If you have any feedback feel free to raise an issue/submit a PR. +This is still a work in progress so there's still bugs to iron out and as this is my first project in Go the code could no doubt use an increase in quality, but I'll be improving on it whenever I find the time. If you have any feedback feel free to [raise an issue](https://github.com/jesseduffield/lazygit/issues)/[submit a PR](https://github.com/jesseduffield/lazygit/pulls). From b1918f2f688891efd34a190a210f10ec35c160f3 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Mon, 6 Aug 2018 18:55:08 +1000 Subject: [PATCH 09/10] no panic on git commit error --- files_panel.go | 4 ++-- gitcommands.go | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/files_panel.go b/files_panel.go index ad4f9e809..50d6b64e2 100644 --- a/files_panel.go +++ b/files_panel.go @@ -160,8 +160,8 @@ func handleCommitPress(g *gocui.Gui, filesView *gocui.View) error { if message == "" { return createErrorPanel(g, "You cannot commit without a commit message") } - if err := gitCommit(message); err != nil { - panic(err) + if output, err := gitCommit(message); err != nil { + return createErrorPanel(g, output) } refreshFiles(g) return refreshCommits(g) diff --git a/gitcommands.go b/gitcommands.go index 719a8ceb0..d537672a0 100644 --- a/gitcommands.go +++ b/gitcommands.go @@ -445,9 +445,8 @@ func removeFile(file GitFile) error { return err } -func gitCommit(message string) error { - _, err := runDirectCommand("git commit -m \"" + message + "\"") - return err +func gitCommit(message string) (string, error) { + return runCommand("git commit -m \"" + message + "\"") } func gitPull() (string, error) { From cf73d4f558a1c180a52f9be4768aa9b754d4b4b3 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Mon, 6 Aug 2018 19:01:27 +1000 Subject: [PATCH 10/10] standardise error handling of command functions --- gitcommands.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/gitcommands.go b/gitcommands.go index d537672a0..27e4e8369 100644 --- a/gitcommands.go +++ b/gitcommands.go @@ -119,8 +119,7 @@ func runDirectCommand(command string) (string, error) { Command("bash", "-c", command). CombinedOutput() devLog("run direct command time for command: ", command, time.Now().Sub(timeStart)) - - return string(cmdOut), err + return sanitisedCommandOutput(cmdOut, err) } func branchStringParts(branchString string) (string, string) { @@ -299,17 +298,21 @@ func gitCheckout(branch string, force bool) (string, error) { return runCommand("git checkout " + forceArg + branch) } +func sanitisedCommandOutput(output []byte, err error) (string, error) { + outputString := string(output) + if outputString == "" && err != nil { + return err.Error(), err + } + return outputString, err +} + func runCommand(command string) (string, error) { commandStartTime := time.Now() commandLog(command) splitCmd := strings.Split(command, " ") cmdOut, err := exec.Command(splitCmd[0], splitCmd[1:]...).CombinedOutput() devLog("run command time: ", time.Now().Sub(commandStartTime)) - outputString := string(cmdOut) - if outputString == "" && err != nil { - return err.Error(), err - } - return outputString, err + return sanitisedCommandOutput(cmdOut, err) } func openFile(filename string) (string, error) { @@ -446,7 +449,7 @@ func removeFile(file GitFile) error { } func gitCommit(message string) (string, error) { - return runCommand("git commit -m \"" + message + "\"") + return runDirectCommand("git commit -m \"" + message + "\"") } func gitPull() (string, error) {