mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-01-12 04:23:03 +02:00
better handling of current branch name
This commit is contained in:
parent
c7f68a2ef9
commit
d027cf969c
@ -3,7 +3,9 @@ package commands
|
|||||||
// Branch : A git branch
|
// Branch : A git branch
|
||||||
// duplicating this for now
|
// duplicating this for now
|
||||||
type Branch struct {
|
type Branch struct {
|
||||||
Name string
|
Name string
|
||||||
|
// the displayname is something like '(HEAD detached at 123asdf)', whereas in that case the name would be '123asdf'
|
||||||
|
DisplayName string
|
||||||
Recency string
|
Recency string
|
||||||
Pushables string
|
Pushables string
|
||||||
Pullables string
|
Pullables string
|
||||||
|
@ -34,14 +34,6 @@ func NewBranchListBuilder(log *logrus.Entry, gitCommand *GitCommand) (*BranchLis
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BranchListBuilder) obtainCurrentBranchName() string {
|
|
||||||
branchName, err := b.GitCommand.CurrentBranchName()
|
|
||||||
if err != nil {
|
|
||||||
panic(err.Error())
|
|
||||||
}
|
|
||||||
return branchName
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *BranchListBuilder) obtainBranches() []*Branch {
|
func (b *BranchListBuilder) obtainBranches() []*Branch {
|
||||||
cmdStr := `git for-each-ref --sort=-committerdate --format="%(HEAD)|%(refname:short)|%(upstream:short)|%(upstream:track)" refs/heads`
|
cmdStr := `git for-each-ref --sort=-committerdate --format="%(HEAD)|%(refname:short)|%(upstream:short)|%(upstream:track)" refs/heads`
|
||||||
output, err := b.GitCommand.OSCommand.RunCommandWithOutput(cmdStr)
|
output, err := b.GitCommand.OSCommand.RunCommandWithOutput(cmdStr)
|
||||||
@ -100,7 +92,6 @@ func (b *BranchListBuilder) obtainBranches() []*Branch {
|
|||||||
|
|
||||||
// Build the list of branches for the current repo
|
// Build the list of branches for the current repo
|
||||||
func (b *BranchListBuilder) Build() []*Branch {
|
func (b *BranchListBuilder) Build() []*Branch {
|
||||||
currentBranchName := b.obtainCurrentBranchName()
|
|
||||||
branches := b.obtainBranches()
|
branches := b.obtainBranches()
|
||||||
|
|
||||||
reflogBranches := b.obtainReflogBranches()
|
reflogBranches := b.obtainReflogBranches()
|
||||||
@ -128,7 +119,6 @@ outer:
|
|||||||
for i, branch := range branches {
|
for i, branch := range branches {
|
||||||
if branch.Head {
|
if branch.Head {
|
||||||
foundHead = true
|
foundHead = true
|
||||||
branch.Name = currentBranchName
|
|
||||||
branch.Recency = " *"
|
branch.Recency = " *"
|
||||||
branches = append(branches[0:i], branches[i+1:]...)
|
branches = append(branches[0:i], branches[i+1:]...)
|
||||||
branches = append([]*Branch{branch}, branches...)
|
branches = append([]*Branch{branch}, branches...)
|
||||||
@ -136,7 +126,11 @@ outer:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !foundHead {
|
if !foundHead {
|
||||||
branches = append([]*Branch{{Name: currentBranchName, Head: true, Recency: " *"}}, branches...)
|
currentBranchName, currentBranchDisplayName, err := b.GitCommand.CurrentBranchName()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
branches = append([]*Branch{{Name: currentBranchName, DisplayName: currentBranchDisplayName, Head: true, Recency: " *"}}, branches...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return branches
|
return branches
|
||||||
|
@ -279,7 +279,7 @@ func (c *CommitListBuilder) setCommitCherryPickStatuses(commits []*Commit) ([]*C
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *CommitListBuilder) getMergeBase() (string, error) {
|
func (c *CommitListBuilder) getMergeBase() (string, error) {
|
||||||
currentBranch, err := c.GitCommand.CurrentBranchName()
|
currentBranch, _, err := c.GitCommand.CurrentBranchName()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -331,19 +331,29 @@ func (c *GitCommand) NewBranch(name string, baseBranch string) error {
|
|||||||
return c.OSCommand.RunCommand("git checkout -b %s %s", name, baseBranch)
|
return c.OSCommand.RunCommand("git checkout -b %s %s", name, baseBranch)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CurrentBranchName is a function.
|
// CurrentBranchName get the current branch name and displayname.
|
||||||
func (c *GitCommand) CurrentBranchName() (string, error) {
|
// the first returned string is the name and the second is the displayname
|
||||||
|
// e.g. name is 123asdf and displayname is '(HEAD detached at 123asdf)'
|
||||||
|
func (c *GitCommand) CurrentBranchName() (string, string, error) {
|
||||||
branchName, err := c.OSCommand.RunCommandWithOutput("git symbolic-ref --short HEAD")
|
branchName, err := c.OSCommand.RunCommandWithOutput("git symbolic-ref --short HEAD")
|
||||||
if err != nil || branchName == "HEAD\n" {
|
if err == nil && branchName != "HEAD\n" {
|
||||||
output, err := c.OSCommand.RunCommandWithOutput("git branch --contains")
|
trimmedBranchName := strings.TrimSpace(branchName)
|
||||||
if err != nil {
|
return trimmedBranchName, trimmedBranchName, nil
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
re := regexp.MustCompile(CurrentBranchNameRegex)
|
|
||||||
match := re.FindStringSubmatch(output)
|
|
||||||
branchName = match[1]
|
|
||||||
}
|
}
|
||||||
return strings.TrimSpace(branchName), nil
|
output, err := c.OSCommand.RunCommandWithOutput("git branch --contains")
|
||||||
|
if err != nil {
|
||||||
|
return "", "", 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:]
|
||||||
|
return branchName, displayBranchName, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "HEAD", "HEAD", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteBranch delete branch
|
// DeleteBranch delete branch
|
||||||
|
@ -1549,7 +1549,7 @@ func TestGitCommandCurrentBranchName(t *testing.T) {
|
|||||||
type scenario struct {
|
type scenario struct {
|
||||||
testName string
|
testName string
|
||||||
command func(string, ...string) *exec.Cmd
|
command func(string, ...string) *exec.Cmd
|
||||||
test func(string, error)
|
test func(string, string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
scenarios := []scenario{
|
scenarios := []scenario{
|
||||||
@ -1559,9 +1559,10 @@ func TestGitCommandCurrentBranchName(t *testing.T) {
|
|||||||
assert.Equal(t, "git", cmd)
|
assert.Equal(t, "git", cmd)
|
||||||
return exec.Command("echo", "master")
|
return exec.Command("echo", "master")
|
||||||
},
|
},
|
||||||
func(output string, err error) {
|
func(name string, displayname string, err error) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.EqualValues(t, "master", output)
|
assert.EqualValues(t, "master", name)
|
||||||
|
assert.EqualValues(t, "master", displayname)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -1580,9 +1581,32 @@ func TestGitCommandCurrentBranchName(t *testing.T) {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
func(output string, err error) {
|
func(name string, displayname string, err error) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.EqualValues(t, "master", output)
|
assert.EqualValues(t, "master", name)
|
||||||
|
assert.EqualValues(t, "master", displayname)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"handles a detached head",
|
||||||
|
func(cmd string, args ...string) *exec.Cmd {
|
||||||
|
assert.EqualValues(t, "git", cmd)
|
||||||
|
|
||||||
|
switch args[0] {
|
||||||
|
case "symbolic-ref":
|
||||||
|
assert.EqualValues(t, []string{"symbolic-ref", "--short", "HEAD"}, args)
|
||||||
|
return exec.Command("test")
|
||||||
|
case "branch":
|
||||||
|
assert.EqualValues(t, []string{"branch", "--contains"}, args)
|
||||||
|
return exec.Command("echo", "* (HEAD detached at 123abcd)")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
func(name string, displayname string, err error) {
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, "123abcd", name)
|
||||||
|
assert.EqualValues(t, "(HEAD detached at 123abcd)", displayname)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -1591,9 +1615,10 @@ func TestGitCommandCurrentBranchName(t *testing.T) {
|
|||||||
assert.Equal(t, "git", cmd)
|
assert.Equal(t, "git", cmd)
|
||||||
return exec.Command("test")
|
return exec.Command("test")
|
||||||
},
|
},
|
||||||
func(output string, err error) {
|
func(name string, displayname string, err error) {
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.EqualValues(t, "", output)
|
assert.EqualValues(t, "", name)
|
||||||
|
assert.EqualValues(t, "", displayname)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -22,14 +22,18 @@ func GetBranchListDisplayStrings(branches []*commands.Branch, fullDescription bo
|
|||||||
|
|
||||||
// getBranchDisplayStrings returns the display string of branch
|
// getBranchDisplayStrings returns the display string of branch
|
||||||
func getBranchDisplayStrings(b *commands.Branch, fullDescription bool) []string {
|
func getBranchDisplayStrings(b *commands.Branch, fullDescription bool) []string {
|
||||||
displayName := utils.ColoredString(b.Name, GetBranchColor(b.Name))
|
displayName := b.Name
|
||||||
|
if b.DisplayName != "" {
|
||||||
|
displayName = b.DisplayName
|
||||||
|
}
|
||||||
|
coloredName := utils.ColoredString(displayName, GetBranchColor(b.Name))
|
||||||
if b.Pushables != "" && b.Pullables != "" && b.Pushables != "?" && b.Pullables != "?" {
|
if b.Pushables != "" && b.Pullables != "" && b.Pushables != "?" && b.Pullables != "?" {
|
||||||
trackColor := color.FgYellow
|
trackColor := color.FgYellow
|
||||||
if b.Pushables == "0" && b.Pullables == "0" {
|
if b.Pushables == "0" && b.Pullables == "0" {
|
||||||
trackColor = color.FgGreen
|
trackColor = color.FgGreen
|
||||||
}
|
}
|
||||||
track := utils.ColoredString(fmt.Sprintf("↑%s↓%s", b.Pushables, b.Pullables), trackColor)
|
track := utils.ColoredString(fmt.Sprintf("↑%s↓%s", b.Pushables, b.Pullables), trackColor)
|
||||||
displayName = fmt.Sprintf("%s %s", displayName, track)
|
coloredName = fmt.Sprintf("%s %s", coloredName, track)
|
||||||
}
|
}
|
||||||
|
|
||||||
recencyColor := color.FgCyan
|
recencyColor := color.FgCyan
|
||||||
@ -38,10 +42,10 @@ func getBranchDisplayStrings(b *commands.Branch, fullDescription bool) []string
|
|||||||
}
|
}
|
||||||
|
|
||||||
if fullDescription {
|
if fullDescription {
|
||||||
return []string{utils.ColoredString(b.Recency, recencyColor), displayName, utils.ColoredString(b.UpstreamName, color.FgYellow)}
|
return []string{utils.ColoredString(b.Recency, recencyColor), coloredName, utils.ColoredString(b.UpstreamName, color.FgYellow)}
|
||||||
}
|
}
|
||||||
|
|
||||||
return []string{utils.ColoredString(b.Recency, recencyColor), displayName}
|
return []string{utils.ColoredString(b.Recency, recencyColor), coloredName}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBranchColor branch color
|
// GetBranchColor branch color
|
||||||
|
Loading…
Reference in New Issue
Block a user