feat(git): fetch user information

resolves #3933
This commit is contained in:
Jan De Dobbeleer 2023-06-06 07:58:11 +02:00 committed by Jan De Dobbeleer
parent 50fc40a5fe
commit dd68da8793
3 changed files with 47 additions and 23 deletions

View file

@ -63,6 +63,8 @@ const (
FetchUpstreamIcon properties.Property = "fetch_upstream_icon"
// FetchBareInfo fetches the bare repo status
FetchBareInfo properties.Property = "fetch_bare_info"
// FetchUser fetches the current user for the repo
FetchUser properties.Property = "fetch_user"
// BranchIcon the icon to use as branch indicator
BranchIcon properties.Property = "branch_icon"
@ -129,6 +131,7 @@ type Git struct {
UpstreamGone bool
IsWorkTree bool
IsBare bool
User *User
// needed for posh-git support
poshgit bool
@ -145,11 +148,17 @@ func (g *Git) Template() string {
func (g *Git) Enabled() bool {
g.Working = &GitStatus{}
g.Staging = &GitStatus{}
g.User = &User{}
if !g.shouldDisplay() {
return false
}
fetchUser := g.props.GetBool(FetchUser, false)
if fetchUser {
g.setUser()
}
g.RepoName = platform.Base(g.env, g.convertToLinuxPath(g.realDir))
if g.IsBare {
@ -281,6 +290,11 @@ func (g *Git) shouldDisplay() bool {
return true
}
func (g *Git) setUser() {
g.User.Name = g.getGitCommandOutput("config", "user.name")
g.User.Email = g.getGitCommandOutput("config", "user.email")
}
func (g *Git) getBareRepoInfo() {
head := g.FileContents(g.workingDir, "HEAD")
branchIcon := g.props.GetString(BranchIcon, "\uE0A0")

View file

@ -1007,6 +1007,12 @@
"items": {
"type": "string"
}
},
"fetch_user": {
"type": "boolean",
"title": "Fetch the user",
"description": "Fetch the current configured user for the repository",
"default": false
}
}
}

View file

@ -35,30 +35,33 @@ You can then use the `POSH_GIT_STRING` environment variable in a [text segment][
## Sample Configuration
import Config from '@site/src/components/Config.js';
import Config from "@site/src/components/Config.js";
<Config data={{
"type": "git",
"style": "powerline",
"powerline_symbol": "\uE0B0",
"foreground": "#193549",
"background": "#ffeb3b",
"background_templates": [
"{{ if or (.Working.Changed) (.Staging.Changed) }}#FFEB3B{{ end }}",
"{{ if and (gt .Ahead 0) (gt .Behind 0) }}#FFCC80{{ end }}",
"{{ if gt .Ahead 0 }}#B388FF{{ end }}",
"{{ if gt .Behind 0 }}#B388FB{{ end }}"
],
"template": "{{ .UpstreamIcon }}{{ .HEAD }}{{if .BranchStatus }} {{ .BranchStatus }}{{ end }}{{ if .Working.Changed }} \uF044 {{ .Working.String }}{{ end }}{{ if and (.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .Staging.Changed }} \uF046 {{ .Staging.String }}{{ end }}{{ if gt .StashCount 0 }} \uF692 {{ .StashCount }}{{ end }}",
"properties": {
"fetch_status": true,
"fetch_stash_count": true,
"fetch_upstream_icon": true,
"untracked_modes": {
"/Users/user/Projects/oh-my-posh/": "no"
}
}
}}/>
<Config
data={{
type: "git",
style: "powerline",
powerline_symbol: "\uE0B0",
foreground: "#193549",
background: "#ffeb3b",
background_templates: [
"{{ if or (.Working.Changed) (.Staging.Changed) }}#FFEB3B{{ end }}",
"{{ if and (gt .Ahead 0) (gt .Behind 0) }}#FFCC80{{ end }}",
"{{ if gt .Ahead 0 }}#B388FF{{ end }}",
"{{ if gt .Behind 0 }}#B388FB{{ end }}",
],
template:
"{{ .UpstreamIcon }}{{ .HEAD }}{{if .BranchStatus }} {{ .BranchStatus }}{{ end }}{{ if .Working.Changed }} \uF044 {{ .Working.String }}{{ end }}{{ if and (.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .Staging.Changed }} \uF046 {{ .Staging.String }}{{ end }}{{ if gt .StashCount 0 }} \uF692 {{ .StashCount }}{{ end }}",
properties: {
fetch_status: true,
fetch_stash_count: true,
fetch_upstream_icon: true,
untracked_modes: {
"/Users/user/Projects/oh-my-posh/": "no",
},
},
}}
/>
## Properties
@ -76,6 +79,7 @@ You can set the following properties to `true` to enable fetching additional inf
| `untracked_modes` | `map[string]string` | map of repo's where to override the default [untracked files mode][untracked]:<ul><li>`no`</li><li>`normal`</li><li>`all`</li></ul>For example `"untracked_modes": { "/Users/me/repos/repo1": "no" }` - defaults to `normal` for all repo's. If you want to override for all repo's, use `*` to set the mode instead of the repo path |
| `ignore_submodules` | `map[string]string` | map of repo's where to change the [--ignore-submodules][submodules] flag (`none`, `untracked`, `dirty` or `all`). For example `"ignore_submodules": { "/Users/me/repos/repo1": "all" }`. If you want to override for all repo's, use `*` to set the mode instead of the repo path |
| `native_fallback` | `boolean` | when set to `true` and `git.exe` is not available when inside a WSL2 shared Windows drive, we will fallback to the native git executable to fetch data. Not all information can be displayed in this case. Defaults to `false` |
| `fetch_user` | [`User`](#user) | fetch the current configured user for the repository - defaults to `false` |
### Icons