1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-30 03:23:08 +03:00

Make RebaseCommands.AmendTo more robust

This fixes two problems with the "amend commit with staged changes" command:

1. Amending to a fixup commit didn't work (this would create a commmit with the
   title "fixup! fixup! original title" and keep that at the top of the branch)
2. Unrelated fixup commits would be squashed too.

The added integration test verifies that both of these problems are fixed.
This commit is contained in:
Stefan Haller
2023-04-19 08:19:17 +02:00
parent 185bbf0c75
commit 3fe4db9316
6 changed files with 139 additions and 9 deletions

View File

@ -134,6 +134,42 @@ func moveTodoUp(todos []todo.Todo, sha string, action todo.TodoCommand) ([]todo.
return rearrangedTodos, nil
}
func MoveFixupCommitDown(fileName string, originalSha string, fixupSha string) error {
todos, err := ReadRebaseTodoFile(fileName)
if err != nil {
return err
}
newTodos := []todo.Todo{}
numOriginalShaLinesFound := 0
numFixupShaLinesFound := 0
for _, t := range todos {
if t.Command == todo.Pick {
if equalShas(t.Commit, originalSha) {
numOriginalShaLinesFound += 1
// append the original commit, and then the fixup
newTodos = append(newTodos, t)
newTodos = append(newTodos, todo.Todo{Command: todo.Fixup, Commit: fixupSha})
continue
} else if equalShas(t.Commit, fixupSha) {
numFixupShaLinesFound += 1
// skip the fixup here
continue
}
}
newTodos = append(newTodos, t)
}
if numOriginalShaLinesFound != 1 || numFixupShaLinesFound != 1 {
return fmt.Errorf("Expected exactly one each of originalSha and fixupSha, got %d, %d",
numOriginalShaLinesFound, numFixupShaLinesFound)
}
return WriteRebaseTodoFile(fileName, newTodos)
}
// We render a todo in the commits view if it's a commit or if it's an
// update-ref. We don't render label, reset, or comment lines.
func isRenderedTodo(t todo.Todo) bool {