feat(git): expose directory

This commit is contained in:
Jan De Dobbeleer 2022-08-28 15:34:08 +02:00 committed by Jan De Dobbeleer
parent c0935c87f2
commit 07eba99d6e
7 changed files with 73 additions and 70 deletions

View file

@ -726,10 +726,8 @@ func (env *ShellEnvironment) TemplateCache() *TemplateCache {
val := splitted[1:] val := splitted[1:]
tmplCache.Env[key] = strings.Join(val, separator) tmplCache.Env[key] = strings.Join(val, separator)
} }
pwd := env.Pwd() tmplCache.PWD = env.Pwd()
pwd = strings.Replace(pwd, env.Home(), "~", 1) tmplCache.Folder = Base(env, tmplCache.PWD)
tmplCache.PWD = pwd
tmplCache.Folder = Base(env, pwd)
tmplCache.UserName = env.User() tmplCache.UserName = env.User()
if host, err := env.Host(); err == nil { if host, err := env.Host(); err == nil {
tmplCache.HostName = host tmplCache.HostName = host

View file

@ -117,7 +117,7 @@ func (g *Git) Enabled() bool {
if !g.shouldDisplay() { if !g.shouldDisplay() {
return false return false
} }
g.RepoName = environment.Base(g.env, g.realFolder) g.RepoName = environment.Base(g.env, g.realDir)
displayStatus := g.props.GetBool(FetchStatus, false) displayStatus := g.props.GetBool(FetchStatus, false)
if displayStatus { if displayStatus {
g.setGitStatus() g.setGitStatus()
@ -159,19 +159,21 @@ func (g *Git) shouldDisplay() bool {
return false return false
} }
// convert the worktree file path to a windows one when in wsl 2 shared folder
g.Dir = strings.TrimSuffix(g.convertToWindowsPath(gitdir.Path), ".git")
if !gitdir.IsDir { if !gitdir.IsDir {
return g.hasWorktree(gitdir) return g.hasWorktree(gitdir)
} }
g.workingFolder = gitdir.Path g.workingDir = gitdir.Path
g.rootFolder = gitdir.Path g.rootDir = gitdir.Path
// convert the worktree file path to a windows one when in wsl 2 shared folder g.realDir = g.Dir
g.realFolder = strings.TrimSuffix(g.convertToWindowsPath(gitdir.Path), ".git")
return true return true
} }
func (g *Git) hasWorktree(gitdir *environment.FileInfo) bool { func (g *Git) hasWorktree(gitdir *environment.FileInfo) bool {
g.rootFolder = gitdir.Path g.rootDir = gitdir.Path
dirPointer := strings.Trim(g.env.FileContent(gitdir.Path), " \r\n") dirPointer := strings.Trim(g.env.FileContent(gitdir.Path), " \r\n")
matches := regex.FindNamedRegexMatch(`^gitdir: (?P<dir>.*)$`, dirPointer) matches := regex.FindNamedRegexMatch(`^gitdir: (?P<dir>.*)$`, dirPointer)
if matches == nil || matches["dir"] == "" { if matches == nil || matches["dir"] == "" {
@ -179,49 +181,49 @@ func (g *Git) hasWorktree(gitdir *environment.FileInfo) bool {
} }
// if we open a worktree file in a shared wsl2 folder, we have to convert it back // if we open a worktree file in a shared wsl2 folder, we have to convert it back
// to the mounted path // to the mounted path
g.workingFolder = g.convertToLinuxPath(matches["dir"]) g.workingDir = g.convertToLinuxPath(matches["dir"])
// in worktrees, the path looks like this: gitdir: path/.git/worktrees/branch // in worktrees, the path looks like this: gitdir: path/.git/worktrees/branch
// strips the last .git/worktrees part // strips the last .git/worktrees part
// :ind+5 = index + /.git // :ind+5 = index + /.git
ind := strings.LastIndex(g.workingFolder, "/.git/worktrees") ind := strings.LastIndex(g.workingDir, "/.git/worktrees")
if ind > -1 { if ind > -1 {
gitDir := filepath.Join(g.workingFolder, "gitdir") gitDir := filepath.Join(g.workingDir, "gitdir")
g.rootFolder = g.workingFolder[:ind+5] g.rootDir = g.workingDir[:ind+5]
g.realFolder = strings.TrimSuffix(g.env.FileContent(gitDir), ".git\n") g.realDir = strings.TrimSuffix(g.env.FileContent(gitDir), ".git\n")
g.IsWorkTree = true g.IsWorkTree = true
return true return true
} }
// in submodules, the path looks like this: gitdir: ../.git/modules/test-submodule // in submodules, the path looks like this: gitdir: ../.git/modules/test-submodule
// we need the parent folder to detect where the real .git folder is // we need the parent folder to detect where the real .git folder is
ind = strings.LastIndex(g.workingFolder, "/.git/modules") ind = strings.LastIndex(g.workingDir, "/.git/modules")
if ind > -1 { if ind > -1 {
g.rootFolder = resolveGitPath(gitdir.ParentFolder, g.workingFolder) g.rootDir = resolveGitPath(gitdir.ParentFolder, g.workingDir)
// this might be both a worktree and a submodule, where the path would look like // this might be both a worktree and a submodule, where the path would look like
// this: path/.git/modules/module/path/worktrees/location. We cannot distinguish // this: path/.git/modules/module/path/worktrees/location. We cannot distinguish
// between worktree and a module path containing the word 'worktree,' however. // between worktree and a module path containing the word 'worktree,' however.
ind = strings.LastIndex(g.rootFolder, "/worktrees/") ind = strings.LastIndex(g.rootDir, "/worktrees/")
if ind > -1 && g.env.HasFilesInDir(g.rootFolder, "gitdir") { if ind > -1 && g.env.HasFilesInDir(g.rootDir, "gitdir") {
gitDir := filepath.Join(g.rootFolder, "gitdir") gitDir := filepath.Join(g.rootDir, "gitdir")
realGitFolder := g.env.FileContent(gitDir) realGitFolder := g.env.FileContent(gitDir)
g.realFolder = strings.TrimSuffix(realGitFolder, ".git\n") g.realDir = strings.TrimSuffix(realGitFolder, ".git\n")
g.rootFolder = g.rootFolder[:ind] g.rootDir = g.rootDir[:ind]
g.workingFolder = g.rootFolder g.workingDir = g.rootDir
g.IsWorkTree = true g.IsWorkTree = true
return true return true
} }
g.realFolder = g.rootFolder g.realDir = g.rootDir
g.workingFolder = g.rootFolder g.workingDir = g.rootDir
return true return true
} }
// check for separate git folder(--separate-git-dir) // check for separate git folder(--separate-git-dir)
// check if the folder contains a HEAD file // check if the folder contains a HEAD file
if g.env.HasFilesInDir(g.workingFolder, "HEAD") { if g.env.HasFilesInDir(g.workingDir, "HEAD") {
gitFolder := strings.TrimSuffix(g.rootFolder, ".git") gitFolder := strings.TrimSuffix(g.rootDir, ".git")
g.rootFolder = g.workingFolder g.rootDir = g.workingDir
g.workingFolder = gitFolder g.workingDir = gitFolder
g.realFolder = gitFolder g.realDir = gitFolder
return true return true
} }
return false return false
@ -331,7 +333,7 @@ func (g *Git) setGitStatus() {
} }
func (g *Git) getGitCommandOutput(args ...string) string { func (g *Git) getGitCommandOutput(args ...string) string {
args = append([]string{"-C", g.realFolder, "--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false"}, args...) args = append([]string{"-C", g.realDir, "--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false"}, args...)
val, err := g.env.RunCommand(g.getCommand(GITCOMMAND), args...) val, err := g.env.RunCommand(g.getCommand(GITCOMMAND), args...)
if err != nil { if err != nil {
return "" return ""
@ -357,7 +359,7 @@ func (g *Git) setGitHEADContext() {
getPrettyNameOrigin := func(file string) string { getPrettyNameOrigin := func(file string) string {
var origin string var origin string
head := g.FileContents(g.workingFolder, file) head := g.FileContents(g.workingDir, file)
if head == "detached HEAD" { if head == "detached HEAD" {
origin = formatDetached() origin = formatDetached()
} else { } else {
@ -367,20 +369,20 @@ func (g *Git) setGitHEADContext() {
return origin return origin
} }
if g.env.HasFolder(g.workingFolder + "/rebase-merge") { if g.env.HasFolder(g.workingDir + "/rebase-merge") {
origin := getPrettyNameOrigin("rebase-merge/head-name") origin := getPrettyNameOrigin("rebase-merge/head-name")
onto := g.getGitRefFileSymbolicName("rebase-merge/onto") onto := g.getGitRefFileSymbolicName("rebase-merge/onto")
onto = g.formatHEAD(onto) onto = g.formatHEAD(onto)
step := g.FileContents(g.workingFolder, "rebase-merge/msgnum") step := g.FileContents(g.workingDir, "rebase-merge/msgnum")
total := g.FileContents(g.workingFolder, "rebase-merge/end") total := g.FileContents(g.workingDir, "rebase-merge/end")
icon := g.props.GetString(RebaseIcon, "\uE728 ") 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.HEAD = fmt.Sprintf("%s%s onto %s%s (%s/%s) at %s", icon, origin, branchIcon, onto, step, total, g.HEAD)
return return
} }
if g.env.HasFolder(g.workingFolder + "/rebase-apply") { if g.env.HasFolder(g.workingDir + "/rebase-apply") {
origin := getPrettyNameOrigin("rebase-apply/head-name") origin := getPrettyNameOrigin("rebase-apply/head-name")
step := g.FileContents(g.workingFolder, "rebase-apply/next") step := g.FileContents(g.workingDir, "rebase-apply/next")
total := g.FileContents(g.workingFolder, "rebase-apply/last") total := g.FileContents(g.workingDir, "rebase-apply/last")
icon := g.props.GetString(RebaseIcon, "\uE728 ") icon := g.props.GetString(RebaseIcon, "\uE728 ")
g.HEAD = fmt.Sprintf("%s%s (%s/%s) at %s", icon, origin, step, total, g.HEAD) g.HEAD = fmt.Sprintf("%s%s (%s/%s) at %s", icon, origin, step, total, g.HEAD)
return return
@ -389,7 +391,7 @@ func (g *Git) setGitHEADContext() {
commitIcon := g.props.GetString(CommitIcon, "\uF417") commitIcon := g.props.GetString(CommitIcon, "\uF417")
if g.hasGitFile("MERGE_MSG") { if g.hasGitFile("MERGE_MSG") {
icon := g.props.GetString(MergeIcon, "\uE727 ") icon := g.props.GetString(MergeIcon, "\uE727 ")
mergeContext := g.FileContents(g.workingFolder, "MERGE_MSG") mergeContext := g.FileContents(g.workingDir, "MERGE_MSG")
matches := regex.FindNamedRegexMatch(`Merge (remote-tracking )?(?P<type>branch|commit|tag) '(?P<theirs>.*)'`, mergeContext) matches := regex.FindNamedRegexMatch(`Merge (remote-tracking )?(?P<type>branch|commit|tag) '(?P<theirs>.*)'`, mergeContext)
// head := g.getGitRefFileSymbolicName("ORIG_HEAD") // head := g.getGitRefFileSymbolicName("ORIG_HEAD")
if matches != nil && matches["theirs"] != "" { if matches != nil && matches["theirs"] != "" {
@ -415,19 +417,19 @@ func (g *Git) setGitHEADContext() {
// reverts then CHERRY_PICK_HEAD/REVERT_HEAD will not exist so we have to read // reverts then CHERRY_PICK_HEAD/REVERT_HEAD will not exist so we have to read
// the todo file. // the todo file.
if g.hasGitFile("CHERRY_PICK_HEAD") { if g.hasGitFile("CHERRY_PICK_HEAD") {
sha := g.FileContents(g.workingFolder, "CHERRY_PICK_HEAD") sha := g.FileContents(g.workingDir, "CHERRY_PICK_HEAD")
cherry := g.props.GetString(CherryPickIcon, "\uE29B ") cherry := g.props.GetString(CherryPickIcon, "\uE29B ")
g.HEAD = fmt.Sprintf("%s%s%s onto %s", cherry, commitIcon, g.formatSHA(sha), formatDetached()) g.HEAD = fmt.Sprintf("%s%s%s onto %s", cherry, commitIcon, g.formatSHA(sha), formatDetached())
return return
} }
if g.hasGitFile("REVERT_HEAD") { if g.hasGitFile("REVERT_HEAD") {
sha := g.FileContents(g.workingFolder, "REVERT_HEAD") sha := g.FileContents(g.workingDir, "REVERT_HEAD")
revert := g.props.GetString(RevertIcon, "\uF0E2 ") revert := g.props.GetString(RevertIcon, "\uF0E2 ")
g.HEAD = fmt.Sprintf("%s%s%s onto %s", revert, commitIcon, g.formatSHA(sha), formatDetached()) g.HEAD = fmt.Sprintf("%s%s%s onto %s", revert, commitIcon, g.formatSHA(sha), formatDetached())
return return
} }
if g.hasGitFile("sequencer/todo") { if g.hasGitFile("sequencer/todo") {
todo := g.FileContents(g.workingFolder, "sequencer/todo") todo := g.FileContents(g.workingDir, "sequencer/todo")
matches := regex.FindNamedRegexMatch(`^(?P<action>p|pick|revert)\s+(?P<sha>\S+)`, todo) matches := regex.FindNamedRegexMatch(`^(?P<action>p|pick|revert)\s+(?P<sha>\S+)`, todo)
if matches != nil && matches["sha"] != "" { if matches != nil && matches["sha"] != "" {
action := matches["action"] action := matches["action"]
@ -464,18 +466,18 @@ func (g *Git) formatSHA(sha string) string {
} }
func (g *Git) hasGitFile(file string) bool { func (g *Git) hasGitFile(file string) bool {
return g.env.HasFilesInDir(g.workingFolder, file) return g.env.HasFilesInDir(g.workingDir, file)
} }
func (g *Git) getGitRefFileSymbolicName(refFile string) string { func (g *Git) getGitRefFileSymbolicName(refFile string) string {
ref := g.FileContents(g.workingFolder, refFile) ref := g.FileContents(g.workingDir, refFile)
return g.getGitCommandOutput("name-rev", "--name-only", "--exclude=tags/*", ref) return g.getGitCommandOutput("name-rev", "--name-only", "--exclude=tags/*", ref)
} }
func (g *Git) setPrettyHEADName() { func (g *Git) setPrettyHEADName() {
// we didn't fetch status, fallback to parsing the HEAD file // we didn't fetch status, fallback to parsing the HEAD file
if len(g.Hash) == 0 { if len(g.Hash) == 0 {
HEADRef := g.FileContents(g.workingFolder, "HEAD") HEADRef := g.FileContents(g.workingDir, "HEAD")
if strings.HasPrefix(HEADRef, BRANCHPREFIX) { if strings.HasPrefix(HEADRef, BRANCHPREFIX) {
branchName := strings.TrimPrefix(HEADRef, BRANCHPREFIX) branchName := strings.TrimPrefix(HEADRef, BRANCHPREFIX)
g.HEAD = fmt.Sprintf("%s%s", g.props.GetString(BranchIcon, "\uE0A0"), g.formatHEAD(branchName)) g.HEAD = fmt.Sprintf("%s%s", g.props.GetString(BranchIcon, "\uE0A0"), g.formatHEAD(branchName))
@ -501,7 +503,7 @@ func (g *Git) setPrettyHEADName() {
} }
func (g *Git) getStashContext() int { func (g *Git) getStashContext() int {
stashContent := g.FileContents(g.rootFolder, "logs/refs/stash") stashContent := g.FileContents(g.rootDir, "logs/refs/stash")
if stashContent == "" { if stashContent == "" {
return 0 return 0
} }
@ -510,10 +512,10 @@ func (g *Git) getStashContext() int {
} }
func (g *Git) getWorktreeContext() int { func (g *Git) getWorktreeContext() int {
if !g.env.HasFolder(g.rootFolder + "/worktrees") { if !g.env.HasFolder(g.rootDir + "/worktrees") {
return 0 return 0
} }
worktreeFolders := g.env.LsDir(g.rootFolder + "/worktrees") worktreeFolders := g.env.LsDir(g.rootDir + "/worktrees")
var count int var count int
for _, folder := range worktreeFolders { for _, folder := range worktreeFolders {
if folder.IsDir() { if folder.IsDir() {
@ -535,7 +537,7 @@ func (g *Git) getOriginURL(upstream string) string {
return fmt.Sprintf("https://%s", url) return fmt.Sprintf("https://%s", url)
} }
var url string var url string
cfg, err := ini.Load(g.rootFolder + "/config") cfg, err := ini.Load(g.rootDir + "/config")
if err != nil { if err != nil {
url = g.getGitCommandOutput("remote", "get-url", upstream) url = g.getGitCommandOutput("remote", "get-url", upstream)
return cleanSSHURL(url) return cleanSSHURL(url)
@ -562,7 +564,7 @@ func (g *Git) getSwitchMode(property properties.Property, gitSwitch, mode string
mode = val mode = val
} }
// get the specific repo mode // get the specific repo mode
if val := repoModes[g.realFolder]; len(val) != 0 { if val := repoModes[g.realDir]; len(val) != 0 {
mode = val mode = val
} }
if len(mode) == 0 { if len(mode) == 0 {

View file

@ -54,7 +54,7 @@ func TestEnabledInWorkingDirectory(t *testing.T) {
}, },
} }
assert.True(t, g.Enabled()) assert.True(t, g.Enabled())
assert.Equal(t, fileInfo.Path, g.workingFolder) assert.Equal(t, fileInfo.Path, g.workingDir)
} }
func TestResolveEmptyGitPath(t *testing.T) { func TestResolveEmptyGitPath(t *testing.T) {
@ -136,9 +136,9 @@ func TestEnabledInWorktree(t *testing.T) {
}, },
} }
assert.Equal(t, tc.ExpectedEnabled, g.hasWorktree(fileInfo), tc.Case) assert.Equal(t, tc.ExpectedEnabled, g.hasWorktree(fileInfo), tc.Case)
assert.Equal(t, tc.ExpectedWorkingFolder, g.workingFolder, tc.Case) assert.Equal(t, tc.ExpectedWorkingFolder, g.workingDir, tc.Case)
assert.Equal(t, tc.ExpectedRealFolder, g.realFolder, tc.Case) assert.Equal(t, tc.ExpectedRealFolder, g.realDir, tc.Case)
assert.Equal(t, tc.ExpectedRootFolder, g.rootFolder, tc.Case) assert.Equal(t, tc.ExpectedRootFolder, g.rootDir, tc.Case)
} }
} }
@ -507,8 +507,8 @@ func TestGetStashContextZeroEntries(t *testing.T) {
env.On("FileContent", "/logs/refs/stash").Return(tc.StashContent) env.On("FileContent", "/logs/refs/stash").Return(tc.StashContent)
g := &Git{ g := &Git{
scm: scm{ scm: scm{
env: env, env: env,
workingFolder: "", workingDir: "",
}, },
} }
got := g.getStashContext() got := g.getStashContext()
@ -809,7 +809,7 @@ func TestGitUntrackedMode(t *testing.T) {
props: properties.Map{ props: properties.Map{
UntrackedModes: tc.UntrackedModes, UntrackedModes: tc.UntrackedModes,
}, },
realFolder: "foo", realDir: "foo",
}, },
} }
got := g.getUntrackedFilesMode() got := g.getUntrackedFilesMode()
@ -851,7 +851,7 @@ func TestGitIgnoreSubmodules(t *testing.T) {
props: properties.Map{ props: properties.Map{
IgnoreSubmodules: tc.IgnoreSubmodules, IgnoreSubmodules: tc.IgnoreSubmodules,
}, },
realFolder: "foo", realDir: "foo",
}, },
} }
got := g.getIgnoreSubmodulesMode() got := g.getIgnoreSubmodulesMode()

View file

@ -45,10 +45,12 @@ type scm struct {
env environment.Environment env environment.Environment
IsWslSharedPath bool IsWslSharedPath bool
workingFolder string Dir string // actual repo root directory
rootFolder string
realFolder string // real folder (can be different from current path when in worktrees) workingDir string
command string rootDir string
realDir string // real directory (can be different from current path when in worktrees)
command string
} }
const ( const (

View file

@ -78,20 +78,20 @@ func (s *Svn) shouldDisplay() bool {
} }
if Svndir.IsDir { if Svndir.IsDir {
s.workingFolder = Svndir.Path s.workingDir = Svndir.Path
s.rootFolder = Svndir.Path s.rootDir = Svndir.Path
// convert the worktree file path to a windows one when in wsl 2 shared folder // convert the worktree file path to a windows one when in wsl 2 shared folder
s.realFolder = strings.TrimSuffix(s.convertToWindowsPath(Svndir.Path), ".svn") s.realDir = strings.TrimSuffix(s.convertToWindowsPath(Svndir.Path), ".svn")
return true return true
} }
// handle worktree // handle worktree
s.rootFolder = Svndir.Path s.rootDir = Svndir.Path
dirPointer := strings.Trim(s.env.FileContent(Svndir.Path), " \r\n") dirPointer := strings.Trim(s.env.FileContent(Svndir.Path), " \r\n")
matches := regex.FindNamedRegexMatch(`^Svndir: (?P<dir>.*)$`, dirPointer) matches := regex.FindNamedRegexMatch(`^Svndir: (?P<dir>.*)$`, dirPointer)
if matches != nil && matches["dir"] != "" { if matches != nil && matches["dir"] != "" {
// if we open a worktree file in a shared wsl2 folder, we have to convert it back // if we open a worktree file in a shared wsl2 folder, we have to convert it back
// to the mounted path // to the mounted path
s.workingFolder = s.convertToLinuxPath(matches["dir"]) s.workingDir = s.convertToLinuxPath(matches["dir"])
return false return false
} }
return false return false
@ -121,7 +121,7 @@ func (s *Svn) setSvnStatus() {
} }
func (s *Svn) getSvnCommandOutput(command string, args ...string) string { func (s *Svn) getSvnCommandOutput(command string, args ...string) string {
args = append([]string{command, s.realFolder}, args...) args = append([]string{command, s.realDir}, args...)
val, err := s.env.RunCommand(s.getCommand(SVNCOMMAND), args...) val, err := s.env.RunCommand(s.getCommand(SVNCOMMAND), args...)
if err != nil { if err != nil {
return "" return ""

View file

@ -45,8 +45,8 @@ func TestSvnEnabledInWorkingDirectory(t *testing.T) {
}, },
} }
assert.True(t, s.Enabled()) assert.True(t, s.Enabled())
assert.Equal(t, fileInfo.Path, s.workingFolder) assert.Equal(t, fileInfo.Path, s.workingDir)
assert.Equal(t, fileInfo.Path, s.realFolder) assert.Equal(t, fileInfo.Path, s.realDir)
} }
func TestSvnTemplateString(t *testing.T) { func TestSvnTemplateString(t *testing.T) {

View file

@ -130,6 +130,7 @@ instead of the repo path.
- `.StashCount`: `int` - the stash count - `.StashCount`: `int` - the stash count
- `.WorktreeCount`: `int` - the worktree count - `.WorktreeCount`: `int` - the worktree count
- `.IsWorkTree`: `boolean` - if in a worktree repo or not - `.IsWorkTree`: `boolean` - if in a worktree repo or not
- `.Dir`: `string` - the repository's root directory
### GitStatus ### GitStatus