1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-15 11:56:37 +02:00
lazygit/pkg/utils/rebase_todo.go
Stefan Haller 8b99a3c949 Drop update-ref commands at the top of the rebase-todo file
The rebase.updateRefs feature of git is very useful to rebase a stack of
branches and keep everything nicely stacked; however, it is usually in the way
when you make a copy of a branch and want to rebase it "away" from the original
branch in some way or other. For example, the original branch might sit on main,
and you want to rebase the copy onto devel to see if things still compile there.
Or you want to do some heavy history rewriting experiments on the copy, but keep
the original branch in case the experiments fail. Or you want to split a branch
in two because it contains two unrelated sets of changes; so you make a copy,
and drop half of the commits from the copy, then check out the original branch
and drop the other half of the commits from it.

In all these cases, git's updateRefs feature insists on moving the original
branch along with the copy in the first rebase that you make on the copy. I
think this is a bug in git, it should create update-ref todos only for branches
that point into the middle of your branch (because only then do they form a
stack), not when they point at the head (because then it's a copy). I had a long
discussion about this on the git mailing list [1], but people either don't agree
or don't care enough.

So we fix this on our side: whenever we start a rebase for whatever reason, be
it interactive, non-interactive, or behind-the-scenes, we drop any update-ref
todos that are at the very top of the todo list, which fixes all the
above-mentioned scenarios nicely.

I will admit that there's one scenario where git's behavior is the desired one,
and the fix in this PR makes it worse: when you create a new branch off of an
existing one, with the intention of creating a stack of branches, but before you
make the first commit on the new branch you realize some problem with the first
branch (e.g. a commit that needs to be reworded or dropped). It this case you do
want both branches to be affected by the change. In my experience this scenario
is much rarer than the other ones that I described above, and it's also much
easier to recover from: just check out the other branch again and hard-reset it
to the rebased one.

[1]
https://public-inbox.org/git/354f9fed-567f-42c8-9da9-148a5e223022@haller-berlin.de/
2024-04-22 20:59:15 +02:00

296 lines
8.4 KiB
Go

package utils
import (
"bytes"
"fmt"
"os"
"slices"
"strings"
"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
Action todo.TodoCommand
}
// In order to change a TODO in git-rebase-todo, we need to specify the old action,
// because sometimes the same hash appears multiple times in the file (e.g. in a pick
// and later in a merge)
type TodoChange struct {
Hash string
OldAction todo.TodoCommand
NewAction todo.TodoCommand
}
// 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 t.Command == change.OldAction && equalHash(t.Commit, change.Hash) {
matchCount++
t.Command = change.NewAction
}
}
}
if matchCount < len(changes) {
// Should never get here
return fmt.Errorf("Some todos not found in git-rebase-todo")
}
return WriteRebaseTodoFile(filePath, todos, commentChar)
}
func equalHash(a, b string) bool {
return strings.HasPrefix(a, b) || strings.HasPrefix(b, a)
}
func findTodo(todos []todo.Todo, todoToFind Todo) (int, bool) {
_, idx, ok := lo.FindIndexOf(todos, func(t todo.Todo) bool {
// Comparing just the hash is not enough; we need to compare both the
// action and the hash, as the hash could appear multiple times (e.g. in a
// pick and later in a merge). For update-ref todos we also must compare
// the Ref.
return t.Command == todoToFind.Action &&
equalHash(t.Commit, todoToFind.Hash) &&
t.Ref == todoToFind.Ref
})
return idx, ok
}
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 MoveTodosDown(fileName string, todosToMove []Todo, commentChar byte) error {
todos, err := ReadRebaseTodoFile(fileName, commentChar)
if err != nil {
return err
}
rearrangedTodos, err := moveTodosDown(todos, todosToMove)
if err != nil {
return err
}
return WriteRebaseTodoFile(fileName, rearrangedTodos, commentChar)
}
func MoveTodosUp(fileName string, todosToMove []Todo, commentChar byte) error {
todos, err := ReadRebaseTodoFile(fileName, commentChar)
if err != nil {
return err
}
rearrangedTodos, err := moveTodosUp(todos, todosToMove)
if err != nil {
return err
}
return WriteRebaseTodoFile(fileName, rearrangedTodos, commentChar)
}
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{}, fmt.Errorf("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
}
func MoveFixupCommitDown(fileName string, originalHash string, fixupHash string, commentChar byte) error {
todos, err := ReadRebaseTodoFile(fileName, commentChar)
if err != nil {
return err
}
newTodos, err := moveFixupCommitDown(todos, originalHash, fixupHash)
if err != nil {
return err
}
return WriteRebaseTodoFile(fileName, newTodos, commentChar)
}
func moveFixupCommitDown(todos []todo.Todo, originalHash string, fixupHash string) ([]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)
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. We don't render label, reset, or comment lines.
func isRenderedTodo(t todo.Todo) bool {
return t.Commit != "" || t.Command == todo.UpdateRef
}