Skip to content

Commit

Permalink
Make merge panel its own panel
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Dec 11, 2018
1 parent e0ff46f commit 9489a94
Show file tree
Hide file tree
Showing 20 changed files with 467 additions and 218 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ lazygit
!.gitignore
!.goreleaser.yml
!.circleci/
!.github/
!.github/

test/git_server/data
14 changes: 11 additions & 3 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"io"
"io/ioutil"
"os"
"path/filepath"

"github.com/heroku/rollrus"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/gui"
"github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/jesseduffield/lazygit/pkg/updates"
"github.com/shibukawa/configdir"
"github.com/sirupsen/logrus"
)

Expand All @@ -33,9 +35,15 @@ func newProductionLogger(config config.AppConfigurer) *logrus.Logger {
return log
}

func newDevelopmentLogger() *logrus.Logger {
func globalConfigDir() string {
configDirs := configdir.New("jesseduffield", "lazygit")
configDir := configDirs.QueryFolders(configdir.Global)[0]
return configDir.Path
}

func newDevelopmentLogger(config config.AppConfigurer) *logrus.Logger {
log := logrus.New()
file, err := os.OpenFile("development.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
file, err := os.OpenFile(filepath.Join(globalConfigDir(), "development.log"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
panic("unable to log to file") // TODO: don't panic (also, remove this call to the `panic` function)
}
Expand All @@ -48,7 +56,7 @@ func newLogger(config config.AppConfigurer) *logrus.Entry {
environment := "production"
if config.GetDebug() {
environment = "development"
log = newDevelopmentLogger()
log = newDevelopmentLogger(config)
} else {
log = newProductionLogger(config)
}
Expand Down
43 changes: 42 additions & 1 deletion pkg/commands/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,14 @@ func (c *GitCommand) AbortRebaseBranch() error {
return c.OSCommand.RunCommand("git rebase --abort")
}

func (c *GitCommand) ContinueMergeBranch() error {
return c.OSCommand.RunCommand("git merge --continue")
}

func (c *GitCommand) AbortMergeBranch() error {
return c.OSCommand.RunCommand("git merge --abort")
}

// Fetch fetch git repo
func (c *GitCommand) Fetch() error {
return c.OSCommand.RunCommand("git fetch")
Expand Down Expand Up @@ -682,7 +690,40 @@ func (c *GitCommand) Ignore(filename string) error {

// Show shows the diff of a commit
func (c *GitCommand) Show(sha string) (string, error) {
return c.OSCommand.RunCommandWithOutput(fmt.Sprintf("git show --color %s", sha))
show, err := c.OSCommand.RunCommandWithOutput(fmt.Sprintf("git show --color %s", sha))
if err != nil {
return "", err
}

// if this is a merge commit, we need to go a step further and get the diff between the two branches we merged
revList, err := c.OSCommand.RunCommandWithOutput(fmt.Sprintf("git rev-list -1 --merges %s^...%s", sha, sha))
if err != nil {
// turns out we get an error here when it's the first commit. We'll just return the original show
return show, nil
}
if len(revList) == 0 {
return show, nil
}

// we want to pull out 1a6a69a and 3b51d7c from this:
// commit ccc771d8b13d5b0d4635db4463556366470fd4f6
// Merge: 1a6a69a 3b51d7c
lines := utils.SplitLines(show)
if len(lines) < 2 {
return show, nil
}

secondLineWords := strings.Split(lines[1], " ")
if len(secondLineWords) < 3 {
return show, nil
}

mergeDiff, err := c.OSCommand.RunCommandWithOutput(fmt.Sprintf("git diff --color %s...%s", secondLineWords[1], secondLineWords[2]))
if err != nil {
return "", err
}

return show + mergeDiff, nil
}

// GetRemoteURL returns current repo remote url
Expand Down
32 changes: 23 additions & 9 deletions pkg/gui/branches_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (gui *Gui) RenderSelectedBranchUpstreamDifferences() error {

branch := gui.getSelectedBranch()
branch.Pushables, branch.Pullables = gui.GitCommand.GetBranchUpstreamDifferenceCount(branch.Name)
return gui.renderListPanel(gui.getBranchesView(gui.g), gui.State.Branches)
return gui.renderListPanel(gui.getBranchesView(), gui.State.Branches)
}

// gui.refreshStatus is called at the end of this because that's when we can
Expand All @@ -67,9 +67,6 @@ func (gui *Gui) refreshBranches(g *gocui.Gui) error {
gui.State.Branches = builder.Build()

gui.refreshSelectedLine(&gui.State.Panels.Branches.SelectedLine, len(gui.State.Branches))
if err := gui.resetOrigin(gui.getBranchesView(gui.g)); err != nil {
return err
}
if err := gui.RenderSelectedBranchUpstreamDifferences(); err != nil {
return err
}
Expand All @@ -82,13 +79,20 @@ func (gui *Gui) refreshBranches(g *gocui.Gui) error {
func (gui *Gui) handleBranchesNextLine(g *gocui.Gui, v *gocui.View) error {
panelState := gui.State.Panels.Branches
gui.changeSelectedLine(&panelState.SelectedLine, len(gui.State.Branches), false)

if err := gui.resetOrigin(gui.getMainView()); err != nil {
return err
}
return gui.handleBranchSelect(gui.g, v)
}

func (gui *Gui) handleBranchesPrevLine(g *gocui.Gui, v *gocui.View) error {
panelState := gui.State.Panels.Branches
gui.changeSelectedLine(&panelState.SelectedLine, len(gui.State.Branches), true)

if err := gui.resetOrigin(gui.getMainView()); err != nil {
return err
}
return gui.handleBranchSelect(gui.g, v)
}

Expand All @@ -108,18 +112,16 @@ func (gui *Gui) handleRebase(g *gocui.Gui, v *gocui.View) error {
}

if err := gui.GitCommand.RebaseBranch(selectedBranch); err != nil {
gui.Log.Errorln(err)
if err := gui.createConfirmationPanel(g, v, "Rebase failed", "Damn, conflicts! To abort press 'esc', otherwise press 'enter'",
return gui.createConfirmationPanel(g, v, "Auto-rebase failed", gui.Tr.SLocalize("FoundConflicts"),
func(g *gocui.Gui, v *gocui.View) error {
return nil
}, func(g *gocui.Gui, v *gocui.View) error {
if err := gui.GitCommand.AbortRebaseBranch(); err != nil {
return err
}
return gui.refreshSidePanels(g)
}); err != nil {
gui.Log.Errorln(err)
}
},
)
}

return gui.refreshSidePanels(g)
Expand Down Expand Up @@ -251,6 +253,18 @@ func (gui *Gui) handleMerge(g *gocui.Gui, v *gocui.View) error {
return gui.createErrorPanel(g, gui.Tr.SLocalize("CantMergeBranchIntoItself"))
}
if err := gui.GitCommand.Merge(selectedBranch.Name); err != nil {
if strings.Contains(err.Error(), "fix conflicts") {
return gui.createConfirmationPanel(g, v, "Auto-merge failed", gui.Tr.SLocalize("FoundConflicts"),
func(g *gocui.Gui, v *gocui.View) error {
return nil
}, func(g *gocui.Gui, v *gocui.View) error {
if err := gui.GitCommand.AbortMergeBranch(); err != nil {
return err
}
return gui.refreshSidePanels(g)
},
)
}
return gui.createErrorPanel(g, err.Error())
}
return nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/gui/commit_message_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ func (gui *Gui) handleCommitConfirm(g *gocui.Gui, v *gocui.View) error {
_ = v.SetCursor(0, 0)
_ = v.SetOrigin(0, 0)
_, _ = g.SetViewOnBottom("commitMessage")
_ = gui.switchFocus(g, v, gui.getFilesView(g))
_ = gui.switchFocus(g, v, gui.getFilesView())
return gui.refreshSidePanels(g)
}

func (gui *Gui) handleCommitClose(g *gocui.Gui, v *gocui.View) error {
g.SetViewOnBottom("commitMessage")
return gui.switchFocus(g, v, gui.getFilesView(g))
return gui.switchFocus(g, v, gui.getFilesView())
}

func (gui *Gui) handleCommitFocused(g *gocui.Gui, v *gocui.View) error {
Expand Down Expand Up @@ -87,6 +87,6 @@ func (gui *Gui) RenderCommitLength() {
if !gui.Config.GetUserConfig().GetBool("gui.commitLength.show") {
return
}
v := gui.getCommitMessageView(gui.g)
v := gui.getCommitMessageView()
v.Subtitle = gui.getBufferLength(v)
}
10 changes: 8 additions & 2 deletions pkg/gui/commits_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (gui *Gui) refreshCommits(g *gocui.Gui) error {
return err
}

v := gui.getCommitsView(gui.g)
v := gui.getCommitsView()
v.Clear()
fmt.Fprint(v, list)

Expand All @@ -68,13 +68,19 @@ func (gui *Gui) handleCommitsNextLine(g *gocui.Gui, v *gocui.View) error {
panelState := gui.State.Panels.Commits
gui.changeSelectedLine(&panelState.SelectedLine, len(gui.State.Commits), false)

if err := gui.resetOrigin(gui.getMainView()); err != nil {
return err
}
return gui.handleCommitSelect(gui.g, v)
}

func (gui *Gui) handleCommitsPrevLine(g *gocui.Gui, v *gocui.View) error {
panelState := gui.State.Panels.Commits
gui.changeSelectedLine(&panelState.SelectedLine, len(gui.State.Commits), true)

if err := gui.resetOrigin(gui.getMainView()); err != nil {
return err
}
return gui.handleCommitSelect(gui.g, v)
}

Expand All @@ -92,7 +98,7 @@ func (gui *Gui) handleResetToCommit(g *gocui.Gui, commitView *gocui.View) error
if err := gui.refreshCommits(g); err != nil {
panic(err)
}
if err := gui.refreshFiles(g); err != nil {
if err := gui.refreshFiles(); err != nil {
panic(err)
}
gui.resetOrigin(commitView)
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/confirmation_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ func (gui *Gui) prepareConfirmationPanel(currentView *gocui.View, title, prompt
confirmationView.FgColor = gocui.ColorWhite
}
gui.g.Update(func(g *gocui.Gui) error {
confirmationView.Clear()
return gui.switchFocus(gui.g, currentView, confirmationView)
})
return confirmationView, nil
}

func (gui *Gui) onNewPopupPanel() {
gui.g.SetViewOnBottom("commitMessage")
gui.g.SetViewOnBottom("menu")
}

func (gui *Gui) createConfirmationPanel(g *gocui.Gui, currentView *gocui.View, title, prompt string, handleConfirm, handleClose func(*gocui.Gui, *gocui.View) error) error {
Expand Down
Loading

0 comments on commit 9489a94

Please sign in to comment.