Skip to content

Commit

Permalink
refactor: reduce git cli calls
Browse files Browse the repository at this point in the history
should impact #45
  • Loading branch information
JanDeDobbeleer committed Oct 10, 2020
1 parent c2c50f2 commit 09d4c95
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 52 deletions.
4 changes: 2 additions & 2 deletions docs/docs/segment-git.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Local changes can also shown by default using the following syntax for both the
"local_staged_icon": "",
"rebase_icon": "",
"cherry_pick_icon": "",
"detached_icon": "",
"commit_icon": "",
"tag_icon": ""
}
}
Expand All @@ -48,5 +48,5 @@ Local changes can also shown by default using the following syntax for both the
- display_status: `boolean` - display the local changes or not
- rebase_icon: `string` - icon/text to display before the context when in a rebase
- cherry_pick_icon: `string` - icon/text to display before the context when doing a cherry-pick
- detached_icon: `string` - icon/text to display before the detached head context
- commit_icon: `string` - icon/text to display before the commit context
- tag_icon: `string` - icon/text to display before the tag context
66 changes: 31 additions & 35 deletions segment_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type gitRepo struct {
staging *gitStatus
ahead int
behind int
branch string
HEAD string
upstream string
stashCount int
}
Expand Down Expand Up @@ -51,8 +51,8 @@ const (
RebaseIcon Property = "rebase_icon"
//CherryPickIcon shows before the cherry-pick context
CherryPickIcon Property = "cherry_pick_icon"
//DetachedIcon shows before the detached context
DetachedIcon Property = "detached_icon"
//CommitIcon shows before the detached context
CommitIcon Property = "commit_icon"
//TagIcon shows before the tag context
TagIcon Property = "tag_icon"
)
Expand All @@ -69,7 +69,7 @@ func (g *git) string() string {
g.getGitStatus()
buffer := new(bytes.Buffer)
// branchName
fmt.Fprintf(buffer, "%s", g.repo.branch)
fmt.Fprintf(buffer, "%s", g.repo.HEAD)
displayStatus := g.props.getBool(DisplayStatus, true)
if !displayStatus {
return buffer.String()
Expand Down Expand Up @@ -115,7 +115,7 @@ func (g *git) getGitStatus() {
g.repo.behind, _ = strconv.Atoi(status["behind"])
g.repo.upstream = status["upstream"]
}
g.repo.branch = g.getGitHEADContext()
g.repo.HEAD = g.getGitHEADContext(status["local"])
g.repo.stashCount = g.getStashContext()
}

Expand All @@ -124,52 +124,48 @@ func (g *git) getGitCommandOutput(args ...string) string {
return g.env.runCommand("git", args...)
}

func (g *git) getGitHEADContext() string {
commit := g.getGitCommandOutput("rev-parse", "--short", "HEAD")
rebase := g.getGitCommandOutput("rebase", "--show-current-patch")
if rebase != "" {
return g.getGitRebaseContext(commit)
}
// branch
icon := g.props.getString(BranchIcon, "BRANCH:")
ref := g.getGitCommandOutput("symbolic-ref", "-q", "--short", "HEAD")
func (g *git) getGitHEADContext(ref string) string {
branchIcon := g.props.getString(BranchIcon, "BRANCH:")
if ref == "" {
// get a tag name if there's a match for HEAD
icon = g.props.getString(TagIcon, "TAG:")
ref = g.getGitCommandOutput("describe", "--tags", "--exact-match")
ref = g.getPrettyHEADName()
} else {
ref = fmt.Sprintf("%s%s", branchIcon, ref)
}
// validate additional context
if g.env.hasFiles(".git/CHERRY_PICK_HEAD") {
sha := g.getGitRefFileSymbolicName("CHERRY_PICK_HEAD")
icon := g.props.getString(CherryPickIcon, "CHERRY PICK:")
return fmt.Sprintf("%s%s onto %s", icon, sha, ref)
}
if ref == "" {
ref = commit
icon = g.props.getString(DetachedIcon, "DETACHED:")
}
return fmt.Sprintf("%s%s", icon, ref)
}

func (g *git) getGitRebaseContext(commit string) string {
// rebase
if g.env.hasFolder(".git/rebase-merge") {
origin := g.getGitRefFileSymbolicName("rebase-merge/orig-head")
onto := g.getGitRefFileSymbolicName("rebase-merge/onto")
step := g.getGitFileContents("rebase-merge/msgnum")
total := g.getGitFileContents("rebase-merge/end")
icon := g.props.getString(RebaseIcon, "REBASE:")
return fmt.Sprintf("%s%s onto %s (%s/%s) at %s", icon, origin, onto, step, total, commit)
return fmt.Sprintf("%s%s%s onto %s%s (%s/%s) at %s", icon, branchIcon, origin, branchIcon, onto, step, total, ref)
}
if g.env.hasFolder(".git/rebase-apply") {
head := g.getGitFileContents("rebase-apply/head-name")
origin := strings.Replace(head, "refs/heads/", "", 1)
step := g.getGitFileContents("rebase-apply/next")
total := g.getGitFileContents("rebase-apply/last")
icon := g.props.getString(RebaseIcon, "REBASING:")
return fmt.Sprintf("%s%s (%s/%s) at %s", icon, origin, step, total, commit)
return fmt.Sprintf("%s%s%s (%s/%s) at %s", icon, branchIcon, origin, step, total, ref)
}
// cherry-pick
if g.env.hasFiles(".git/CHERRY_PICK_HEAD") {
sha := g.getGitRefFileSymbolicName("CHERRY_PICK_HEAD")
icon := g.props.getString(CherryPickIcon, "CHERRY PICK:")
return fmt.Sprintf("%s%s onto %s", icon, sha, ref)
}
return ref
}

func (g *git) getPrettyHEADName() string {
// check for tag
ref := g.getGitCommandOutput("describe", "--tags", "--exact-match")
if ref != "" {
return fmt.Sprintf("%s%s", g.props.getString(TagIcon, "TAG:"), ref)
}
icon := g.props.getString(RebaseIcon, "REBASE:")
return fmt.Sprintf("%sUNKNOWN", icon)
// fallback to commit
ref = g.getGitCommandOutput("rev-parse", "--short", "HEAD")
return fmt.Sprintf("%s%s", g.props.getString(CommitIcon, "COMMIT:"), ref)
}

func (g *git) getGitFileContents(file string) string {
Expand Down
28 changes: 13 additions & 15 deletions segment_git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ func setupHEADContextEnv(context *detachedContext) environmentInfo {
env.On("getFileContent", ".git/CHERRY_PICK_HEAD").Return(context.cherryPickSHA)
env.On("hasFiles", ".git/CHERRY_PICK_HEAD").Return(context.cherryPick)
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "rev-parse", "--short", "HEAD"}).Return(context.currentCommit)
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "rebase", "--show-current-patch"}).Return(context.rebase)
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "symbolic-ref", "-q", "--short", "HEAD"}).Return(context.branchName)
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "describe", "--tags", "--exact-match"}).Return(context.tagName)
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "name-rev", "--name-only", "--exclude=tags/*", context.origin}).Return(context.origin)
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "name-rev", "--name-only", "--exclude=tags/*", context.onto}).Return(context.onto)
Expand All @@ -78,15 +76,15 @@ func setupHEADContextEnv(context *detachedContext) environmentInfo {
}

