Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve git root detection logic #195

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 1 addition & 32 deletions dir/dir.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package dir

import (
"strings"

"github.com/joshmedeski/sesh/git"
"github.com/joshmedeski/sesh/oswrap"
"github.com/joshmedeski/sesh/pathwrap"
Expand Down Expand Up @@ -41,38 +39,9 @@ func (d *RealDir) Dir(path string) (isDir bool, absPath string) {
}

func (d *RealDir) RootDir(path string) (hasRootDir bool, absPath string) {
isGitBare, absPath := gitBareRootDir(d, path)
if isGitBare {
return true, absPath
}
isGit, absPath := gitRootDir(d, path)
isGit, absPath, _ := d.git.GitRoot(path)
if isGit {
return true, absPath
}
return false, ""
}

func gitBareRootDir(d *RealDir, path string) (hasRootDir bool, absPath string) {
isGitBare, commonDir, _ := d.git.GitCommonDir(path)
if isGitBare && strings.HasSuffix(commonDir, "/.bare") {
topLevelDir := strings.TrimSuffix(commonDir, "/.bare")
relativePath := strings.TrimPrefix(path, topLevelDir)
firstDir := strings.Split(relativePath, string("/"))[1]
name, err := d.path.Abs(topLevelDir + "/" + firstDir)
if err != nil {
return false, ""
}
return true, name
} else {
return false, ""
}
}

func gitRootDir(d *RealDir, path string) (hasDir bool, absPath string) {
isGit, topLevelDir, _ := d.git.ShowTopLevel(path)
if isGit && topLevelDir != "" {
return true, topLevelDir
} else {
return false, ""
}
}
20 changes: 7 additions & 13 deletions git/git.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package git

import (
"strings"

"github.com/joshmedeski/sesh/shell"
)

type Git interface {
ShowTopLevel(name string) (bool, string, error)
GitCommonDir(name string) (bool, string, error)
GitRoot(name string) (bool, string, error)
Clone(name string) (string, error)
}

Expand All @@ -18,20 +19,13 @@ func NewGit(shell shell.Shell) Git {
return &RealGit{shell}
}

func (g *RealGit) ShowTopLevel(path string) (bool, string, error) {
out, err := g.shell.Cmd("git", "-C", path, "rev-parse", "--show-toplevel")
if err != nil {
return false, "", err
}
return true, out, nil
}

func (g *RealGit) GitCommonDir(path string) (bool, string, error) {
out, err := g.shell.Cmd("git", "-C", path, "rev-parse", "--git-common-dir")
func (g *RealGit) GitRoot(path string) (bool, string, error) {
out, err := g.shell.Cmd("git", "-C", path, "worktree", "list")
if err != nil {
return false, "", err
}
return true, out, nil
main := strings.Fields(out)[0]
return true, main, nil
}

func (g *RealGit) Clone(name string) (string, error) {
Expand Down
47 changes: 47 additions & 0 deletions git/git_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package git

import (
"errors"
"testing"

"github.com/joshmedeski/sesh/shell"
"github.com/stretchr/testify/assert"
)

func TestGitRoot(t *testing.T) {
t.Run("run should find worktree root", func(t *testing.T) {
mockShell := new(shell.MockShell)
mockShell.On("Cmd", "git", "-C", "~/code/project/sesh/main", "worktree", "list").Return(`
/Users/hansolo/code/project/sesh (bare)
/Users/hansolo/code/project/sesh/main ba04ca494 [5.x]
`, nil)
git := &RealGit{shell: mockShell}
isGit, out, err := git.GitRoot("~/code/project/sesh/main")
assert.True(t, isGit)
assert.Nil(t, err)
assert.Equal(t, "/Users/hansolo/code/project/sesh", out)
})

t.Run("run should find non-worktree root", func(t *testing.T) {
mockShell := new(shell.MockShell)
mockShell.On("Cmd", "git", "-C", "~/.dotfiles/nvim", "worktree", "list").Return(`
/Users/hansolo/.dotfiles ba04ca494 [5.x]
`, nil)
git := &RealGit{shell: mockShell}
isGit, out, err := git.GitRoot("~/.dotfiles/nvim")
assert.True(t, isGit)
assert.Nil(t, err)
assert.Equal(t, "/Users/hansolo/.dotfiles", out)
})

t.Run("run should fail when not in git repo", func(t *testing.T) {
mockShell := new(shell.MockShell)
mockShell.On("Cmd", "git", "-C", "~/not-a-repo", "worktree", "list").Return("", errors.New(`
fatal: not a git repository (or any of the parent directories): .git
`))
git := &RealGit{shell: mockShell}
isGit, out, _ := git.GitRoot("~/not-a-repo")
assert.False(t, isGit)
assert.Equal(t, "", out)
})
}
85 changes: 11 additions & 74 deletions git/mock_Git.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 5 additions & 35 deletions namer/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,20 @@ import (
"strings"
)

func gitBareRootName(n *RealNamer, path string) (string, error) {
isGit, commonDir, _ := n.git.GitCommonDir(path)
if isGit && strings.HasSuffix(commonDir, "/.bare") {
topLevelDir := strings.TrimSuffix(commonDir, "/.bare")
name, err := n.home.ShortenHome(topLevelDir)
if err != nil {
return "", fmt.Errorf("couldn't shorten path: %q", err)
}
return name, nil
} else {
return "", nil
}
}

// Gets the name from a git bare repository
func gitBareName(n *RealNamer, path string) (string, error) {
var name string
isGit, commonDir, _ := n.git.GitCommonDir(path)
if isGit && strings.HasSuffix(commonDir, "/.bare") {
topLevelDir := strings.TrimSuffix(commonDir, "/.bare")
func gitName(n *RealNamer, path string) (string, error) {
isGit, topLevelDir, _ := n.git.GitRoot(path)
if isGit && topLevelDir != "" {
relativePath := strings.TrimPrefix(path, topLevelDir)
baseDir := n.pathwrap.Base(topLevelDir)
name = baseDir + relativePath
name := baseDir + relativePath
return name, nil
} else {
return "", nil
}
}

func gitRootName(n *RealNamer, path string) (string, error) {
isGit, topLevelDir, _ := n.git.ShowTopLevel(path)
isGit, topLevelDir, _ := n.git.GitRoot(path)
if isGit && topLevelDir != "" {
name, err := n.home.ShortenHome(topLevelDir)
if err != nil {
Expand All @@ -46,16 +29,3 @@ func gitRootName(n *RealNamer, path string) (string, error) {
return "", nil
}
}

// Gets the name from a git repository
func gitName(n *RealNamer, path string) (string, error) {
isGit, topLevelDir, _ := n.git.ShowTopLevel(path)
if isGit && topLevelDir != "" {
relativePath := strings.TrimPrefix(path, topLevelDir)
baseDir := n.pathwrap.Base(topLevelDir)
name := baseDir + relativePath
return name, nil
} else {
return "", nil
}
}
1 change: 0 additions & 1 deletion namer/namer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func (n *RealNamer) Name(path string) (string, error) {
}

strategies := []func(*RealNamer, string) (string, error){
gitBareName,
gitName,
dirName,
}
Expand Down
18 changes: 6 additions & 12 deletions namer/namer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,23 @@ func TestFromPath(t *testing.T) {

t.Run("name for git repo", func(t *testing.T) {
mockPathwrap.On("EvalSymlinks", "/Users/josh/config/dotfiles/.config/neovim").Return("/Users/josh/config/dotfiles/.config/neovim", nil)
mockGit.On("ShowTopLevel", "/Users/josh/config/dotfiles/.config/neovim").Return(true, "/Users/josh/config/dotfiles", nil)
mockGit.On("GitCommonDir", "/Users/josh/config/dotfiles/.config/neovim").Return(true, "", nil)
mockGit.On("GitRoot", "/Users/josh/config/dotfiles/.config/neovim").Return(true, "/Users/josh/config/dotfiles", nil)
mockPathwrap.On("Base", "/Users/josh/config/dotfiles").Return("dotfiles")
name, _ := n.Name("/Users/josh/config/dotfiles/.config/neovim")
assert.Equal(t, "dotfiles/_config/neovim", name)
})

t.Run("name for git worktree", func(t *testing.T) {
mockPathwrap.On("EvalSymlinks", "/Users/josh/config/sesh/main").Return("/Users/josh/config/sesh/main", nil)
mockGit.On("ShowTopLevel", "/Users/josh/config/sesh/main").Return(true, "/Users/josh/config/sesh/main", nil)
mockGit.On("GitCommonDir", "/Users/josh/config/sesh/main").Return(true, "/Users/josh/config/sesh/.bare", nil)
mockGit.On("GitRoot", "/Users/josh/config/sesh/main").Return(true, "/Users/josh/config/sesh", nil)
mockPathwrap.On("Base", "/Users/josh/config/sesh").Return("sesh")
name, _ := n.Name("/Users/josh/config/sesh/main")
assert.Equal(t, "sesh/main", name)
})

t.Run("returns base on non-git dir", func(t *testing.T) {
mockPathwrap.On("EvalSymlinks", "/Users/josh/.config/neovim").Return("/Users/josh/.config/neovim", nil)
mockGit.On("ShowTopLevel", "/Users/josh/.config/neovim").Return(false, "", fmt.Errorf("not a git repository (or any of the parent"))
mockGit.On("GitCommonDir", "/Users/josh/.config/neovim").Return(false, "", fmt.Errorf("not a git repository (or any of the parent"))
mockGit.On("GitRoot", "/Users/josh/.config/neovim").Return(false, "", fmt.Errorf("not a git repository (or any of the parent"))
mockPathwrap.On("Base", "/Users/josh/.config/neovim").Return("neovim")
name, _ := n.Name("/Users/josh/.config/neovim")
assert.Equal(t, "neovim", name)
Expand All @@ -53,26 +50,23 @@ func TestFromPath(t *testing.T) {

t.Run("name for symlinked file in symlinked git repo", func(t *testing.T) {
mockPathwrap.On("EvalSymlinks", "/Users/josh/d/.c/neovim").Return("/Users/josh/dotfiles/.config/neovim", nil)
mockGit.On("ShowTopLevel", "/Users/josh/dotfiles/.config/neovim").Return(true, "/Users/josh/dotfiles", nil)
mockGit.On("GitCommonDir", "/Users/josh/dotfiles/.config/neovim").Return(true, "", nil)
mockGit.On("GitRoot", "/Users/josh/dotfiles/.config/neovim").Return(true, "/Users/josh/dotfiles", nil)
mockPathwrap.On("Base", "/Users/josh/dotfiles").Return("dotfiles")
name, _ := n.Name("/Users/josh/d/.c/neovim")
assert.Equal(t, "dotfiles/_config/neovim", name)
})

t.Run("name for git worktree", func(t *testing.T) {
mockPathwrap.On("EvalSymlinks", "/Users/josh/p/sesh/main").Return("/Users/josh/projects/sesh/main", nil)
mockGit.On("ShowTopLevel", "/Users/josh/projects/sesh/main").Return(true, "/Users/josh/projects/sesh/main", nil)
mockGit.On("GitCommonDir", "/Users/josh/projects/sesh/main").Return(true, "/Users/josh/projects/sesh/.bare", nil)
mockGit.On("GitRoot", "/Users/josh/projects/sesh/main").Return(true, "/Users/josh/projects/sesh", nil)
mockPathwrap.On("Base", "/Users/josh/projects/sesh").Return("sesh")
name, _ := n.Name("/Users/josh/p/sesh/main")
assert.Equal(t, "sesh/main", name)
})

t.Run("returns base on non-git dir", func(t *testing.T) {
mockPathwrap.On("EvalSymlinks", "/Users/josh/c/neovim").Return("/Users/josh/.config/neovim", nil)
mockGit.On("ShowTopLevel", "/Users/josh/.config/neovim").Return(false, "", fmt.Errorf("not a git repository (or any of the parent"))
mockGit.On("GitCommonDir", "/Users/josh/.config/neovim").Return(false, "", fmt.Errorf("not a git repository (or any of the parent"))
mockGit.On("GitRoot", "/Users/josh/.config/neovim").Return(false, "", fmt.Errorf("not a git repository (or any of the parent"))
mockPathwrap.On("Base", "/Users/josh/.config/neovim").Return("neovim")
name, _ := n.Name("/Users/josh/c/neovim")
assert.Equal(t, "neovim", name)
Expand Down
Loading