diff --git a/docs-master/Stacked_Branches.md b/docs-master/Stacked_Branches.md index 6bbd7ae56..8f0624749 100644 --- a/docs-master/Stacked_Branches.md +++ b/docs-master/Stacked_Branches.md @@ -19,6 +19,13 @@ fonts](Config.md#display-nerd-fonts-icons)). In the commits panel, `>` selects the first commit of the branch that the selection is in. Press it again to get to the first commit of the next branch down the stack. +To move commits from one branch of the stack to another, move them with `` +and ``. When they get to the boundary between two branches, the next press +moves them across the boundary only. The order of the commits stays the same; +only the branch marker moves. So to add a commit at the end of a branch further +down in the stack, you can create it at the top of the stack and move it down +until the marker of that branch is on it. + When you push the topmost branch of the stack with `P`, and the branches below it have commits that haven't been pushed yet, lazygit offers to push them along with it. After rebasing the stack this saves you from checking out and diff --git a/pkg/gui/controllers/local_commits_controller.go b/pkg/gui/controllers/local_commits_controller.go index 579fda30f..e90bd02fc 100644 --- a/pkg/gui/controllers/local_commits_controller.go +++ b/pkg/gui/controllers/local_commits_controller.go @@ -1268,12 +1268,22 @@ func moveCommitsToArgs( func (self *LocalCommitsController) moveDown(selectedCommits []*models.Commit, startIdx int, endIdx int) error { updateRefBranches := self.context().UpdateRefBranches() destination := commitsDestination{index: endIdx + 2} + if len(updateRefBranchesAt(updateRefBranches, endIdx+1)) > 0 { + // The commit below is the head of branches of a stack. Move to the end + // of these branches, past their update-ref todos only. + destination = destinationBelowUpdateRefs(updateRefBranches, endIdx+1) + } return self.move(selectedCommits, startIdx, endIdx, destination, updateRefBranches, nil) } func (self *LocalCommitsController) moveUp(selectedCommits []*models.Commit, startIdx int, endIdx int) error { updateRefBranches := self.context().UpdateRefBranches() destination := destinationBelowUpdateRefs(updateRefBranches, startIdx-1) + if len(updateRefBranchesAt(updateRefBranches, startIdx)) > 0 { + // The topmost commit is the head of branches of a stack. Move to the + // beginning of the branch above, past their update-ref todos only. + destination = commitsDestination{index: startIdx} + } return self.move(selectedCommits, startIdx, endIdx, destination, updateRefBranches, nil) } diff --git a/pkg/integration/tests/interactive_rebase/move_across_branch_boundary_outside_rebase.go b/pkg/integration/tests/interactive_rebase/move_across_branch_boundary_outside_rebase.go index 36f1f5312..a75ec54bc 100644 --- a/pkg/integration/tests/interactive_rebase/move_across_branch_boundary_outside_rebase.go +++ b/pkg/integration/tests/interactive_rebase/move_across_branch_boundary_outside_rebase.go @@ -19,6 +19,9 @@ var MoveAcrossBranchBoundaryOutsideRebase = NewIntegrationTest(NewIntegrationTes CreateNCommits(1). NewBranch("branch1"). CreateNCommitsStartingAt(2, 2). + // A second branch at the head of branch1, so that the boundary + // consists of two update-ref todos + NewBranch("branch1b"). NewBranch("branch2"). CreateNCommitsStartingAt(2, 4) @@ -35,6 +38,16 @@ var MoveAcrossBranchBoundaryOutsideRebase = NewIntegrationTest(NewIntegrationTes Contains("CI commit-01"), ). NavigateToLine(Contains("commit-04")). + // Moving the commit down past both update-ref todos makes it the + // head of both branches; it stays selected + Press(keys.Commits.MoveDownCommit). + Lines( + Contains("CI commit-05"), + Contains("CI * commit-04").IsSelected(), + Contains("CI commit-03"), + Contains("CI commit-02"), + Contains("CI commit-01"), + ). Press(keys.Commits.MoveDownCommit). Lines( Contains("CI commit-05"), @@ -42,6 +55,32 @@ var MoveAcrossBranchBoundaryOutsideRebase = NewIntegrationTest(NewIntegrationTes Contains("CI commit-04").IsSelected(), Contains("CI commit-02"), Contains("CI commit-01"), + ). + Press(keys.Commits.MoveUpCommit). + Lines( + Contains("CI commit-05"), + Contains("CI * commit-04").IsSelected(), + Contains("CI commit-03"), + Contains("CI commit-02"), + Contains("CI commit-01"), + ). + // Moving it up past the update-ref todos makes it the first commit + // of branch2 again + Press(keys.Commits.MoveUpCommit). + Lines( + Contains("CI commit-05"), + Contains("CI commit-04").IsSelected(), + Contains("CI * commit-03"), + Contains("CI commit-02"), + Contains("CI commit-01"), + ). + Press(keys.Commits.MoveUpCommit). + Lines( + Contains("CI commit-04").IsSelected(), + Contains("CI commit-05"), + Contains("CI * commit-03"), + Contains("CI commit-02"), + Contains("CI commit-01"), ) }, })