From 3b40f825c3e102ac14271b9b253b938b22f41758 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sun, 22 Dec 2024 16:47:05 +0100 Subject: [PATCH] feat(git): expose .Rebase as an object resolves #6044 --- src/segments/git.go | 59 ++++++++++++++++++++++--------- src/segments/git_test.go | 7 +++- website/docs/segments/scm/git.mdx | 19 +++++++--- 3 files changed, 62 insertions(+), 23 deletions(-) diff --git a/src/segments/git.go b/src/segments/git.go index bf9aca96..0d80abd3 100644 --- a/src/segments/git.go +++ b/src/segments/git.go @@ -129,28 +129,35 @@ const ( trueStr = "true" ) +type Rebase struct { + HEAD string + Onto string + Current int + Total int +} + type Git struct { User *User Working *GitStatus Staging *GitStatus commit *Commit - UpstreamURL string - UpstreamIcon string + Rebase *Rebase + RawUpstreamURL string Ref string Hash string ShortHash string BranchStatus string Upstream string HEAD string - RawUpstreamURL string + UpstreamIcon string + UpstreamURL string scm - Ahead int - stashCount int worktreeCount int + stashCount int Behind int + Ahead int IsWorkTree bool Merge bool - Rebase bool CherryPick bool Revert bool poshgit bool @@ -587,7 +594,7 @@ func (g *Git) setGitStatus() { } // map conflicts separately when in a merge or rebase - if g.Rebase || g.Merge { + if g.Rebase != nil || g.Merge { conflict := "AA" full := status[2:4] if full == conflict { @@ -698,25 +705,43 @@ func (g *Git) setGitHEADContext() { return origin } + parseInt := func(file string) int { + val, _ := strconv.Atoi(g.FileContents(g.workingDir, file)) + return val + } + if g.env.HasFolder(g.workingDir + "/rebase-merge") { - g.Rebase = true - origin := getPrettyNameOrigin("rebase-merge/head-name") + head := getPrettyNameOrigin("rebase-merge/head-name") onto := g.getGitRefFileSymbolicName("rebase-merge/onto") onto = g.formatBranch(onto) - step := g.FileContents(g.workingDir, "rebase-merge/msgnum") - total := g.FileContents(g.workingDir, "rebase-merge/end") + current := parseInt("rebase-merge/msgnum") + total := parseInt("rebase-merge/end") icon := g.props.GetString(RebaseIcon, "\uE728 ") - g.HEAD = fmt.Sprintf("%s%s onto %s%s (%s/%s) at %s", icon, origin, branchIcon, onto, step, total, g.HEAD) + + g.Rebase = &Rebase{ + HEAD: head, + Onto: onto, + Current: current, + Total: total, + } + + g.HEAD = fmt.Sprintf("%s%s onto %s%s (%d/%d) at %s", icon, head, branchIcon, onto, current, total, g.HEAD) return } if g.env.HasFolder(g.workingDir + "/rebase-apply") { - g.Rebase = true - origin := getPrettyNameOrigin("rebase-apply/head-name") - step := g.FileContents(g.workingDir, "rebase-apply/next") - total := g.FileContents(g.workingDir, "rebase-apply/last") + head := getPrettyNameOrigin("rebase-apply/head-name") + current := parseInt("rebase-apply/next") + total := parseInt("rebase-apply/last") icon := g.props.GetString(RebaseIcon, "\uE728 ") - g.HEAD = fmt.Sprintf("%s%s (%s/%s) at %s", icon, origin, step, total, g.HEAD) + + g.Rebase = &Rebase{ + HEAD: head, + Current: current, + Total: total, + } + + g.HEAD = fmt.Sprintf("%s%s (%d/%d) at %s", icon, head, current, total, g.HEAD) return } diff --git a/src/segments/git_test.go b/src/segments/git_test.go index 1501c46c..f5df399a 100644 --- a/src/segments/git_test.go +++ b/src/segments/git_test.go @@ -590,10 +590,15 @@ func TestSetGitStatus(t *testing.T) { if tc.ExpectedWorking == nil { tc.ExpectedWorking = &GitStatus{} } + if tc.ExpectedStaging == nil { tc.ExpectedStaging = &GitStatus{} } - g.Rebase = tc.Rebase + + if tc.Rebase { + g.Rebase = &Rebase{} + } + g.Merge = tc.Merge tc.ExpectedStaging.Formats = map[string]string{} tc.ExpectedWorking.Formats = map[string]string{} diff --git a/website/docs/segments/scm/git.mdx b/website/docs/segments/scm/git.mdx index 25789d3a..70c793bd 100644 --- a/website/docs/segments/scm/git.mdx +++ b/website/docs/segments/scm/git.mdx @@ -139,12 +139,12 @@ You can set the following properties to `true` to enable fetching additional inf | `.Commit` | `Commit` | HEAD commit information (see below) | | `.Detached` | `boolean` | true when the head is detached | | `.Merge` | `boolean` | true when in a merge | -| `.Rebase` | `boolean` | true when in a rebase | +| `.Rebase` | `Rebase` | contains the relevant information when in a rebase | | `.CherryPick` | `boolean` | true when in a cherry pick | | `.Revert` | `boolean` | true when in a revert | | `.LatestTag` | `string` | the latest tag name | -### Status +#### Status | Name | Type | Description | | ------------ | --------- | -------------------------------------------- | @@ -166,7 +166,7 @@ Local changes use the following syntax: | `~` | Modified | | `?` | Untracked | -### Commit +#### Commit | Name | Type | Description | | ------------ | ----------- | --------------------------------------- | @@ -177,14 +177,14 @@ Local changes use the following syntax: | `.Sha` | `string` | the commit SHA1 | | `.Refs` | `Refs` | the commit references | -### User +##### User | Name | Type | Description | | -------- | -------- | ---------------- | | `.Name` | `string` | the user's name | | `.Email` | `string` | the user's email | -### Refs +##### Refs | Name | Type | Description | | ---------- | ---------- | ----------------- | @@ -198,6 +198,15 @@ As these are arrays of strings, you can join them using the `join` function: {{ join ", " .Commit.Refs.Tags }} ``` +#### Rebase + +| Name | Type | Description | +| ---------- | -------- | -------------------------------- | +| `.Current` | `int` | the current rebase step | +| `.Total` | `int` | the total number of rebase steps | +| `.HEAD` | `string` | the current HEAD | +| `.Onto` | `string` | the branch we're rebasing onto | + ## posh-git If you want to display the default [posh-git][poshgit] output, **do not** use this segment