refactor(git): add booleans to identify state

This commit is contained in:
Jan De Dobbeleer 2023-09-04 16:33:50 +02:00 committed by Jan De Dobbeleer
parent aa4230fb3f
commit 3d53650b22
2 changed files with 26 additions and 1 deletions

View file

@ -135,6 +135,11 @@ type Git struct {
IsWorkTree bool
IsBare bool
User *User
Detached bool
Merge bool
Rebase bool
CherryPick bool
Revert bool
// needed for posh-git support
poshgit bool
@ -555,6 +560,7 @@ func (g *Git) getGitCommandOutput(args ...string) string {
func (g *Git) setGitHEADContext() {
branchIcon := g.props.GetString(BranchIcon, "\uE0A0")
if g.Ref == DETACHED {
g.Detached = true
g.setPrettyHEADName()
} else {
head := g.formatHEAD(g.Ref)
@ -581,6 +587,7 @@ func (g *Git) setGitHEADContext() {
}
if g.env.HasFolder(g.workingDir + "/rebase-merge") {
g.Rebase = true
origin := getPrettyNameOrigin("rebase-merge/head-name")
onto := g.getGitRefFileSymbolicName("rebase-merge/onto")
onto = g.formatHEAD(onto)
@ -590,7 +597,9 @@ func (g *Git) setGitHEADContext() {
g.HEAD = fmt.Sprintf("%s%s onto %s%s (%s/%s) at %s", icon, origin, branchIcon, onto, step, 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")
@ -598,9 +607,12 @@ func (g *Git) setGitHEADContext() {
g.HEAD = fmt.Sprintf("%s%s (%s/%s) at %s", icon, origin, step, total, g.HEAD)
return
}
// merge
commitIcon := g.props.GetString(CommitIcon, "\uF417")
if g.hasGitFile("MERGE_MSG") {
g.Merge = true
icon := g.props.GetString(MergeIcon, "\uE727 ")
mergeContext := g.FileContents(g.workingDir, "MERGE_MSG")
matches := regex.FindNamedRegexMatch(`Merge (remote-tracking )?(?P<type>branch|commit|tag) '(?P<theirs>.*)'`, mergeContext)
@ -622,23 +634,28 @@ func (g *Git) setGitHEADContext() {
return
}
}
// sequencer status
// see if a cherry-pick or revert is in progress, if the user has committed a
// conflict resolution with 'git commit' in the middle of a sequence of picks or
// reverts then CHERRY_PICK_HEAD/REVERT_HEAD will not exist so we have to read
// the todo file.
if g.hasGitFile("CHERRY_PICK_HEAD") {
g.CherryPick = true
sha := g.FileContents(g.workingDir, "CHERRY_PICK_HEAD")
cherry := g.props.GetString(CherryPickIcon, "\uE29B ")
g.HEAD = fmt.Sprintf("%s%s%s onto %s", cherry, commitIcon, g.formatSHA(sha), formatDetached())
return
}
if g.hasGitFile("REVERT_HEAD") {
g.Revert = true
sha := g.FileContents(g.workingDir, "REVERT_HEAD")
revert := g.props.GetString(RevertIcon, "\uF0E2 ")
g.HEAD = fmt.Sprintf("%s%s%s onto %s", revert, commitIcon, g.formatSHA(sha), formatDetached())
return
}
if g.hasGitFile("sequencer/todo") {
todo := g.FileContents(g.workingDir, "sequencer/todo")
matches := regex.FindNamedRegexMatch(`^(?P<action>p|pick|revert)\s+(?P<sha>\S+)`, todo)
@ -647,16 +664,19 @@ func (g *Git) setGitHEADContext() {
sha := matches["sha"]
switch action {
case "p", "pick":
g.CherryPick = true
cherry := g.props.GetString(CherryPickIcon, "\uE29B ")
g.HEAD = fmt.Sprintf("%s%s%s onto %s", cherry, commitIcon, g.formatSHA(sha), formatDetached())
return
case "revert":
g.Revert = true
revert := g.props.GetString(RevertIcon, "\uF0E2 ")
g.HEAD = fmt.Sprintf("%s%s%s onto %s", revert, commitIcon, g.formatSHA(sha), formatDetached())
return
}
}
}
g.HEAD = formatDetached()
}

View file

@ -116,7 +116,7 @@ You can set the following properties to `true` to enable fetching additional inf
| `gitlab_icon` | `string` | icon/text to display when the upstream is Gitlab - defaults to `\uF296 ` |
| `bitbucket_icon` | `string` | icon/text to display when the upstream is Bitbucket - defaults to `\uF171 ` |
| `azure_devops_icon` | `string` | icon/text to display when the upstream is Azure DevOps - defaults to `\uEBE8 ` |
| `codecommit_icon` | `string` | icon/text to display when the upstream is AWS CodeCommit - defaults to `\uF270 ` |
| `codecommit_icon` | `string` | icon/text to display when the upstream is AWS CodeCommit - defaults to `\uF270 ` |
| `git_icon` | `string` | icon/text to display when the upstream is not known/mapped - defaults to `\uE5FB ` |
| `upstream_icons` | `map[string]string` | a key, value map representing the remote URL (or a part of that URL) and icon to use in case the upstream URL contains the key. These get precedence over the standard icons |
@ -153,6 +153,11 @@ You can set the following properties to `true` to enable fetching additional inf
| `.Dir` | `string` | the repository's root directory |
| `.Kraken` | `string` | a link to the current HEAD in [GitKraken][kraken-ref] for use in [hyperlinks][hyperlinks] in templates `{{ url .HEAD .Kraken }}` |
| `.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 |
| `.CherryPick` | `boolean` | true when in a cherry pick |
| `.Revert` | `boolean` | true when in a revert |
### Status