From dd68da8793dc49af1ee2c784e68f5b254728a122 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Tue, 6 Jun 2023 07:58:11 +0200 Subject: [PATCH] feat(git): fetch user information resolves #3933 --- src/segments/git.go | 14 ++++++++++ themes/schema.json | 6 +++++ website/docs/segments/git.mdx | 50 +++++++++++++++++++---------------- 3 files changed, 47 insertions(+), 23 deletions(-) diff --git a/src/segments/git.go b/src/segments/git.go index 48c9307c..8f4faa6c 100644 --- a/src/segments/git.go +++ b/src/segments/git.go @@ -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") diff --git a/themes/schema.json b/themes/schema.json index 98875bae..99b1af8d 100644 --- a/themes/schema.json +++ b/themes/schema.json @@ -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 } } } diff --git a/website/docs/segments/git.mdx b/website/docs/segments/git.mdx index 74faf9f5..1770c03f 100644 --- a/website/docs/segments/git.mdx +++ b/website/docs/segments/git.mdx @@ -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"; - + ## 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]:
  • `no`
  • `normal`
  • `all`
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