Skip to content

Commit

Permalink
feat(git): expose .Rebase as an object
Browse files Browse the repository at this point in the history
resolves #6044
  • Loading branch information
JanDeDobbeleer committed Dec 22, 2024
1 parent bba8c04 commit 3b40f82
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 23 deletions.
59 changes: 42 additions & 17 deletions src/segments/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
7 changes: 6 additions & 1 deletion src/segments/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
19 changes: 14 additions & 5 deletions website/docs/segments/scm/git.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
| ------------ | --------- | -------------------------------------------- |
Expand All @@ -166,7 +166,7 @@ Local changes use the following syntax:
| `~` | Modified |
| `?` | Untracked |

### Commit
#### Commit

| Name | Type | Description |
| ------------ | ----------- | --------------------------------------- |
Expand All @@ -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 |
| ---------- | ---------- | ----------------- |
Expand All @@ -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
Expand Down

0 comments on commit 3b40f82

Please # to comment.