1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-30 03:23:08 +03:00

support detached heads when showing the selected branch

This commit is contained in:
Jesse Duffield
2019-11-17 12:07:36 +11:00
parent 6b7aaeca45
commit 55ff0c0dee
5 changed files with 39 additions and 7 deletions

View File

@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"
@ -21,6 +22,13 @@ import (
gogit "gopkg.in/src-d/go-git.v4"
)
// this takes something like:
// * (HEAD detached at 264fc6f5)
// remotes
// and returns '264fc6f5' as the second match
const CurrentBranchNameRegex = `(?m)^\*.*?([^ ]*?)\)?$`
func verifyInGitRepo(runCmd func(string) error) error {
return runCmd("git status")
}
@ -325,8 +333,11 @@ func (c *GitCommand) NewBranch(name string) error {
// CurrentBranchName is a function.
func (c *GitCommand) CurrentBranchName() (string, error) {
branchName, err := c.OSCommand.RunCommandWithOutput("git symbolic-ref --short HEAD")
if err != nil {
branchName, err = c.OSCommand.RunCommandWithOutput("git rev-parse --short HEAD")
if err != nil || branchName == "HEAD\n" {
output, err := c.OSCommand.RunCommandWithOutput("git branch --contains")
re := regexp.MustCompile(CurrentBranchNameRegex)
match := re.FindStringSubmatch(output)
branchName = match[1]
if err != nil {
return "", err
}

View File

@ -22,14 +22,15 @@ func (c *GitCommand) GetRemotes() ([]*Remote, error) {
// first step is to get our remotes from go-git
remotes := make([]*Remote, len(goGitRemotes))
for i, goGitRemote := range goGitRemotes {
name := goGitRemote.Config().Name
remoteName := goGitRemote.Config().Name
re := regexp.MustCompile(fmt.Sprintf("%s\\/(.*)", name))
re := regexp.MustCompile(fmt.Sprintf("%s\\/(.*)", remoteName))
matches := re.FindAllStringSubmatch(remoteBranchesStr, -1)
branches := make([]*RemoteBranch, len(matches))
for j, match := range matches {
branches[j] = &RemoteBranch{
Name: match[1],
Name: match[1],
RemoteName: remoteName,
}
}

View File

@ -6,8 +6,9 @@ import (
// Remote Branch : A git remote branch
type RemoteBranch struct {
Name string
Selected bool
Name string
Selected bool
RemoteName string
}
// GetDisplayStrings returns the display string of branch