mirror of
https://github.com/jesseduffield/lazygit.git
synced 2026-11-18 03:49:50 -06:00
Add a rebase that moves commits on top of a given commit
Outside of a rebase, we move commits by a number of rows, skipping the update-ref todos that we don't show. This can't move a commit past the update-ref todos only. We will need that for moving a commit to the end of the branch below it in a stack. It also moves each commit separately, so the update-ref todos between the moved commits stay behind. MoveCommitsTo names the commit that the moved commits are to sit on, and which of the branches that point at that commit are to point at the moved commits instead. The update-ref todos between the moved commits move along with them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f8877eef26
commit
8bfa74a709
4 files changed
+331
No files matched your search
@@ -38,6 +38,7 @@ const (
|
||||
DaemonKindDropMergeCommit
|
||||
DaemonKindMoveFixupCommitDown
|
||||
DaemonKindWriteRebaseTodo
|
||||
DaemonKindMoveCommitTodos
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -60,6 +61,7 @@ func getInstruction() Instruction {
|
||||
DaemonKindMoveTodosDown: deserializeInstruction[*MoveTodosDownInstruction],
|
||||
DaemonKindInsertBreak: deserializeInstruction[*InsertBreakInstruction],
|
||||
DaemonKindWriteRebaseTodo: deserializeInstruction[*WriteRebaseTodoInstruction],
|
||||
DaemonKindMoveCommitTodos: deserializeInstruction[*MoveCommitTodosInstruction],
|
||||
}
|
||||
|
||||
return mapping[getDaemonKind()](jsonData)
|
||||
@@ -325,6 +327,32 @@ func (self *MoveTodosDownInstruction) run(log *logrus.Entry) error {
|
||||
})
|
||||
}
|
||||
|
||||
type MoveCommitTodosInstruction struct {
|
||||
Hashes []string
|
||||
Destination utils.TodoMoveDestination
|
||||
}
|
||||
|
||||
func NewMoveCommitTodosInstruction(hashes []string, destination utils.TodoMoveDestination) Instruction {
|
||||
return &MoveCommitTodosInstruction{
|
||||
Hashes: hashes,
|
||||
Destination: destination,
|
||||
}
|
||||
}
|
||||
|
||||
func (self *MoveCommitTodosInstruction) Kind() DaemonKind {
|
||||
return DaemonKindMoveCommitTodos
|
||||
}
|
||||
|
||||
func (self *MoveCommitTodosInstruction) SerializedInstructions() string {
|
||||
return serializeInstruction(self)
|
||||
}
|
||||
|
||||
func (self *MoveCommitTodosInstruction) run(log *logrus.Entry) error {
|
||||
return handleInteractiveRebase(log, func(path string) error {
|
||||
return utils.MoveCommitTodos(path, self.Hashes, self.Destination, getCommentChar())
|
||||
})
|
||||
}
|
||||
|
||||
type InsertBreakInstruction struct{}
|
||||
|
||||
func NewInsertBreakInstruction() Instruction {
|
||||
|
||||
@@ -140,6 +140,47 @@ func (self *RebaseCommands) MoveCommits(commits []*models.Commit, startIdx int,
|
||||
}).Run()
|
||||
}
|
||||
|
||||
// MoveCommitsTo moves the commits from startIdx to endIdx so that they sit
|
||||
// directly on top of the commit at destinationIdx, which must be outside of
|
||||
// that range; if destinationIdx is len(commits), they go below the last
|
||||
// commit. Branches that point at commits inside the range stay with them.
|
||||
//
|
||||
// Once the moved commits are taken out, some branches may point at the commit
|
||||
// at destinationIdx: its own ones and, if it is the commit directly below the
|
||||
// moved commits, the ones that pointed at the newest of them. Those listed in
|
||||
// branchesAbove point at the newest moved commit afterwards, the others at the
|
||||
// commit at destinationIdx.
|
||||
func (self *RebaseCommands) MoveCommitsTo(
|
||||
commits []*models.Commit, startIdx int, endIdx int, destinationIdx int, branchesAbove []string,
|
||||
) error {
|
||||
// The commit at destinationIdx needs to be part of the rebase only if some
|
||||
// of the branches pointing at it are to move; otherwise, the moved commits
|
||||
// can go first in the rebase todo.
|
||||
baseIdx := destinationIdx
|
||||
if len(branchesAbove) > 0 {
|
||||
baseIdx++
|
||||
}
|
||||
baseIdx = max(baseIdx, endIdx+1)
|
||||
destination := utils.TodoMoveDestination{
|
||||
RefsAfter: lo.Map(branchesAbove, func(branch string, _ int) string {
|
||||
return "refs/heads/" + branch
|
||||
}),
|
||||
}
|
||||
if baseIdx != destinationIdx {
|
||||
destination.AfterHash = commits[destinationIdx].Hash()
|
||||
}
|
||||
|
||||
hashes := lo.Map(commits[startIdx:endIdx+1], func(commit *models.Commit, _ int) string {
|
||||
return commit.Hash()
|
||||
})
|
||||
|
||||
return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{
|
||||
baseHashOrRoot: getBaseHashOrRoot(commits, baseIdx),
|
||||
instruction: daemon.NewMoveCommitTodosInstruction(hashes, destination),
|
||||
overrideEditor: true,
|
||||
}).Run()
|
||||
}
|
||||
|
||||
func (self *RebaseCommands) InteractiveRebase(commits []*models.Commit, startIdx int, endIdx int, action todo.TodoCommand, flag string) error {
|
||||
baseIndex := endIdx + 1
|
||||
if action == todo.Squash || action == todo.Fixup {
|
||||
|
||||
@@ -264,6 +264,90 @@ func moveTodosUp(todos []todo.Todo, todosToMove []Todo, isInRebase bool) ([]todo
|
||||
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 {
|
||||
|
||||
@@ -52,6 +52,184 @@ func TestMoveTodos(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestMoveCommitTodos(t *testing.T) {
|
||||
pick := func(hash string) todo.Todo { return todo.Todo{Command: todo.Pick, Commit: hash} }
|
||||
updateRef := func(ref string) todo.Todo { return todo.Todo{Command: todo.UpdateRef, Ref: ref} }
|
||||
|
||||
scenarios := []struct {
|
||||
testName string
|
||||
todos []todo.Todo
|
||||
hashes []string
|
||||
destination TodoMoveDestination
|
||||
expectedErr string
|
||||
expectedTodos []todo.Todo
|
||||
}{
|
||||
{
|
||||
testName: "move to the beginning",
|
||||
todos: []todo.Todo{pick("a"), pick("b"), pick("c")},
|
||||
hashes: []string{"c"},
|
||||
destination: TodoMoveDestination{},
|
||||
expectedTodos: []todo.Todo{pick("c"), pick("a"), pick("b")},
|
||||
},
|
||||
{
|
||||
testName: "move to the beginning, after labels and resets",
|
||||
todos: []todo.Todo{
|
||||
{Command: todo.Label, Label: "onto"},
|
||||
{Command: todo.Reset, Label: "onto"},
|
||||
pick("a"),
|
||||
pick("b"),
|
||||
},
|
||||
hashes: []string{"b"},
|
||||
destination: TodoMoveDestination{},
|
||||
expectedTodos: []todo.Todo{
|
||||
{Command: todo.Label, Label: "onto"},
|
||||
{Command: todo.Reset, Label: "onto"},
|
||||
pick("b"),
|
||||
pick("a"),
|
||||
},
|
||||
},
|
||||
{
|
||||
testName: "move towards the beginning",
|
||||
todos: []todo.Todo{pick("a"), pick("b"), pick("c"), pick("d")},
|
||||
hashes: []string{"d"},
|
||||
destination: TodoMoveDestination{AfterHash: "a"},
|
||||
expectedTodos: []todo.Todo{pick("a"), pick("d"), pick("b"), pick("c")},
|
||||
},
|
||||
{
|
||||
testName: "move towards the end",
|
||||
todos: []todo.Todo{pick("a"), pick("b"), pick("c"), pick("d")},
|
||||
hashes: []string{"a"},
|
||||
destination: TodoMoveDestination{AfterHash: "c"},
|
||||
expectedTodos: []todo.Todo{pick("b"), pick("c"), pick("a"), pick("d")},
|
||||
},
|
||||
{
|
||||
testName: "move several commits",
|
||||
todos: []todo.Todo{pick("a"), pick("b"), pick("c"), pick("d")},
|
||||
hashes: []string{"c", "b"},
|
||||
destination: TodoMoveDestination{AfterHash: "d"},
|
||||
expectedTodos: []todo.Todo{pick("a"), pick("d"), pick("b"), pick("c")},
|
||||
},
|
||||
{
|
||||
testName: "update-refs between the moved commits move along",
|
||||
todos: []todo.Todo{pick("a"), pick("b"), updateRef("refs/heads/x"), pick("c"), pick("d")},
|
||||
hashes: []string{"c", "b"},
|
||||
destination: TodoMoveDestination{AfterHash: "d"},
|
||||
expectedTodos: []todo.Todo{pick("a"), pick("d"), pick("b"), updateRef("refs/heads/x"), pick("c")},
|
||||
},
|
||||
{
|
||||
testName: "update-refs after the moved commits stay",
|
||||
todos: []todo.Todo{pick("a"), pick("b"), updateRef("refs/heads/x"), pick("c")},
|
||||
hashes: []string{"b"},
|
||||
destination: TodoMoveDestination{},
|
||||
expectedTodos: []todo.Todo{pick("b"), pick("a"), updateRef("refs/heads/x"), pick("c")},
|
||||
},
|
||||
{
|
||||
testName: "update-refs at the beginning stay before the moved commits",
|
||||
todos: []todo.Todo{pick("a"), updateRef("refs/heads/x"), pick("b")},
|
||||
hashes: []string{"a"},
|
||||
destination: TodoMoveDestination{},
|
||||
expectedTodos: []todo.Todo{updateRef("refs/heads/x"), pick("a"), pick("b")},
|
||||
},
|
||||
{
|
||||
testName: "update-refs at the beginning that are listed go after the moved commits",
|
||||
todos: []todo.Todo{pick("a"), updateRef("refs/heads/x"), pick("b")},
|
||||
hashes: []string{"a"},
|
||||
destination: TodoMoveDestination{RefsAfter: []string{"refs/heads/x"}},
|
||||
expectedTodos: []todo.Todo{pick("a"), updateRef("refs/heads/x"), pick("b")},
|
||||
},
|
||||
{
|
||||
testName: "update-refs of the destination stay before the moved commits",
|
||||
todos: []todo.Todo{pick("a"), updateRef("refs/heads/x"), pick("b"), pick("c")},
|
||||
hashes: []string{"c"},
|
||||
destination: TodoMoveDestination{AfterHash: "a"},
|
||||
expectedTodos: []todo.Todo{pick("a"), updateRef("refs/heads/x"), pick("c"), pick("b")},
|
||||
},
|
||||
{
|
||||
testName: "update-refs of the destination that are listed go after the moved commits",
|
||||
todos: []todo.Todo{
|
||||
pick("a"),
|
||||
updateRef("refs/heads/x"),
|
||||
updateRef("refs/heads/y"),
|
||||
updateRef("refs/heads/z"),
|
||||
pick("b"),
|
||||
},
|
||||
hashes: []string{"b"},
|
||||
destination: TodoMoveDestination{AfterHash: "a", RefsAfter: []string{"refs/heads/x", "refs/heads/z"}},
|
||||
expectedTodos: []todo.Todo{
|
||||
pick("a"),
|
||||
updateRef("refs/heads/y"),
|
||||
pick("b"),
|
||||
updateRef("refs/heads/x"),
|
||||
updateRef("refs/heads/z"),
|
||||
},
|
||||
},
|
||||
{
|
||||
testName: "update-refs after the moved commits join those of the destination",
|
||||
todos: []todo.Todo{
|
||||
pick("a"),
|
||||
updateRef("refs/heads/x"),
|
||||
pick("b"),
|
||||
updateRef("refs/heads/y"),
|
||||
pick("c"),
|
||||
},
|
||||
hashes: []string{"b"},
|
||||
destination: TodoMoveDestination{AfterHash: "a", RefsAfter: []string{"refs/heads/x", "refs/heads/y"}},
|
||||
expectedTodos: []todo.Todo{
|
||||
pick("a"),
|
||||
pick("b"),
|
||||
updateRef("refs/heads/x"),
|
||||
updateRef("refs/heads/y"),
|
||||
pick("c"),
|
||||
},
|
||||
},
|
||||
{
|
||||
testName: "commit not found",
|
||||
todos: []todo.Todo{pick("a"), pick("b")},
|
||||
hashes: []string{"c"},
|
||||
destination: TodoMoveDestination{},
|
||||
expectedErr: "Todo c not found in git-rebase-todo",
|
||||
},
|
||||
{
|
||||
testName: "destination not found",
|
||||
todos: []todo.Todo{pick("a"), pick("b")},
|
||||
hashes: []string{"b"},
|
||||
destination: TodoMoveDestination{AfterHash: "c"},
|
||||
expectedErr: "Todo c not found in git-rebase-todo",
|
||||
},
|
||||
{
|
||||
testName: "other commit between the moved commits",
|
||||
todos: []todo.Todo{pick("a"), pick("b"), pick("c"), pick("d")},
|
||||
hashes: []string{"d", "b"},
|
||||
destination: TodoMoveDestination{},
|
||||
expectedErr: "The commits to move are not contiguous in git-rebase-todo",
|
||||
},
|
||||
{
|
||||
testName: "label between the moved commits",
|
||||
todos: []todo.Todo{
|
||||
pick("a"),
|
||||
pick("b"),
|
||||
{Command: todo.Label, Label: "x"},
|
||||
pick("c"),
|
||||
},
|
||||
hashes: []string{"c", "b"},
|
||||
destination: TodoMoveDestination{},
|
||||
expectedErr: "The commits to move are not contiguous in git-rebase-todo",
|
||||
},
|
||||
}
|
||||
|
||||
for _, s := range scenarios {
|
||||
t.Run(s.testName, func(t *testing.T) {
|
||||
actual, err := moveCommitTodos(s.todos, s.hashes, s.destination)
|
||||
if s.expectedErr == "" {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, s.expectedTodos, actual)
|
||||
} else {
|
||||
assert.EqualError(t, err, s.expectedErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRebaseCommands_moveTodoDown(t *testing.T) {
|
||||
type scenario struct {
|
||||
testName string
|
||||
|
||||
Reference in new issue
Block a user