Files
lazygit/pkg/utils/rebase_todo.go

471 lines
14 KiB
Go

package utils
import (
"bytes"
"errors"
"fmt"
"os"
"slices"
"github.com/samber/lo"
"github.com/stefanhaller/git-todo-parser/todo"
)
type Todo struct {
Hash string // for todos that have one, e.g. pick, drop, fixup, etc.
Ref string // for update-ref todos
}
type TodoChange struct {
Hash string
NewAction todo.TodoCommand
Flag string
}
// Read a git-rebase-todo file, change the actions for the given commits,
// and write it back
func EditRebaseTodo(filePath string, changes []TodoChange, commentChar byte) error {
todos, err := ReadRebaseTodoFile(filePath, commentChar)
if err != nil {
return err
}
matchCount := 0
for i := range todos {
t := &todos[i]
// This is a nested loop, but it's ok because the number of todos should be small
for _, change := range changes {
if equalHash(t.Commit, change.Hash) {
matchCount++
t.Command = change.NewAction
t.Flag = change.Flag
}
}
}
if matchCount < len(changes) {
// Should never get here
return errors.New("Some todos not found in git-rebase-todo")
}
return WriteRebaseTodoFile(filePath, todos, commentChar)
}
func equalHash(a, b string) bool {
if len(a) == 0 && len(b) == 0 {
return true
}
commonLength := min(len(a), len(b))
return commonLength > 0 && a[:commonLength] == b[:commonLength]
}
func findTodo(todos []todo.Todo, todoToFind Todo) (int, bool) {
_, idx, ok := lo.FindIndexOf(todos, func(t todo.Todo) bool {
// For update-ref todos we also must compare the Ref (they have an empty hash)
return equalHash(t.Commit, todoToFind.Hash) && t.Ref == todoToFind.Ref
})
return idx, ok
}
// LastDoneTodoWasRescheduled tells whether git put the last command of
// rebase-merge/done back into git-rebase-todo, given the parsed contents of
// these two files. In certain cases, git reschedules commands that failed. One
// example is if a patch would overwrite an untracked file; another one is an
// "exec" that failed.
func LastDoneTodoWasRescheduled(doneTodos []todo.Todo, todos []todo.Todo) bool {
// If the last command of the "done" file is the same as the first command
// of "git-rebase-todo", the command was rescheduled.
if len(doneTodos) > 0 && len(todos) > 0 && doneTodos[len(doneTodos)-1] == todos[0] {
return true
}
// Older versions of git have a bug whereby, if a command is rescheduled,
// the last successful command is appended to the "done" file again. To
// detect this, we need to compare the second-to-last done entry against the
// first todo entry, and also compare the last done entry against the
// last-but-two done entry; this latter check is needed for the following
// case:
// pick A
// exec make test
// pick B
// exec make test
// If pick B fails with conflicts, then the "done" file contains
// pick A
// exec make test
// pick B
// and git-rebase-todo contains
// exec make test
// Without the last condition we would erroneously treat this as the exec
// command being rescheduled.
return len(doneTodos) >= 3 && len(todos) > 0 && doneTodos[len(doneTodos)-2] == todos[0] &&
doneTodos[len(doneTodos)-1] == doneTodos[len(doneTodos)-3]
}
func ReadRebaseTodoFile(fileName string, commentChar byte) ([]todo.Todo, error) {
f, err := os.Open(fileName)
if err != nil {
return nil, err
}
todos, err := todo.Parse(f, commentChar)
err2 := f.Close()
if err == nil {
err = err2
}
return todos, err
}
func WriteRebaseTodoFile(fileName string, todos []todo.Todo, commentChar byte) error {
f, err := os.Create(fileName)
if err != nil {
return err
}
err = todo.Write(f, todos, commentChar)
err2 := f.Close()
if err == nil {
err = err2
}
return err
}
func todosToString(todos []todo.Todo, commentChar byte) ([]byte, error) {
buffer := bytes.Buffer{}
err := todo.Write(&buffer, todos, commentChar)
return buffer.Bytes(), err
}
func PrependStrToTodoFile(filePath string, linesToPrepend []byte) error {
existingContent, err := os.ReadFile(filePath)
if err != nil {
return err
}
linesToPrepend = append(linesToPrepend, existingContent...)
return os.WriteFile(filePath, linesToPrepend, 0o644)
}
// Unlike the other functions in this file, which write the changed todos file
// back to disk, this one returns the new content as a byte slice. This is
// because when deleting update-ref todos, we must perform a "git rebase
// --edit-todo" command to pass the changed todos to git so that it can do some
// housekeeping around the deleted todos. This can only be done by our caller.
func DeleteTodos(fileName string, todosToDelete []Todo, commentChar byte) ([]byte, error) {
todos, err := ReadRebaseTodoFile(fileName, commentChar)
if err != nil {
return nil, err
}
rearrangedTodos, err := deleteTodos(todos, todosToDelete)
if err != nil {
return nil, err
}
return todosToString(rearrangedTodos, commentChar)
}
func deleteTodos(todos []todo.Todo, todosToDelete []Todo) ([]todo.Todo, error) {
for _, todoToDelete := range todosToDelete {
idx, ok := findTodo(todos, todoToDelete)
if !ok {
// Should never happen
return []todo.Todo{}, fmt.Errorf("Todo %s not found in git-rebase-todo", todoToDelete.Hash)
}
todos = Remove(todos, idx)
}
return todos, nil
}
func MoveTodos(fileName string, todosToMove []Todo, offset int, commentChar byte) error {
todos, err := ReadRebaseTodoFile(fileName, commentChar)
if err != nil {
return err
}
rearrangedTodos, err := moveTodos(todos, todosToMove, offset)
if err != nil {
return err
}
return WriteRebaseTodoFile(fileName, rearrangedTodos, commentChar)
}
func moveTodos(todos []todo.Todo, todosToMove []Todo, offset int) ([]todo.Todo, error) {
moveOneRow := moveTodosUp
if offset > 0 {
moveOneRow = moveTodosDown
}
for range max(offset, -offset) {
var err error
todos, err = moveOneRow(todos, slices.Clone(todosToMove))
if err != nil {
return nil, err
}
}
return todos, nil
}
func moveTodoDown(todos []todo.Todo, todoToMove Todo) ([]todo.Todo, error) {
rearrangedTodos, err := moveTodoUp(lo.Reverse(todos), todoToMove)
return lo.Reverse(rearrangedTodos), err
}
func moveTodosDown(todos []todo.Todo, todosToMove []Todo) ([]todo.Todo, error) {
rearrangedTodos, err := moveTodosUp(lo.Reverse(todos), lo.Reverse(todosToMove))
return lo.Reverse(rearrangedTodos), err
}
func moveTodoUp(todos []todo.Todo, todoToMove Todo) ([]todo.Todo, error) {
sourceIdx, ok := findTodo(todos, todoToMove)
if !ok {
// Should never happen
return []todo.Todo{}, fmt.Errorf("Todo %s not found in git-rebase-todo", todoToMove.Hash)
}
// The todos are ordered backwards compared to our model commits, so
// actually move the commit _down_ in the todos slice (i.e. towards
// the end of the slice)
// Find the next todo that we show in lazygit's commits view (skipping the rest)
_, skip, ok := lo.FindIndexOf(todos[sourceIdx+1:], isRenderedTodo)
if !ok {
// We expect callers to guard against this
return []todo.Todo{}, errors.New("Destination position for moving todo is out of range")
}
destinationIdx := sourceIdx + 1 + skip
rearrangedTodos := MoveElement(todos, sourceIdx, destinationIdx)
return rearrangedTodos, nil
}
func moveTodosUp(todos []todo.Todo, todosToMove []Todo) ([]todo.Todo, error) {
for _, todoToMove := range todosToMove {
var newTodos []todo.Todo
newTodos, err := moveTodoUp(todos, todoToMove)
if err != nil {
return nil, err
}
todos = newTodos
}
return todos, nil
}
// Where MoveCommitTodos puts the todos that it moves.
type TodoMoveDestination struct {
// The commit whose todo the moved todos are to follow. If empty, they go
// to the beginning, after any labels and resets.
AfterHash string
// Once the moved todos are taken out, there may be update-ref todos
// directly after the todo of AfterHash (or at the beginning, if AfterHash
// is empty). Those for the refs listed here go after the moved todos, so
// that git points these refs at the last of them. The other ones stay
// before the moved todos.
RefsAfter []string
}
// MoveCommitTodos moves the todos of the given commits to the given
// destination. The update-ref todos between them move along, so that each
// commit stays on the branch that it was on. The commits must be contiguous
// in the todo file apart from these update-ref todos.
func MoveCommitTodos(fileName string, hashes []string, destination TodoMoveDestination, commentChar byte) error {
todos, err := ReadRebaseTodoFile(fileName, commentChar)
if err != nil {
return err
}
rearrangedTodos, err := moveCommitTodos(todos, hashes, destination)
if err != nil {
return err
}
return WriteRebaseTodoFile(fileName, rearrangedTodos, commentChar)
}
func moveCommitTodos(todos []todo.Todo, hashes []string, destination TodoMoveDestination) ([]todo.Todo, error) {
indices := make([]int, 0, len(hashes))
for _, hash := range hashes {
idx, ok := findTodo(todos, Todo{Hash: hash})
if !ok {
// Should never happen
return nil, fmt.Errorf("Todo %s not found in git-rebase-todo", hash)
}
indices = append(indices, idx)
}
start, end := slices.Min(indices), slices.Max(indices)+1
isUpdateRefOrComment := func(t todo.Todo) bool {
return t.Command == todo.UpdateRef || t.Command == todo.Comment
}
commitCount := lo.CountBy(todos[start:end], func(t todo.Todo) bool { return t.Commit != "" })
if commitCount != len(hashes) || lo.SomeBy(todos[start:end], func(t todo.Todo) bool {
return t.Commit == "" && !isUpdateRefOrComment(t)
}) {
return nil, errors.New("The commits to move are not contiguous in git-rebase-todo")
}
movedTodos := todos[start:end]
otherTodos := slices.Concat(todos[:start], todos[end:])
var runStart int
if destination.AfterHash == "" {
var found bool
_, runStart, found = lo.FindIndexOf(otherTodos, func(t todo.Todo) bool {
return t.Commit != "" || t.Command == todo.UpdateRef
})
if !found {
runStart = len(otherTodos)
}
} else {
afterIdx, ok := findTodo(otherTodos, Todo{Hash: destination.AfterHash})
if !ok {
// Should never happen
return nil, fmt.Errorf("Todo %s not found in git-rebase-todo", destination.AfterHash)
}
runStart = afterIdx + 1
}
runEnd := runStart
for runEnd < len(otherTodos) && isUpdateRefOrComment(otherTodos[runEnd]) {
runEnd++
}
todosBefore, todosAfter := lo.FilterReject(otherTodos[runStart:runEnd], func(t todo.Todo, _ int) bool {
return t.Command != todo.UpdateRef || !slices.Contains(destination.RefsAfter, t.Ref)
})
return slices.Concat(
otherTodos[:runStart], todosBefore, movedTodos, todosAfter, otherTodos[runEnd:],
), nil
}
func MoveFixupCommitDown(fileName string, originalHash string, fixupHash string, changeToFixup bool, commentChar byte) error {
todos, err := ReadRebaseTodoFile(fileName, commentChar)
if err != nil {
return err
}
newTodos, err := moveFixupCommitDown(todos, originalHash, fixupHash, changeToFixup)
if err != nil {
return err
}
return WriteRebaseTodoFile(fileName, newTodos, commentChar)
}
func moveFixupCommitDown(todos []todo.Todo, originalHash string, fixupHash string, changeToFixup bool) ([]todo.Todo, error) {
isOriginal := func(t todo.Todo) bool {
return (t.Command == todo.Pick || t.Command == todo.Merge) && equalHash(t.Commit, originalHash)
}
isFixup := func(t todo.Todo) bool {
return t.Command == todo.Pick && equalHash(t.Commit, fixupHash)
}
originalHashCount := lo.CountBy(todos, isOriginal)
if originalHashCount != 1 {
return nil, fmt.Errorf("Expected exactly one original hash, found %d", originalHashCount)
}
fixupHashCount := lo.CountBy(todos, isFixup)
if fixupHashCount != 1 {
return nil, fmt.Errorf("Expected exactly one fixup hash, found %d", fixupHashCount)
}
_, fixupIndex, _ := lo.FindIndexOf(todos, isFixup)
_, originalIndex, _ := lo.FindIndexOf(todos, isOriginal)
newTodos := MoveElement(todos, fixupIndex, originalIndex+1)
if changeToFixup {
newTodos[originalIndex+1].Command = todo.Fixup
}
return newTodos, nil
}
func RemoveUpdateRefsForCopiedBranch(fileName string, commentChar byte) error {
todos, err := ReadRebaseTodoFile(fileName, commentChar)
if err != nil {
return err
}
// Filter out comments
todos = lo.Filter(todos, func(t todo.Todo, _ int) bool {
return t.Command != todo.Comment
})
// Delete any update-ref todos at the end of the todo list. These are not
// part of a stack of branches, and so shouldn't be updated. This makes it
// possible to create a copy of a branch and rebase the copy without
// affecting the original branch.
if _, i, found := lo.FindLastIndexOf(todos, func(t todo.Todo) bool {
return t.Command != todo.UpdateRef
}); found && i < len(todos)-1 {
todos = slices.Delete(todos, i+1, len(todos))
return WriteRebaseTodoFile(fileName, todos, commentChar)
}
return nil
}
// We render a todo in the commits view if it's a commit or if it's an
// update-ref or exec. We don't render label, reset, or comment lines.
func isRenderedTodo(t todo.Todo) bool {
return t.Commit != "" || t.Command == todo.UpdateRef || t.Command == todo.Exec
}
func DropMergeCommit(fileName string, hash string, commentChar byte) error {
todos, err := ReadRebaseTodoFile(fileName, commentChar)
if err != nil {
return err
}
newTodos, err := dropMergeCommit(todos, hash)
if err != nil {
return err
}
return WriteRebaseTodoFile(fileName, newTodos, commentChar)
}
func dropMergeCommit(todos []todo.Todo, hash string) ([]todo.Todo, error) {
isMerge := func(t todo.Todo) bool {
return t.Command == todo.Merge && t.Flag == "-C" && equalHash(t.Commit, hash)
}
if lo.CountBy(todos, isMerge) != 1 {
return nil, fmt.Errorf("Expected exactly one merge commit with hash %s", hash)
}
_, idx, _ := lo.FindIndexOf(todos, isMerge)
return slices.Delete(todos, idx, idx+1), nil
}
// Read a git-rebase-todo file, make the todo for the given commit use a
// different commit instead, and write it back
func ChangeTodoCommit(fileName string, hash string, newHash string, commentChar byte) error {
todos, err := ReadRebaseTodoFile(fileName, commentChar)
if err != nil {
return err
}
newTodos, err := changeTodoCommit(todos, hash, newHash)
if err != nil {
return err
}
return WriteRebaseTodoFile(fileName, newTodos, commentChar)
}
func changeTodoCommit(todos []todo.Todo, hash string, newHash string) ([]todo.Todo, error) {
idx, ok := findTodo(todos, Todo{Hash: hash})
if !ok {
// Should never happen
return nil, fmt.Errorf("Todo %s not found in git-rebase-todo", hash)
}
todos[idx].Commit = newHash
return todos, nil
}