refactor: remove additional git calls

This commit is contained in:
Jan De Dobbeleer 2021-01-07 19:29:34 +01:00 committed by Jan De Dobbeleer
parent 3a86f49b72
commit 667151fe28
3 changed files with 15 additions and 11 deletions

View file

@ -79,7 +79,7 @@ func (env *environment) getcwd() string {
return env.cwd return env.cwd
} }
correctPath := func(pwd string) string { correctPath := func(pwd string) string {
// on Windows, and being case sentisitive and not consistent and all, this gives silly issues // on Windows, and being case sensitive and not consistent and all, this gives silly issues
return strings.Replace(pwd, "c:", "C:", 1) return strings.Replace(pwd, "c:", "C:", 1)
} }
if env.args != nil && *env.args.PWD != "" { if env.args != nil && *env.args.PWD != "" {

View file

@ -268,7 +268,8 @@ func (g *git) getGitHEADContext(ref string) string {
} }
// rebase // rebase
if g.hasGitFolder("rebase-merge") { if g.hasGitFolder("rebase-merge") {
origin := g.getGitRefFileSymbolicName("rebase-merge/orig-head") head := g.getGitFileContents("rebase-merge/head-name")
origin := strings.Replace(head, "refs/heads/", "", 1)
onto := g.getGitRefFileSymbolicName("rebase-merge/onto") onto := g.getGitRefFileSymbolicName("rebase-merge/onto")
step := g.getGitFileContents("rebase-merge/msgnum") step := g.getGitFileContents("rebase-merge/msgnum")
total := g.getGitFileContents("rebase-merge/end") total := g.getGitFileContents("rebase-merge/end")
@ -284,16 +285,19 @@ func (g *git) getGitHEADContext(ref string) string {
return fmt.Sprintf("%s%s%s (%s/%s) at %s", icon, branchIcon, origin, step, total, ref) return fmt.Sprintf("%s%s%s (%s/%s) at %s", icon, branchIcon, origin, step, total, ref)
} }
// merge // merge
if g.hasGitFile("MERGE_HEAD") { if g.hasGitFile("MERGE_MSG") && g.hasGitFile("MERGE_HEAD") {
mergeHEAD := g.getGitRefFileSymbolicName("MERGE_HEAD")
icon := g.props.getString(MergeIcon, "\uE727 ") icon := g.props.getString(MergeIcon, "\uE727 ")
return fmt.Sprintf("%s%s%s into %s", icon, branchIcon, mergeHEAD, ref) mergeContext := g.getGitFileContents("MERGE_MSG")
matches := findNamedRegexMatch(`Merge branch '(?P<head>.*)' into`, mergeContext)
if matches != nil && matches["head"] != "" {
return fmt.Sprintf("%s%s%s into %s", icon, branchIcon, matches["head"], ref)
}
} }
// cherry-pick // cherry-pick
if g.hasGitFile("CHERRY_PICK_HEAD") { if g.hasGitFile("CHERRY_PICK_HEAD") {
sha := g.getGitRefFileSymbolicName("CHERRY_PICK_HEAD") sha := g.getGitFileContents("CHERRY_PICK_HEAD")
icon := g.props.getString(CherryPickIcon, "\uE29B ") icon := g.props.getString(CherryPickIcon, "\uE29B ")
return fmt.Sprintf("%s%s onto %s", icon, sha, ref) return fmt.Sprintf("%s%s onto %s", icon, sha[0:6], ref)
} }
return ref return ref
} }

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -86,7 +87,7 @@ func setupHEADContextEnv(context *detachedContext) *git {
env := new(MockedEnvironment) env := new(MockedEnvironment)
env.On("hasFolder", "/rebase-merge").Return(context.rebaseMerge) env.On("hasFolder", "/rebase-merge").Return(context.rebaseMerge)
env.On("hasFolder", "/rebase-apply").Return(context.rebaseApply) env.On("hasFolder", "/rebase-apply").Return(context.rebaseApply)
env.On("getFileContent", "/rebase-merge/orig-head").Return(context.origin) env.On("getFileContent", "/rebase-merge/head-name").Return(context.origin)
env.On("getFileContent", "/rebase-merge/onto").Return(context.onto) env.On("getFileContent", "/rebase-merge/onto").Return(context.onto)
env.On("getFileContent", "/rebase-merge/msgnum").Return(context.step) env.On("getFileContent", "/rebase-merge/msgnum").Return(context.step)
env.On("getFileContent", "/rebase-apply/next").Return(context.step) env.On("getFileContent", "/rebase-apply/next").Return(context.step)
@ -94,15 +95,14 @@ func setupHEADContextEnv(context *detachedContext) *git {
env.On("getFileContent", "/rebase-apply/last").Return(context.total) env.On("getFileContent", "/rebase-apply/last").Return(context.total)
env.On("getFileContent", "/rebase-apply/head-name").Return(context.origin) env.On("getFileContent", "/rebase-apply/head-name").Return(context.origin)
env.On("getFileContent", "/CHERRY_PICK_HEAD").Return(context.cherryPickSHA) env.On("getFileContent", "/CHERRY_PICK_HEAD").Return(context.cherryPickSHA)
env.On("getFileContent", "/MERGE_HEAD").Return(context.mergeHEAD) env.On("getFileContent", "/MERGE_MSG").Return(fmt.Sprintf("Merge branch '%s' into %s", context.mergeHEAD, context.onto))
env.On("hasFilesInDir", "", "CHERRY_PICK_HEAD").Return(context.cherryPick) env.On("hasFilesInDir", "", "CHERRY_PICK_HEAD").Return(context.cherryPick)
env.On("hasFilesInDir", "", "MERGE_MSG").Return(context.merge)
env.On("hasFilesInDir", "", "MERGE_HEAD").Return(context.merge) env.On("hasFilesInDir", "", "MERGE_HEAD").Return(context.merge)
env.mockGitCommand(context.currentCommit, "rev-parse", "--short", "HEAD") env.mockGitCommand(context.currentCommit, "rev-parse", "--short", "HEAD")
env.mockGitCommand(context.tagName, "describe", "--tags", "--exact-match") env.mockGitCommand(context.tagName, "describe", "--tags", "--exact-match")
env.mockGitCommand(context.origin, "name-rev", "--name-only", "--exclude=tags/*", context.origin) env.mockGitCommand(context.origin, "name-rev", "--name-only", "--exclude=tags/*", context.origin)
env.mockGitCommand(context.onto, "name-rev", "--name-only", "--exclude=tags/*", context.onto) env.mockGitCommand(context.onto, "name-rev", "--name-only", "--exclude=tags/*", context.onto)
env.mockGitCommand(context.cherryPickSHA, "name-rev", "--name-only", "--exclude=tags/*", context.cherryPickSHA)
env.mockGitCommand(context.mergeHEAD, "name-rev", "--name-only", "--exclude=tags/*", context.mergeHEAD)
g := &git{ g := &git{
env: env, env: env,
repo: &gitRepo{ repo: &gitRepo{