func TestGetGitDetachedCommitHash(t *testing.T) {
want := "DETACHED:lalasha1"
want := "COMMIT:lalasha1"
context := &detachedContext{
currentCommit: "lalasha1",
}
env := setupHEADContextEnv(context)
g := &git{
env: env,
}
got := g.getGitHEADContext()
got := g.getGitHEADContext("")
assert.Equal(t, want, got)
}

Expand All @@ -100,12 +98,12 @@ func TestGetGitHEADContextTagName(t *testing.T) {
g := &git{
env: env,
}
got := g.getGitHEADContext()
got := g.getGitHEADContext("")
assert.Equal(t, want, got)
}

func TestGetGitHEADContextRebaseMerge(t *testing.T) {
want := "REBASE:cool-feature-bro onto main (2/3) at whatever"
want := "REBASE:BRANCH:cool-feature-bro onto BRANCH:main (2/3) at COMMIT:whatever"
context := &detachedContext{
currentCommit: "whatever",
rebase: "true",
Expand All @@ -119,12 +117,12 @@ func TestGetGitHEADContextRebaseMerge(t *testing.T) {
g := &git{
env: env,
}
got := g.getGitHEADContext()
got := g.getGitHEADContext("")
assert.Equal(t, want, got)
}

func TestGetGitHEADContextRebaseApply(t *testing.T) {
want := "REBASING:cool-feature-bro (2/3) at whatever"
want := "REBASING:BRANCH:cool-feature-bro (2/3) at COMMIT:whatever"
context := &detachedContext{
currentCommit: "whatever",
rebase: "true",
Expand All @@ -137,12 +135,12 @@ func TestGetGitHEADContextRebaseApply(t *testing.T) {
g := &git{
env: env,
}
got := g.getGitHEADContext()
got := g.getGitHEADContext("")
assert.Equal(t, want, got)
}

func TestGetGitHEADContextRebaseUnknown(t *testing.T) {
want := "REBASE:UNKNOWN"
want := "COMMIT:whatever"
context := &detachedContext{
currentCommit: "whatever",
rebase: "true",
Expand All @@ -151,12 +149,12 @@ func TestGetGitHEADContextRebaseUnknown(t *testing.T) {
g := &git{
env: env,
}
got := g.getGitHEADContext()
got := g.getGitHEADContext("")
assert.Equal(t, want, got)
}

func TestGetGitHEADContextCherryPickOnBranch(t *testing.T) {
want := "CHERRY PICK:pickme onto main"
want := "CHERRY PICK:pickme onto BRANCH:main"
context := &detachedContext{
currentCommit: "whatever",
branchName: "main",
Expand All @@ -167,12 +165,12 @@ func TestGetGitHEADContextCherryPickOnBranch(t *testing.T) {
g := &git{
env: env,
}
got := g.getGitHEADContext()
got := g.getGitHEADContext("main")
assert.Equal(t, want, got)
}

func TestGetGitHEADContextCherryPickOnTag(t *testing.T) {
want := "CHERRY PICK:pickme onto v3.4.6"
want := "CHERRY PICK:pickme onto TAG:v3.4.6"
context := &detachedContext{
currentCommit: "whatever",
tagName: "v3.4.6",
Expand All @@ -183,7 +181,7 @@ func TestGetGitHEADContextCherryPickOnTag(t *testing.T) {
g := &git{
env: env,
}
got := g.getGitHEADContext()
got := g.getGitHEADContext("")
assert.Equal(t, want, got)
}

Expand Down

0 comments on commit 09d4c95

Please sign in to comment.