mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-28 16:02:01 +03:00
Extract EditRebaseTodo into a function in utils.rebaseTodo
We want to reuse it from the daemon code in the next commit.
This commit is contained in:
@ -231,25 +231,8 @@ func (self *RebaseCommands) AmendTo(commit *models.Commit) error {
|
|||||||
|
|
||||||
// EditRebaseTodo sets the action for a given rebase commit in the git-rebase-todo file
|
// EditRebaseTodo sets the action for a given rebase commit in the git-rebase-todo file
|
||||||
func (self *RebaseCommands) EditRebaseTodo(commit *models.Commit, action todo.TodoCommand) error {
|
func (self *RebaseCommands) EditRebaseTodo(commit *models.Commit, action todo.TodoCommand) error {
|
||||||
fileName := filepath.Join(self.dotGitDir, "rebase-merge/git-rebase-todo")
|
return utils.EditRebaseTodo(
|
||||||
todos, err := utils.ReadRebaseTodoFile(fileName)
|
filepath.Join(self.dotGitDir, "rebase-merge/git-rebase-todo"), commit.Sha, commit.Action, action)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := range todos {
|
|
||||||
t := &todos[i]
|
|
||||||
// Comparing just the sha is not enough; we need to compare both the
|
|
||||||
// action and the sha, as the sha could appear multiple times (e.g. in a
|
|
||||||
// pick and later in a merge)
|
|
||||||
if t.Command == commit.Action && t.Commit == commit.Sha {
|
|
||||||
t.Command = action
|
|
||||||
return utils.WriteRebaseTodoFile(fileName, todos)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Should never get here
|
|
||||||
return fmt.Errorf("Todo %s not found in git-rebase-todo", commit.Sha)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MoveTodoDown moves a rebase todo item down by one position
|
// MoveTodoDown moves a rebase todo item down by one position
|
||||||
|
@ -9,6 +9,29 @@ import (
|
|||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Read a git-rebase-todo file, change the action for the given sha to
|
||||||
|
// newAction, and write it back
|
||||||
|
func EditRebaseTodo(filePath string, sha string, oldAction todo.TodoCommand, newAction todo.TodoCommand) error {
|
||||||
|
todos, err := ReadRebaseTodoFile(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range todos {
|
||||||
|
t := &todos[i]
|
||||||
|
// Comparing just the sha is not enough; we need to compare both the
|
||||||
|
// action and the sha, as the sha could appear multiple times (e.g. in a
|
||||||
|
// pick and later in a merge)
|
||||||
|
if t.Command == oldAction && equalShas(t.Commit, sha) {
|
||||||
|
t.Command = newAction
|
||||||
|
return WriteRebaseTodoFile(filePath, todos)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should never get here
|
||||||
|
return fmt.Errorf("Todo %s not found in git-rebase-todo", sha)
|
||||||
|
}
|
||||||
|
|
||||||
func equalShas(a, b string) bool {
|
func equalShas(a, b string) bool {
|
||||||
return strings.HasPrefix(a, b) || strings.HasPrefix(b, a)
|
return strings.HasPrefix(a, b) || strings.HasPrefix(b, a)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user