mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-02-03 13:21:56 +02:00
rearranging todo items while interactively rebasing
This commit is contained in:
parent
cdc50e8557
commit
f6b3a9b184
@ -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))
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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",
|
||||
},
|
||||
)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user