Skip to content

Commit

Permalink
Merge branch 'master' into custom-keybindings
Browse files Browse the repository at this point in the history
  • Loading branch information
David Chen authored Jan 7, 2020
2 parents 529ba45 + 09aabce commit 844a2db
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 23 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mattn/go-isatty v0.0.11 // indirect
github.com/mattn/go-runewidth v0.0.7 // indirect
github.com/mattn/go-runewidth v0.0.7
github.com/mgutz/str v1.2.0
github.com/nicksnyder/go-i18n/v2 v2.0.3
github.com/onsi/ginkgo v1.10.3 // indirect
Expand Down
16 changes: 8 additions & 8 deletions pkg/commands/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func includesInt(list []int, a int) bool {

// ResetAndClean removes all unstaged changes and removes all untracked files
func (c *GitCommand) ResetAndClean() error {
if err := c.ResetHardHead(); err != nil {
if err := c.ResetHard("HEAD"); err != nil {
return err
}

Expand Down Expand Up @@ -515,7 +515,7 @@ func (c *GitCommand) DiscardUnstagedFileChanges(file *File) error {
return c.OSCommand.RunCommand("git checkout -- %s", quotedFileName)
}

// Checkout checks out a branch, with --force if you set the force arg to true
// Checkout checks out a branch (or commit), with --force if you set the force arg to true
func (c *GitCommand) Checkout(branch string, force bool) error {
forceArg := ""
if force {
Expand Down Expand Up @@ -961,14 +961,14 @@ func (c *GitCommand) RemoveUntrackedFiles() error {
return c.OSCommand.RunCommand("git clean -fd")
}

// ResetHardHead runs `git reset --hard HEAD`
func (c *GitCommand) ResetHardHead() error {
return c.OSCommand.RunCommand("git reset --hard HEAD")
// ResetHardHead runs `git reset --hard`
func (c *GitCommand) ResetHard(ref string) error {
return c.OSCommand.RunCommand("git reset --hard " + ref)
}

// ResetSoftHead runs `git reset --soft HEAD`
func (c *GitCommand) ResetSoftHead() error {
return c.OSCommand.RunCommand("git reset --soft HEAD")
// ResetSoft runs `git reset --soft HEAD`
func (c *GitCommand) ResetSoft(ref string) error {
return c.OSCommand.RunCommand("git reset --soft " + ref)
}

// DiffCommits show diff between commits
Expand Down
8 changes: 5 additions & 3 deletions pkg/commands/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2106,17 +2106,19 @@ func TestGitCommandRemoveUntrackedFiles(t *testing.T) {
}
}

// TestGitCommandResetHardHead is a function.
func TestGitCommandResetHardHead(t *testing.T) {
// TestGitCommandResetHard is a function.
func TestGitCommandResetHard(t *testing.T) {
type scenario struct {
testName string
ref string
command func(string, ...string) *exec.Cmd
test func(error)
}

scenarios := []scenario{
{
"valid case",
"HEAD",
test.CreateMockCommand(t, []*test.CommandSwapper{
{
Expect: `git reset --hard HEAD`,
Expand All @@ -2134,7 +2136,7 @@ func TestGitCommandResetHardHead(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd.OSCommand.command = s.command
s.test(gitCmd.ResetHardHead())
s.test(gitCmd.ResetHard(s.ref))
})
}
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/gui/branches_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (gui *Gui) handleBranchPress(g *gocui.Gui, v *gocui.View) error {
return gui.createErrorPanel(g, gui.Tr.SLocalize("AlreadyCheckedOutBranch"))
}
branch := gui.getSelectedBranch()
return gui.handleCheckoutBranch(branch.Name)
return gui.handleCheckoutRef(branch.Name)
}

func (gui *Gui) handleCreatePullRequestPress(g *gocui.Gui, v *gocui.View) error {
Expand Down Expand Up @@ -165,17 +165,17 @@ func (gui *Gui) handleForceCheckout(g *gocui.Gui, v *gocui.View) error {
}, nil)
}

func (gui *Gui) handleCheckoutBranch(branchName string) error {
if err := gui.GitCommand.Checkout(branchName, false); err != nil {
func (gui *Gui) handleCheckoutRef(ref string) error {
if err := gui.GitCommand.Checkout(ref, false); err != nil {
// note, this will only work for english-language git commands. If we force git to use english, and the error isn't this one, then the user will receive an english command they may not understand. I'm not sure what the best solution to this is. Running the command once in english and a second time in the native language is one option

if strings.Contains(err.Error(), "Please commit your changes or stash them before you switch branch") {
// offer to autostash changes
return gui.createConfirmationPanel(gui.g, gui.getBranchesView(), true, gui.Tr.SLocalize("AutoStashTitle"), gui.Tr.SLocalize("AutoStashPrompt"), func(g *gocui.Gui, v *gocui.View) error {
if err := gui.GitCommand.StashSave(gui.Tr.SLocalize("StashPrefix") + branchName); err != nil {
if err := gui.GitCommand.StashSave(gui.Tr.SLocalize("StashPrefix") + ref); err != nil {
return gui.createErrorPanel(g, err.Error())
}
if err := gui.GitCommand.Checkout(branchName, false); err != nil {
if err := gui.GitCommand.Checkout(ref, false); err != nil {
return gui.createErrorPanel(g, err.Error())
}

Expand All @@ -198,12 +198,13 @@ func (gui *Gui) handleCheckoutBranch(branchName string) error {
}

gui.State.Panels.Branches.SelectedLine = 0
gui.State.Panels.Commits.SelectedLine = 0
return gui.refreshSidePanels(gui.g)
}

func (gui *Gui) handleCheckoutByName(g *gocui.Gui, v *gocui.View) error {
gui.createPromptPanel(g, v, gui.Tr.SLocalize("BranchName")+":", "", func(g *gocui.Gui, v *gocui.View) error {
return gui.handleCheckoutBranch(gui.trimmedContent(v))
return gui.handleCheckoutRef(gui.trimmedContent(v))
})
return nil
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/gui/commits_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,3 +611,14 @@ func (gui *Gui) handleCreateLightweightTag(commitSha string) error {
return gui.handleCommitSelect(g, v)
})
}

func (gui *Gui) handleCheckoutCommit(g *gocui.Gui, v *gocui.View) error {
commit := gui.getSelectedCommit(g)
if commit == nil {
return gui.renderString(g, "main", gui.Tr.SLocalize("NoCommitsThisBranch"))
}

return gui.createConfirmationPanel(g, gui.getCommitsView(), true, gui.Tr.SLocalize("checkoutCommit"), gui.Tr.SLocalize("SureCheckoutThisCommit"), func(g *gocui.Gui, v *gocui.View) error {
return gui.handleCheckoutRef(commit.Sha)
}, nil)
}
13 changes: 10 additions & 3 deletions pkg/gui/files_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,14 +604,21 @@ func (gui *Gui) handleCreateResetMenu(g *gocui.Gui, v *gocui.View) error {
description: gui.Tr.SLocalize("softReset"),
command: "git reset --soft HEAD",
handler: func() error {
return gui.GitCommand.ResetSoftHead()
return gui.GitCommand.ResetSoft("HEAD")
},
},
{
description: gui.Tr.SLocalize("hardReset"),
command: "git reset --hard HEAD",
handler: func() error {
return gui.GitCommand.ResetHardHead()
return gui.GitCommand.ResetHard("HEAD")
},
},
{
description: gui.Tr.SLocalize("hardResetUpstream"),
command: "git reset --hard @{upstream}",
handler: func() error {
return gui.GitCommand.ResetHard("@{upstream}")
},
},
{
Expand All @@ -624,7 +631,7 @@ func (gui *Gui) handleCreateResetMenu(g *gocui.Gui, v *gocui.View) error {

handleMenuPress := func(index int) error {
if err := options[index].handler(); err != nil {
return err
return gui.createErrorPanel(gui.g, err.Error())
}

return gui.refreshFiles()
Expand Down
4 changes: 4 additions & 0 deletions pkg/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"math"
"os"
"runtime"
"sync"

// "io"
Expand All @@ -28,6 +29,7 @@ import (
"github.com/jesseduffield/lazygit/pkg/theme"
"github.com/jesseduffield/lazygit/pkg/updates"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/mattn/go-runewidth"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -782,6 +784,8 @@ func (gui *Gui) Run() error {
}
defer g.Close()

g.ASCII = runtime.GOOS == "windows" && runewidth.IsEastAsian()

if gui.Config.GetUserConfig().GetBool("gui.mouseEvents") {
g.Mouse = true
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/gui/keybindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,13 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
ViewName: "commits",
Key: gui.getKey("universal.select"),
Modifier: gocui.ModNone,
Handler: gui.handleCheckoutCommit,
Description: gui.Tr.SLocalize("checkoutCommit"),
},
{
ViewName: "commits",
Key: 'h',
Modifier: gocui.ModNone,
Handler: gui.handleToggleDiffCommit,
Description: gui.Tr.SLocalize("CommitsDiff"),
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/remote_branches_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (gui *Gui) handleCheckoutRemoteBranch(g *gocui.Gui, v *gocui.View) error {
if remoteBranch == nil {
return nil
}
if err := gui.handleCheckoutBranch(remoteBranch.RemoteName + "/" + remoteBranch.Name); err != nil {
if err := gui.handleCheckoutRef(remoteBranch.RemoteName + "/" + remoteBranch.Name); err != nil {
return err
}
return gui.switchBranchesPanelContext("local-branches")
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/tags_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (gui *Gui) handleCheckoutTag(g *gocui.Gui, v *gocui.View) error {
if tag == nil {
return nil
}
if err := gui.handleCheckoutBranch(tag.Name); err != nil {
if err := gui.handleCheckoutRef(tag.Name); err != nil {
return err
}
return gui.switchBranchesPanelContext("local-branches")
Expand Down
9 changes: 9 additions & 0 deletions pkg/i18n/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,9 @@ func addEnglish(i18nObject *i18n.Bundle) error {
}, &i18n.Message{
ID: "hardReset",
Other: "hard reset",
}, &i18n.Message{
ID: "hardResetUpstream",
Other: "hard reset to upstream branch",
}, &i18n.Message{
ID: "viewResetOptions",
Other: `view reset options`,
Expand Down Expand Up @@ -921,6 +924,12 @@ func addEnglish(i18nObject *i18n.Bundle) error {
}, &i18n.Message{
ID: "FetchingRemoteStatus",
Other: "fetching remote",
}, &i18n.Message{
ID: "checkoutCommit",
Other: "checkout commit",
}, &i18n.Message{
ID: "SureCheckoutThisCommit",
Other: "Are you sure you want to checkout this commit?",
},
)
}

0 comments on commit 844a2db

Please sign in to comment.