summaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2018-09-25 20:11:36 +1000
committerJesse Duffield <jessedduffield@gmail.com>2018-09-25 20:11:36 +1000
commit0d33a746ba679a2ca23066aa9bea3305506aae14 (patch)
tree5d6ceaa5334f421fa72cfa1214e19985ae9cfdb2 /pkg
parentf3fc98a3d03756ec94bcad955eda268fddb3613a (diff)
parent17d7bcdeafe35d60dd4ebb553a6c46a716e5df92 (diff)
Merge branch 'feature/informative-commit-colors' of https://github.com/jesseduffield/lazygit into feature/informative-commit-colors
Diffstat (limited to 'pkg')
-rw-r--r--pkg/commands/git.go12
-rw-r--r--pkg/commands/git_test.go179
-rw-r--r--pkg/gui/commit_message_panel.go4
-rw-r--r--pkg/gui/commits_panel.go5
-rw-r--r--pkg/gui/status_panel.go19
-rw-r--r--pkg/gui/view_helpers.go13
-rw-r--r--pkg/utils/utils.go10
-rw-r--r--pkg/utils/utils_test.go35
8 files changed, 178 insertions, 99 deletions
diff --git a/pkg/commands/git.go b/pkg/commands/git.go
index 0bfbc36a7..4d132237f 100644
--- a/pkg/commands/git.go
+++ b/pkg/commands/git.go
@@ -543,26 +543,22 @@ func (c *GitCommand) Ignore(filename string) error {
}
// Show shows the diff of a commit
-func (c *GitCommand) Show(sha string) string {
- result, err := c.OSCommand.RunCommandWithOutput("git show --color " + sha)
- if err != nil {
- panic(err)
- }
- return result
+func (c *GitCommand) Show(sha string) (string, error) {
+ return c.OSCommand.RunCommandWithOutput(fmt.Sprintf("git show --color %s", sha))
}
// Diff returns the diff of a file
func (c *GitCommand) Diff(file *File) string {
cachedArg := ""
+ trackedArg := "--"
fileName := c.OSCommand.Quote(file.Name)
if file.HasStagedChanges && !file.HasUnstagedChanges {
cachedArg = "--cached"
}
- trackedArg := "--"
if !file.Tracked && !file.HasStagedChanges {
trackedArg = "--no-index /dev/null"
}
- command := fmt.Sprintf("%s %s %s %s", "git diff --color ", cachedArg, trackedArg, fileName)
+ command := fmt.Sprintf("git diff --color %s %s %s", cachedArg, trackedArg, fileName)
// for now we assume an error means the file was deleted
s, _ := c.OSCommand.RunCommandWithOutput(command)
diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go
index e5faa707b..5a331ea18 100644
--- a/pkg/commands/git_test.go
+++ b/pkg/commands/git_test.go
@@ -9,7 +9,6 @@ import (
"time"
"github.com/jesseduffield/lazygit/pkg/i18n"
- "github.com/jesseduffield/lazygit/pkg/test"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
gogit "gopkg.in/src-d/go-git.v4"
@@ -1421,6 +1420,19 @@ func TestGitCommandRemoveFile(t *testing.T) {
}
}
+func TestGitCommandShow(t *testing.T) {
+ gitCmd := newDummyGitCommand()
+ gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd {
+ assert.EqualValues(t, "git", cmd)
+ assert.EqualValues(t, []string{"show", "--color", "456abcde"}, args)
+
+ return exec.Command("echo")
+ }
+
+ _, err := gitCmd.Show("456abcde")
+ assert.NoError(t, err)
+}
+
func TestGitCommandCheckout(t *testing.T) {
type scenario struct {
testName string
@@ -1569,97 +1581,106 @@ func TestGitCommandGetCommits(t *testing.T) {
}
}
-func TestGitCommandDiff(t *testing.T) {
- gitCommand := newDummyGitCommand()
- assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh"))
+func TestGitCommandGetLog(t *testing.T) {
+ type scenario struct {
+ testName string
+ command func(string, ...string) *exec.Cmd
+ test func(string)
+ }
- files := []*File{
- {
- Name: "deleted_staged",
- HasStagedChanges: false,
- HasUnstagedChanges: true,
- Tracked: true,
- Deleted: true,
- HasMergeConflicts: false,
- DisplayString: " D deleted_staged",
- },
- {
- Name: "file with space staged",
- HasStagedChanges: true,
- HasUnstagedChanges: false,
- Tracked: false,
- Deleted: false,
- HasMergeConflicts: false,
- DisplayString: "A \"file with space staged\"",
- },
- {
- Name: "file with space unstaged",
- HasStagedChanges: false,
- HasUnstagedChanges: true,
- Tracked: false,
- Deleted: false,
- HasMergeConflicts: false,
- DisplayString: "?? file with space unstaged",
- },
- {
- Name: "modified_unstaged",
- HasStagedChanges: true,
- HasUnstagedChanges: false,
- Tracked: true,
- Deleted: false,
- HasMergeConflicts: false,
- DisplayString: "M modified_unstaged",
- },
+ scenarios := []scenario{
{
- Name: "modified_staged",
- HasStagedChanges: false,
- HasUnstagedChanges: true,
- Tracked: true,
- Deleted: false,
- HasMergeConflicts: false,
- DisplayString: " M modified_staged",
+ "Retrieves logs",
+ func(cmd string, args ...string) *exec.Cmd {
+ assert.EqualValues(t, "git", cmd)
+ assert.EqualValues(t, []string{"log", "--oneline", "-30"}, args)
+
+ return exec.Command("echo", "6f0b32f commands/git : add GetCommits tests refactor\n9d9d775 circle : remove new line")
+ },
+ func(output string) {
+ assert.EqualValues(t, "6f0b32f commands/git : add GetCommits tests refactor\n9d9d775 circle : remove new line\n", output)
+ },
},
{
- Name: "renamed_before -> renamed_after",
- HasStagedChanges: true,
- HasUnstagedChanges: false,
- Tracked: true,
- Deleted: false,
- HasMergeConflicts: false,
- DisplayString: "R renamed_before -> renamed_after",
+ "An error occurred when retrieving logs",
+ func(cmd string, args ...string) *exec.Cmd {
+ assert.EqualValues(t, "git", cmd)
+ assert.EqualValues(t, []string{"log", "--oneline", "-30"}, args)
+ return exec.Command("test")
+ },
+ func(output string) {
+ assert.Empty(t, output)
+ },
},
+ }
+
+ for _, s := range scenarios {
+ t.Run(s.testName, func(t *testing.T) {
+ gitCmd := newDummyGitCommand()
+ gitCmd.OSCommand.command = s.command
+ s.test(gitCmd.GetLog())
+ })
+ }
+}
+
+func TestGitCommandDiff(t *testing.T) {
+ type scenario struct {
+ testName string
+ command func(string, ...string) *exec.Cmd
+ file *File
+ }
+
+ scenarios := []scenario{
{
- Name: "untracked_unstaged",
- HasStagedChanges: false,
- HasUnstagedChanges: true,
- Tracked: false,
- Deleted: false,
- HasMergeConflicts: false,
- DisplayString: "?? untracked_unstaged",
+ "Default case",
+ func(cmd string, args ...string) *exec.Cmd {
+ assert.EqualValues(t, "git", cmd)
+ assert.EqualValues(t, []string{"diff", "--color", "--", "test.txt"}, args)
+
+ return exec.Command("echo")
+ },
+ &File{
+ Name: "test.txt",
+ HasStagedChanges: false,
+ Tracked: true,
+ },
},
{
- Name: "untracked_staged",
- HasStagedChanges: true,
- HasUnstagedChanges: false,
- Tracked: false,
- Deleted: false,
- HasMergeConflicts: false,
- DisplayString: "A untracked_staged",
+ "All changes staged",
+ func(cmd string, args ...string) *exec.Cmd {
+ assert.EqualValues(t, "git", cmd)
+ assert.EqualValues(t, []string{"diff", "--color", "--cached", "--", "test.txt"}, args)
+
+ return exec.Command("echo")
+ },
+ &File{
+ Name: "test.txt",
+ HasStagedChanges: true,
+ HasUnstagedChanges: false,
+ Tracked: true,
+ },
},
{
- Name: "master",
- HasStagedChanges: false,
- HasUnstagedChanges: true,
- Tracked: false,
- Deleted: false,
- HasMergeConflicts: false,
- DisplayString: "?? master",
+ "File not tracked and file has no staged changes",
+ func(cmd string, args ...string) *exec.Cmd {
+ assert.EqualValues(t, "git", cmd)
+ assert.EqualValues(t, []string{"diff", "--color", "--no-index", "/dev/null", "test.txt"}, args)
+
+ return exec.Command("echo")
+ },
+ &File{
+ Name: "test.txt",
+ HasStagedChanges: false,
+ Tracked: false,
+ },
},
}
- for _, file := range files {
- t.Run(file.Name, func(t *testing.T) {
- assert.NotContains(t, gitCommand.Diff(file), "error")
+ for _, s := range scenarios {
+ t.Run(s.testName, func(t *testing.T) {
+ gitCmd := newDummyGitCommand()
+ gitCmd.OSCommand.command = s.command
+ gitCmd.Diff(s.file)
})
}
}
diff --git a/pkg/gui/commit_message_panel.go b/pkg/gui/commit_message_panel.go
index e23c47da0..74e02be90 100644
--- a/pkg/gui/commit_message_panel.go
+++ b/pkg/gui/commit_message_panel.go
@@ -37,6 +37,10 @@ func (gui *Gui) handleCommitClose(g *gocui.Gui, v *gocui.View) error {
}
func (gui *Gui) handleCommitFocused(g *gocui.Gui, v *gocui.View) error {
+ if _, err := g.SetViewOnTop("commitMessage"); err != nil {
+ return err
+ }
+
message := gui.Tr.TemplateLocalize(
"CloseConfirm",
Teml{
diff --git a/pkg/gui/commits_panel.go b/pkg/gui/commits_panel.go
index a86401ebf..ee7f191a7 100644
--- a/pkg/gui/commits_panel.go
+++ b/pkg/gui/commits_panel.go
@@ -74,7 +74,10 @@ func (gui *Gui) handleCommitSelect(g *gocui.Gui, v *gocui.View) error {
}
return gui.renderString(g, "main", gui.Tr.SLocalize("NoCommitsThisBranch"))
}
- commitText := gui.GitCommand.Show(commit.Sha)
+ commitText, err := gui.GitCommand.Show(commit.Sha)
+ if err != nil {
+ return err
+ }
return gui.renderString(g, "main", commitText)
}
diff --git a/pkg/gui/status_panel.go b/pkg/gui/status_panel.go
index 6948e9678..89b3e997e 100644
--- a/pkg/gui/status_panel.go
+++ b/pkg/gui/status_panel.go
@@ -2,6 +2,7 @@ package gui
import (
"fmt"
+ "strings"
"github.com/fatih/color"
"github.com/jesseduffield/gocui"
@@ -51,14 +52,16 @@ func (gui *Gui) handleCheckForUpdate(g *gocui.Gui, v *gocui.View) error {
}
func (gui *Gui) handleStatusSelect(g *gocui.Gui, v *gocui.View) error {
- dashboardString := fmt.Sprintf(
- "%s\n\n%s\n\n%s\n\n%s\n\n%s",
- lazygitTitle(),
- "Keybindings: https://github.com/jesseduffield/lazygit/blob/master/docs/Keybindings.md",
- "Config Options: https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md",
- "Tutorial: https://www.youtube.com/watch?v=VDXvbHZYeKY",
- "Raise an Issue: https://github.com/jesseduffield/lazygit/issues",
- )
+ dashboardString := strings.Join(
+ []string{
+ lazygitTitle(),
+ "Copyright (c) 2018 Jesse Duffield",
+ "Keybindings: https://github.com/jesseduffield/lazygit/blob/master/docs/Keybindings.md",
+ "Config Options: https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md",
+ "Tutorial: https://www.youtube.com/watch?v=VDXvbHZYeKY",
+ "Raise an Issue: https://github.com/jesseduffield/lazygit/issues",
+ "Buy Jesse a coffee: https://donorbox.org/lazygit",
+ }, "\n\n")
if err := gui.renderString(g, "main", dashboardString); err != nil {
return err
diff --git a/pkg/gui/view_helpers.go b/pkg/gui/view_helpers.go
index 5178bd4d9..6c3e5505c 100644
--- a/pkg/gui/view_helpers.go
+++ b/pkg/gui/view_helpers.go
@@ -133,8 +133,15 @@ func (gui *Gui) switchFocus(g *gocui.Gui, oldView, newView *gocui.View) error {
},
)
gui.Log.Info(message)
- gui.State.PreviousView = oldView.Name()
+
+ // second class panels should never have focus restored to them because
+ // once they lose focus they are effectively 'destroyed'
+ secondClassPanels := []string{"confirmation", "menu"}
+ if !utils.IncludesString(secondClassPanels, oldView.Name()) {
+ gui.State.PreviousView = oldView.Name()
+ }
}
+
newView.Highlight = true
message := gui.Tr.TemplateLocalize(
"newFocusedViewIs",
@@ -183,7 +190,7 @@ func (gui *Gui) cursorDown(g *gocui.Gui, v *gocui.View) error {
}
cx, cy := v.Cursor()
ox, oy := v.Origin()
- ly := len(v.BufferLines()) - 1
+ ly := v.LinesHeight() - 1
_, height := v.Size()
maxY := height - 1
@@ -219,7 +226,7 @@ func (gui *Gui) correctCursor(v *gocui.View) error {
ox, oy := v.Origin()
_, height := v.Size()
maxY := height - 1
- ly := len(v.BufferLines()) - 1
+ ly := v.LinesHeight() - 1
if oy+cy <= ly {
return nil
}
diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go
index aef653c50..8e481b3a4 100644
--- a/pkg/utils/utils.go
+++ b/pkg/utils/utils.go
@@ -204,3 +204,13 @@ func getDisplayStringArrays(displayables []Displayable) [][]string {
}
return stringArrays
}
+
+// IncludesString if the list contains the string
+func IncludesString(list []string, a string) bool {
+ for _, b := range list {
+ if b == a {
+ return true
+ }
+ }
+ return false
+}
diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go
index 918031512..21e1d5031 100644
--- a/pkg/utils/utils_test.go
+++ b/pkg/utils/utils_test.go
@@ -376,3 +376,38 @@ func TestMin(t *testing.T) {
assert.EqualValues(t, s.expected, Min(s.a, s.b))
}
}
+
+func TestIncludesString(t *testing.T) {
+ type scenario struct {
+ list []string
+ element string
+ expected bool
+ }
+
+ scenarios := []scenario{
+ {
+ []string{"a", "b"},
+ "a",
+ true,
+ },
+ {
+ []string{"a", "b"},
+ "c",
+ false,
+ },
+ {
+ []string{"a", "b"},
+ "",
+ false,
+ },
+ {
+ []string{""},
+ "",
+ true,
+ },
+ }
+
+ for _, s := range scenarios {
+ assert.EqualValues(t, s.expected, IncludesString(s.list, s.element))
+ }
+}