diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2019-02-20 20:51:24 +1100 |
---|---|---|
committer | Jesse Duffield Duffield <jesseduffieldduffield@Jesses-MacBook-Pro-3.local> | 2019-02-24 09:42:34 +1100 |
commit | f6b3a9b1842438ece889ce4ef7045365325e53fe (patch) | |
tree | 970b6e2c899ec58e9183064eb9e17bbb25cf515c | |
parent | cdc50e8557442fbe471c7f45cd88d6f2088dae71 (diff) |
rearranging todo items while interactively rebasing
-rw-r--r-- | pkg/commands/git.go | 21 | ||||
-rw-r--r-- | pkg/gui/commits_panel.go | 52 | ||||
-rw-r--r-- | pkg/i18n/english.go | 3 |
3 files changed, 69 insertions, 7 deletions
diff --git a/pkg/commands/git.go b/pkg/commands/git.go index d8530c209..b042e24da 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -598,7 +598,7 @@ func (c *GitCommand) MoveCommitDown(commits []*Commit, index int) error { if len(commits) <= index+2 { // assuming they aren't picking the bottom commit // TODO: support more than say 30 commits and ensure this logic is correct, and i18n - return errors.New("Not enough room") + return errors.New(c.Tr.SLocalize("NoRoom")) } todo := "" @@ -696,6 +696,7 @@ func (c *GitCommand) AmendTo(sha string) error { return c.OSCommand.RunCommand(fmt.Sprintf("git %s rebase --interactive --autostash --autosquash %s^", c.OSCommand.Platform.skipEditorArg, sha)) } +// EditRebaseTodo sets the action at a given index in the git-rebase-todo file func (c *GitCommand) EditRebaseTodo(index int, action string) error { fileName := ".git/rebase-merge/git-rebase-todo" bytes, err := ioutil.ReadFile(fileName) @@ -713,6 +714,24 @@ func (c *GitCommand) EditRebaseTodo(index int, action string) error { return ioutil.WriteFile(fileName, []byte(result), 0644) } +// MoveTodoDown moves a rebase todo item down by one position +func (c *GitCommand) MoveTodoDown(index int) error { + fileName := ".git/rebase-merge/git-rebase-todo" + bytes, err := ioutil.ReadFile(fileName) + if err != nil { + return err + } + + content := strings.Split(string(bytes), "\n") + contentIndex := len(content) - 2 - index + + rearrangedContent := append(content[0:contentIndex-1], content[contentIndex], content[contentIndex-1]) + rearrangedContent = append(rearrangedContent, content[contentIndex+1:]...) + result := strings.Join(rearrangedContent, "\n") + + return ioutil.WriteFile(fileName, []byte(result), 0644) +} + // Revert reverts the selected commit by sha func (c *GitCommand) Revert(sha string) error { return c.OSCommand.RunCommand(fmt.Sprintf("git revert %s", sha)) diff --git a/pkg/gui/commits_panel.go b/pkg/gui/commits_panel.go index 77b0b54dd..8189079c2 100644 --- a/pkg/gui/commits_panel.go +++ b/pkg/gui/commits_panel.go @@ -222,6 +222,21 @@ func (gui *Gui) handleMidRebaseCommand(action string) (bool, error) { return true, gui.refreshCommits(gui.g) } +// handleMoveTodoDown like handleMidRebaseCommand but for moving an item up in the todo list +func (gui *Gui) handleMoveTodoDown(index int) (bool, error) { + selectedCommit := gui.State.Commits[index] + if selectedCommit.Status != "rebasing" { + return false, nil + } + if gui.State.Commits[index+1].Status != "rebasing" { + return true, nil + } + if err := gui.GitCommand.MoveTodoDown(index); err != nil { + return true, gui.createErrorPanel(gui.g, err.Error()) + } + return true, gui.refreshCommits(gui.g) +} + func (gui *Gui) handleCommitDelete(g *gocui.Gui, v *gocui.View) error { applied, err := gui.handleMidRebaseCommand("drop") if err != nil { @@ -239,20 +254,45 @@ func (gui *Gui) handleCommitDelete(g *gocui.Gui, v *gocui.View) error { } func (gui *Gui) handleCommitMoveDown(g *gocui.Gui, v *gocui.View) error { - gui.State.Panels.Commits.SelectedLine++ + index := gui.State.Panels.Commits.SelectedLine + selectedCommit := gui.State.Commits[index] + if selectedCommit.Status == "rebasing" { + if gui.State.Commits[index+1].Status != "rebasing" { + return nil + } + if err := gui.GitCommand.MoveTodoDown(index); err != nil { + return gui.createErrorPanel(gui.g, err.Error()) + } + gui.State.Panels.Commits.SelectedLine++ + return gui.refreshCommits(gui.g) + } - err := gui.GitCommand.MoveCommitDown(gui.State.Commits, gui.State.Panels.Commits.SelectedLine-1) + err := gui.GitCommand.MoveCommitDown(gui.State.Commits, index) + if err == nil { + gui.State.Panels.Commits.SelectedLine++ + } return gui.handleGenericMergeCommandResult(err) } func (gui *Gui) handleCommitMoveUp(g *gocui.Gui, v *gocui.View) error { - if gui.State.Panels.Commits.SelectedLine == 0 { - return gui.createErrorPanel(gui.g, "You cannot move the topmost commit up") // TODO: i18n + index := gui.State.Panels.Commits.SelectedLine + if index == 0 { + return nil } - gui.State.Panels.Commits.SelectedLine-- + selectedCommit := gui.State.Commits[index] + if selectedCommit.Status == "rebasing" { + if err := gui.GitCommand.MoveTodoDown(index - 1); err != nil { + return gui.createErrorPanel(gui.g, err.Error()) + } + gui.State.Panels.Commits.SelectedLine-- + return gui.refreshCommits(gui.g) + } - err := gui.GitCommand.MoveCommitDown(gui.State.Commits, gui.State.Panels.Commits.SelectedLine) + err := gui.GitCommand.MoveCommitDown(gui.State.Commits, index-1) + if err == nil { + gui.State.Panels.Commits.SelectedLine-- + } return gui.handleGenericMergeCommandResult(err) } diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 88058b265..e910d855b 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -562,6 +562,9 @@ func addEnglish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "ErrorOccurred", Other: "An error occurred! Please create an issue at https://github.com/jesseduffield/lazygit/issues", + }, &i18n.Message{ + ID: "NoRoom", + Other: "Not enough room", }, ) } |