mirror of
https://github.com/jesseduffield/lazygit.git
synced 2026-11-21 04:46:29 +03:00
Allow merging into a detached HEAD
Pressing the merge key while HEAD is detached shows an error. This also happens during a rebase and during a bisect, because HEAD is detached then. As a result, you can't squash merge another branch at the base of the current one by starting an interactive rebase with `i`. The workaround is to squash merge the branch at the tip and move the commit down. If the merged changes conflict with the branch's own commits, you have to resolve those conflicts twice this way. Remove the check. A merge on a detached HEAD is no riskier than a commit, a cherry-pick or a revert, and lazygit allows all of those there. It is also useful outside of a rebase, for example to check out master as a throwaway detached HEAD and merge several branches into it to see whether they conflict. There is no reason to prevent the user from doing these things. If the merge conflicts during a rebase, the merge takes precedence over the rebase in the working tree state. So continuing goes to the merge first and then to the rebase. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bb924c7ccd
commit
73ac8fe6c6
8 files changed
+202
-11
No files matched your search
@@ -456,6 +456,8 @@ git:
|
||||
|
||||
# The commit message to use for a squash merge commit. Can contain
|
||||
# "{{selectedRef}}" and "{{currentBranch}}" placeholders.
|
||||
# If HEAD is detached, "{{currentBranch}}" is the branch being rebased, or
|
||||
# "HEAD" if no rebase is in progress.
|
||||
squashMergeMessage: Squash merge {{selectedRef}} into {{currentBranch}}
|
||||
|
||||
# list of branches that are considered 'main' branches, used when displaying
|
||||
|
||||
@@ -236,13 +236,6 @@ func (self *BranchCommands) countDifferences(from, to string) (string, error) {
|
||||
return self.cmd.New(cmdArgs).DontLog().RunWithOutput()
|
||||
}
|
||||
|
||||
func (self *BranchCommands) IsHeadDetached() bool {
|
||||
cmdArgs := NewGitCmd("symbolic-ref").Arg("-q", "HEAD").ToArgv()
|
||||
|
||||
err := self.cmd.New(cmdArgs).DontLog().Run()
|
||||
return err != nil
|
||||
}
|
||||
|
||||
func (self *BranchCommands) Rename(oldName string, newName string) error {
|
||||
cmdArgs := NewGitCmd("branch").
|
||||
Arg("--move", oldName, newName).
|
||||
|
||||
@@ -424,6 +424,7 @@ type MergingConfig struct {
|
||||
// Extra args passed to `git merge`, e.g. --no-ff
|
||||
Args string `yaml:"args" jsonschema:"example=--no-ff"`
|
||||
// The commit message to use for a squash merge commit. Can contain "{{selectedRef}}" and "{{currentBranch}}" placeholders.
|
||||
// If HEAD is detached, "{{currentBranch}}" is the branch being rebased, or "HEAD" if no rebase is in progress.
|
||||
SquashMergeMessage string `yaml:"squashMergeMessage"`
|
||||
}
|
||||
|
||||
|
||||
@@ -603,9 +603,6 @@ func (self *MergeAndRebaseHelper) RebaseOntoRef(ref string) error {
|
||||
}
|
||||
|
||||
func (self *MergeAndRebaseHelper) MergeRefIntoCheckedOutBranch(refName string) error {
|
||||
if self.c.Git().Branch.IsHeadDetached() {
|
||||
return errors.New("Cannot merge branch in detached head state. You might have checked out a commit directly or a remote branch, in which case you should checkout the local branch you want to be on")
|
||||
}
|
||||
if self.c.Model().Branches[0].Name == refName {
|
||||
return errors.New(self.c.Tr.CantMergeBranchIntoItself)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package branch
|
||||
|
||||
import (
|
||||
"github.com/jesseduffield/lazygit/pkg/config"
|
||||
. "github.com/jesseduffield/lazygit/pkg/integration/components"
|
||||
)
|
||||
|
||||
var MergeIntoDetachedHead = NewIntegrationTest(NewIntegrationTestArgs{
|
||||
Description: "Merge and squash merge branches into a detached HEAD",
|
||||
ExtraCmdArgs: []string{},
|
||||
Skip: false,
|
||||
SetupConfig: func(config *config.AppConfig) {
|
||||
config.GetUserConfig().Git.LocalBranchSortOrder = "alphabetical"
|
||||
},
|
||||
SetupRepo: func(shell *Shell) {
|
||||
shell.
|
||||
EmptyCommit("one").
|
||||
NewBranch("branch1").
|
||||
EmptyCommit("branch1").
|
||||
Checkout("master").
|
||||
NewBranch("branch2").
|
||||
CreateFileAndAdd("file", "content").
|
||||
Commit("branch2").
|
||||
Checkout("master").
|
||||
EmptyCommit("two").
|
||||
Checkout("master^0")
|
||||
},
|
||||
Run: func(t *TestDriver, keys config.KeybindingConfig) {
|
||||
t.Views().Branches().
|
||||
Focus().
|
||||
Lines(
|
||||
Contains("HEAD detached").IsSelected(),
|
||||
Contains("branch1"),
|
||||
Contains("branch2"),
|
||||
Contains("master"),
|
||||
).
|
||||
NavigateToLine(Contains("branch1")).
|
||||
Press(keys.Branches.MergeIntoCurrentBranch)
|
||||
|
||||
t.ExpectPopup().Menu().
|
||||
Title(Equals("Merge")).
|
||||
Select(Contains("Regular merge (with merge commit)")).
|
||||
Tooltip(Equals("Merge 'branch1' into 'HEAD', creating a merge commit.")).
|
||||
Confirm()
|
||||
|
||||
t.Views().Commits().
|
||||
Lines(
|
||||
Contains("◎─╮ Merge branch 'branch1' into HEAD").IsSelected(),
|
||||
Contains("│ ○ * branch1"),
|
||||
Contains("○ │ two"),
|
||||
Contains("○─╯ one"),
|
||||
)
|
||||
|
||||
t.Views().Branches().
|
||||
Focus().
|
||||
NavigateToLine(Contains("branch2")).
|
||||
Press(keys.Branches.MergeIntoCurrentBranch)
|
||||
|
||||
t.ExpectPopup().Menu().
|
||||
Title(Equals("Merge")).
|
||||
Select(Contains("Squash merge and commit")).
|
||||
Tooltip(Equals("Squash merge 'branch2' into 'HEAD' as a single commit.")).
|
||||
Confirm()
|
||||
|
||||
t.Views().Commits().
|
||||
Lines(
|
||||
Contains("Squash merge branch2 into HEAD").IsSelected(),
|
||||
Contains("Merge branch 'branch1' into HEAD"),
|
||||
Contains("branch1"),
|
||||
Contains("two"),
|
||||
Contains("one"),
|
||||
)
|
||||
|
||||
t.Views().Branches().
|
||||
Lines(
|
||||
Contains("HEAD detached"),
|
||||
Contains("branch1"),
|
||||
Contains("branch2"),
|
||||
Contains("master"),
|
||||
)
|
||||
},
|
||||
})
|
||||
@@ -0,0 +1,114 @@
|
||||
package branch
|
||||
|
||||
import (
|
||||
"github.com/jesseduffield/lazygit/pkg/config"
|
||||
. "github.com/jesseduffield/lazygit/pkg/integration/components"
|
||||
)
|
||||
|
||||
var SquashMergeDuringRebase = NewIntegrationTest(NewIntegrationTestArgs{
|
||||
Description: "Squash merge a branch with conflicts at the base of an interactive rebase, then continue the merge and the rebase",
|
||||
ExtraCmdArgs: []string{},
|
||||
Skip: false,
|
||||
SetupConfig: func(config *config.AppConfig) {
|
||||
config.GetUserConfig().Git.LocalBranchSortOrder = "alphabetical"
|
||||
},
|
||||
SetupRepo: func(shell *Shell) {
|
||||
shell.
|
||||
CreateFileAndAdd("file", "original\n").
|
||||
Commit("original").
|
||||
NewBranch("other").
|
||||
UpdateFileAndAdd("file", "other change\n").
|
||||
Commit("other change").
|
||||
Checkout("master").
|
||||
UpdateFileAndAdd("file", "master change\n").
|
||||
Commit("master change").
|
||||
NewBranch("mybranch").
|
||||
CreateFileAndAdd("file2", "content\n").
|
||||
Commit("mybranch 1").
|
||||
CreateFileAndAdd("file3", "content\n").
|
||||
Commit("mybranch 2")
|
||||
},
|
||||
Run: func(t *TestDriver, keys config.KeybindingConfig) {
|
||||
t.Views().Commits().
|
||||
Focus().
|
||||
Lines(
|
||||
Contains("mybranch 2").IsSelected(),
|
||||
Contains("mybranch 1"),
|
||||
Contains("master change"),
|
||||
Contains("original"),
|
||||
).
|
||||
Press(keys.Commits.StartInteractiveRebase).
|
||||
Lines(
|
||||
Contains("─── Pending rebase todos"),
|
||||
Contains("mybranch 2").IsSelected(),
|
||||
Contains("mybranch 1"),
|
||||
Contains("─── Commits"),
|
||||
Contains("master change"),
|
||||
Contains("original"),
|
||||
)
|
||||
|
||||
t.Views().Branches().
|
||||
Focus().
|
||||
Lines(
|
||||
Contains("(no branch, rebasing mybranch)"),
|
||||
Contains("master"),
|
||||
Contains("mybranch").IsSelected(),
|
||||
Contains("other"),
|
||||
).
|
||||
NavigateToLine(Contains("other")).
|
||||
Press(keys.Branches.MergeIntoCurrentBranch)
|
||||
|
||||
t.ExpectPopup().Menu().
|
||||
Title(Equals("Merge")).
|
||||
Select(Contains("Squash merge and commit")).
|
||||
Tooltip(Equals("Squash merge 'other' into 'mybranch' as a single commit.")).
|
||||
Confirm()
|
||||
|
||||
t.Common().AcknowledgeConflicts()
|
||||
|
||||
t.Views().Status().Content(Contains("(merging) repo → "))
|
||||
|
||||
t.GlobalPress(keys.Universal.CreateRebaseOptionsMenu)
|
||||
t.ExpectPopup().Menu().
|
||||
Title(Equals("Merge options")).
|
||||
Cancel()
|
||||
|
||||
t.Views().Files().
|
||||
IsFocused().
|
||||
Lines(
|
||||
Contains("UU file").IsSelected(),
|
||||
).
|
||||
Tap(func() {
|
||||
t.Shell().UpdateFile("file", "resolved\n")
|
||||
}).
|
||||
Press(keys.Universal.Refresh)
|
||||
|
||||
t.Common().ContinueOnConflictsResolved("merge")
|
||||
|
||||
t.Views().Status().Content(Contains("(rebasing) repo → "))
|
||||
|
||||
t.Views().Commits().
|
||||
Lines(
|
||||
Contains("─── Pending rebase todos"),
|
||||
Contains("mybranch 2"),
|
||||
Contains("mybranch 1"),
|
||||
Contains("─── Commits"),
|
||||
Contains("Squash merge other into mybranch"),
|
||||
Contains("master change"),
|
||||
Contains("original"),
|
||||
)
|
||||
|
||||
t.Common().ContinueRebase()
|
||||
|
||||
t.Views().Status().Content(Equals("repo → mybranch"))
|
||||
|
||||
t.Views().Commits().
|
||||
Lines(
|
||||
Contains("mybranch 2"),
|
||||
Contains("mybranch 1"),
|
||||
Contains("Squash merge other into mybranch"),
|
||||
Contains("master change"),
|
||||
Contains("original"),
|
||||
)
|
||||
},
|
||||
})
|
||||
@@ -71,6 +71,7 @@ var tests = []*components.IntegrationTest{
|
||||
branch.DeleteWhileFiltering,
|
||||
branch.DetachedHead,
|
||||
branch.MergeFastForward,
|
||||
branch.MergeIntoDetachedHead,
|
||||
branch.MergeNonFastForward,
|
||||
branch.MoveCommitsToNewBranchFromBaseBranch,
|
||||
branch.MoveCommitsToNewBranchFromMainBranch,
|
||||
@@ -113,6 +114,7 @@ var tests = []*components.IntegrationTest{
|
||||
branch.SquashMerge,
|
||||
branch.SquashMergeAbortAfterConflicts,
|
||||
branch.SquashMergeContinueAfterConflicts,
|
||||
branch.SquashMergeDuringRebase,
|
||||
branch.SquashMergeUncommittedWithConflicts,
|
||||
branch.SquashMergeWithStagedChanges,
|
||||
branch.Suggestions,
|
||||
|
||||
@@ -3988,7 +3988,7 @@
|
||||
},
|
||||
"squashMergeMessage": {
|
||||
"type": "string",
|
||||
"description": "The commit message to use for a squash merge commit. Can contain \"{{selectedRef}}\" and \"{{currentBranch}}\" placeholders.",
|
||||
"description": "The commit message to use for a squash merge commit. Can contain \"{{selectedRef}}\" and \"{{currentBranch}}\" placeholders.\nIf HEAD is detached, \"{{currentBranch}}\" is the branch being rebased, or \"HEAD\" if no rebase is in progress.",
|
||||
"default": "Squash merge {{selectedRef}} into {{currentBranch}}"
|
||||
}
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user