feat(git): set untracked files mode

resolves #2117
This commit is contained in:
Jan De Dobbeleer 2022-04-22 08:11:20 +02:00 committed by Jan De Dobbeleer
parent 09a4489216
commit bcf7d59e83
4 changed files with 68 additions and 3 deletions

View file

@ -46,7 +46,10 @@ An alternative is to use the [Posh-Git segment][poshgit]
"properties": {
"fetch_status": true,
"fetch_stash_count": true,
"fetch_upstream_icon": true
"fetch_upstream_icon": true,
"untracked_modes": {
"/Users/user/Projects/oh-my-posh/": "no"
}
}
}
```
@ -62,6 +65,8 @@ You can set the following properties to `true` to enable fetching additional inf
- fetch_stash_count: `boolean` fetch stash count - defaults to `false`
- fetch_worktree_count: `boolean` fetch worktree count - defaults to `false`
- fetch_upstream_icon: `boolean` - fetch upstream icon - defaults to `false`
- untracked_modes: `map[string]string` - map of repo's where to override the default untracked mode (`no` | `normal` | `all`). For example
`"untracked_modes": { "/Users/me/repos/repo1": "no" }` - defaults to `normal` for all repo's
### Icons

View file

@ -103,6 +103,8 @@ const (
GitlabIcon properties.Property = "gitlab_icon"
// GitIcon shows when the upstream can't be identified
GitIcon properties.Property = "git_icon"
// UntrackedModes list the optional untracked files mode per repo
UntrackedModes properties.Property = "untracked_modes"
DETACHED = "(detached)"
BRANCHPREFIX = "ref: refs/heads/"
@ -269,7 +271,8 @@ func (g *Git) setGitStatus() {
g.UpstreamGone = true
g.Working = &GitStatus{}
g.Staging = &GitStatus{}
output := g.getGitCommandOutput("status", "-unormal", "--branch", "--porcelain=2")
untrackedMode := g.getUntrackedFilesMode()
output := g.getGitCommandOutput("status", untrackedMode, "--branch", "--porcelain=2")
for _, line := range strings.Split(output, "\n") {
if strings.HasPrefix(line, HASH) && len(line) >= len(HASH)+7 {
g.Hash = line[len(HASH) : len(HASH)+7]
@ -542,3 +545,12 @@ func (g *Git) convertToLinuxPath(path string) string {
}
return g.env.ConvertToLinuxPath(path)
}
func (g *Git) getUntrackedFilesMode() string {
mode := "normal"
repoModes := g.props.GetKeyValueMap(UntrackedModes, map[string]string{})
if val := repoModes[g.gitRealFolder]; len(val) != 0 {
mode = val
}
return fmt.Sprintf("-u%s", mode)
}

View file

@ -465,7 +465,8 @@ func TestSetGitStatus(t *testing.T) {
env.MockGitCommand("", strings.ReplaceAll(tc.Output, "\t", ""), "status", "-unormal", "--branch", "--porcelain=2")
g := &Git{
scm: scm{
env: env,
env: env,
props: properties.Map{},
},
}
if tc.ExpectedWorking == nil {
@ -761,3 +762,44 @@ func TestGitTemplateString(t *testing.T) {
assert.Equal(t, tc.Expected, renderTemplate(env, tc.Template, tc.Git), tc.Case)
}
}
func TestGitUntrackedMode(t *testing.T) {
cases := []struct {
Case string
Expected string
UntrackedModes map[string]string
}{
{
Case: "Default mode - no map",
Expected: "-unormal",
},
{
Case: "Default mode - no match",
Expected: "-unormal",
UntrackedModes: map[string]string{
"bar": "no",
},
},
{
Case: "No mode - match",
Expected: "-uno",
UntrackedModes: map[string]string{
"foo": "no",
"bar": "normal",
},
},
}
for _, tc := range cases {
g := &Git{
scm: scm{
props: properties.Map{
UntrackedModes: tc.UntrackedModes,
},
},
gitRealFolder: "foo",
}
got := g.getUntrackedFilesMode()
assert.Equal(t, tc.Expected, got, tc.Case)
}
}

View file

@ -615,6 +615,12 @@
"title": "Branch max length",
"description": "the max length for the displayed branch name where 0 implies full length",
"default": 0
},
"untracked_modes": {
"type": "object",
"title": "Untracked files mode",
"description": "Set the untracked files mode for a repository",
"default": {}
}
}
}