summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2023-01-29 14:19:05 +1100
committerGitHub <noreply@github.com>2023-01-29 14:19:05 +1100
commitf79a8c281f3f0ac500e3f29255cf94e0efbe3b3f (patch)
tree764593bb5675cdbe9b803577d05b0a7c7973c9a9
parent996a1e469f91809f58f2110416acf4a39e6a5f78 (diff)
parent5dec08071902fabf5b9853c55ad3fb33fe14c5e6 (diff)
Merge pull request #2398 from Ryooooooga/fix-detached-head
fix https://github.com/jesseduffield/lazygit/issues/1467
-rw-r--r--pkg/commands/git_commands/branch.go22
-rw-r--r--pkg/commands/git_commands/branch_test.go22
-rw-r--r--pkg/integration/tests/branch/detached_head.go40
-rw-r--r--pkg/integration/tests/tests.go1
4 files changed, 61 insertions, 24 deletions
diff --git a/pkg/commands/git_commands/branch.go b/pkg/commands/git_commands/branch.go
index 117427778..a71e365ea 100644
--- a/pkg/commands/git_commands/branch.go
+++ b/pkg/commands/git_commands/branch.go
@@ -2,19 +2,12 @@ package git_commands
import (
"fmt"
- "regexp"
"strings"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/utils"
)
-// this takes something like:
-// * (HEAD detached at 264fc6f5)
-// remotes
-// and returns '264fc6f5' as the second match
-const CurrentBranchNameRegex = `(?m)^\*.*?([^ ]*?)\)?$`
-
type BranchCommands struct {
*GitCommon
}
@@ -41,19 +34,18 @@ func (self *BranchCommands) CurrentBranchInfo() (BranchInfo, error) {
DetachedHead: false,
}, nil
}
- output, err := self.cmd.New("git branch --contains").DontLog().RunWithOutput()
+ output, err := self.cmd.New(`git branch --points-at=HEAD --format="%(HEAD)%00%(objectname)%00%(refname)"`).DontLog().RunWithOutput()
if err != nil {
return BranchInfo{}, err
}
for _, line := range utils.SplitLines(output) {
- re := regexp.MustCompile(CurrentBranchNameRegex)
- match := re.FindStringSubmatch(line)
- if len(match) > 0 {
- branchName = match[1]
- displayBranchName := match[0][2:]
+ split := strings.Split(strings.TrimRight(line, "\r\n"), "\x00")
+ if len(split) == 3 && split[0] == "*" {
+ sha := split[1]
+ displayName := split[2]
return BranchInfo{
- RefName: branchName,
- DisplayName: displayBranchName,
+ RefName: sha,
+ DisplayName: displayName,
DetachedHead: true,
}, nil
}
diff --git a/pkg/commands/git_commands/branch_test.go b/pkg/commands/git_commands/branch_test.go
index 94456c0f8..2fdf7d9c2 100644
--- a/pkg/commands/git_commands/branch_test.go
+++ b/pkg/commands/git_commands/branch_test.go
@@ -181,26 +181,30 @@ func TestBranchCurrentBranchInfo(t *testing.T) {
},
},
{
- "falls back to git `git branch --contains` if symbolic-ref fails",
+ "falls back to git `git branch --points-at=HEAD` if symbolic-ref fails",
oscommands.NewFakeRunner(t).
Expect(`git symbolic-ref --short HEAD`, "", errors.New("error")).
- Expect(`git branch --contains`, "* (HEAD detached at 8982166a)", nil),
+ Expect(`git branch --points-at=HEAD --format="%(HEAD)%00%(objectname)%00%(refname)"`, "*\x006f71c57a8d4bd6c11399c3f55f42c815527a73a4\x00(HEAD detached at 6f71c57a)\n", nil),
func(info BranchInfo, err error) {
assert.NoError(t, err)
- assert.EqualValues(t, "8982166a", info.RefName)
- assert.EqualValues(t, "(HEAD detached at 8982166a)", info.DisplayName)
+ assert.EqualValues(t, "6f71c57a8d4bd6c11399c3f55f42c815527a73a4", info.RefName)
+ assert.EqualValues(t, "(HEAD detached at 6f71c57a)", info.DisplayName)
assert.True(t, info.DetachedHead)
},
},
{
- "handles a detached head",
+ "handles a detached head (LANG=zh_CN.UTF-8)",
oscommands.NewFakeRunner(t).
Expect(`git symbolic-ref --short HEAD`, "", errors.New("error")).
- Expect(`git branch --contains`, "* (HEAD detached at 123abcd)", nil),
+ Expect(
+ `git branch --points-at=HEAD --format="%(HEAD)%00%(objectname)%00%(refname)"`,
+ "*\x00679b0456f3db7c505b398def84e7d023e5b55a8d\x00(头指针在 679b0456 分离)\n"+
+ " \x00679b0456f3db7c505b398def84e7d023e5b55a8d\x00refs/heads/master\n",
+ nil),
func(info BranchInfo, err error) {
assert.NoError(t, err)
- assert.EqualValues(t, "123abcd", info.RefName)
- assert.EqualValues(t, "(HEAD detached at 123abcd)", info.DisplayName)
+ assert.EqualValues(t, "679b0456f3db7c505b398def84e7d023e5b55a8d", info.RefName)
+ assert.EqualValues(t, "(头指针在 679b0456 分离)", info.DisplayName)
assert.True(t, info.DetachedHead)
},
},
@@ -208,7 +212,7 @@ func TestBranchCurrentBranchInfo(t *testing.T) {
"bubbles up error if there is one",
oscommands.NewFakeRunner(t).
Expect(`git symbolic-ref --short HEAD`, "", errors.New("error")).
- Expect(`git branch --contains`, "", errors.New("error")),
+ Expect(`git branch --points-at=HEAD --format="%(HEAD)%00%(objectname)%00%(refname)"`, "", errors.New("error")),
func(info BranchInfo, err error) {
assert.Error(t, err)
assert.EqualValues(t, "", info.RefName)
diff --git a/pkg/integration/tests/branch/detached_head.go b/pkg/integration/tests/branch/detached_head.go
new file mode 100644
index 000000000..7eb7e18ae
--- /dev/null
+++ b/pkg/integration/tests/branch/detached_head.go
@@ -0,0 +1,40 @@
+package branch
+
+import (
+ "github.com/jesseduffield/lazygit/pkg/config"
+ . "github.com/jesseduffield/lazygit/pkg/integration/components"
+)
+
+var DetachedHead = NewIntegrationTest(NewIntegrationTestArgs{
+ Description: "Create a new branch on detached head",
+ ExtraCmdArgs: "",
+ Skip: false,
+ SetupConfig: func(config *config.AppConfig) {},
+ SetupRepo: func(shell *Shell) {
+ shell.
+ CreateNCommits(10).
+ Checkout("HEAD^")
+ },
+ Run: func(t *TestDriver, keys config.KeybindingConfig) {
+ t.Views().Branches().
+ Focus().
+ Lines(
+ MatchesRegexp(`\*.*HEAD`).IsSelected(),
+ MatchesRegexp(`master`),
+ ).
+ Press(keys.Universal.New)
+
+ t.ExpectPopup().Prompt().
+ Title(MatchesRegexp(`^New Branch Name \(Branch is off of '[0-9a-f]+'\)$`)).
+ Type("new-branch").
+ Confirm()
+
+ t.Views().Branches().
+ Lines(
+ MatchesRegexp(`\* new-branch`).IsSelected(),
+ MatchesRegexp(`master`),
+ )
+
+ t.Git().CurrentBranchName("new-branch")
+ },
+})
diff --git a/pkg/integration/tests/tests.go b/pkg/integration/tests/tests.go
index f7463d0da..8d10b236f 100644
--- a/pkg/integration/tests/tests.go
+++ b/pkg/integration/tests/tests.go
@@ -38,6 +38,7 @@ var tests = []*components.IntegrationTest{
branch.RebaseAndDrop,
branch.Suggestions,
branch.Reset,
+ branch.DetachedHead,
cherry_pick.CherryPick,
cherry_pick.CherryPickConflicts,
commit.Commit,