Skip to content

Commit 077a98c

Browse files
committed
Revert this stuff.
1 parent 9306b05 commit 077a98c

File tree

5 files changed

+50
-20
lines changed

5 files changed

+50
-20
lines changed

dir/dir.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dir
22

33
import (
4+
"strings"
5+
46
"github.com/joshmedeski/sesh/v2/git"
57
"github.com/joshmedeski/sesh/v2/oswrap"
68
"github.com/joshmedeski/sesh/v2/pathwrap"
@@ -39,12 +41,38 @@ func (d *RealDir) Dir(path string) (isDir bool, absPath string) {
3941
}
4042

4143
func (d *RealDir) RootDir(path string) (hasRootDir bool, absPath string) {
42-
isGit, absPath, err := d.git.GitRoot(path)
43-
if err != nil {
44-
return false, ""
44+
isGitBare, absPath := gitBareRootDir(d, path)
45+
if isGitBare {
46+
return true, absPath
4547
}
48+
isGit, absPath := gitRootDir(d, path)
4649
if isGit {
4750
return true, absPath
4851
}
4952
return false, ""
5053
}
54+
55+
func gitBareRootDir(d *RealDir, path string) (hasRootDir bool, absPath string) {
56+
isGitBare, commonDir, _ := d.git.GitCommonDir(path)
57+
if isGitBare && strings.HasSuffix(commonDir, "/.bare") {
58+
topLevelDir := strings.TrimSuffix(commonDir, "/.bare")
59+
relativePath := strings.TrimPrefix(path, topLevelDir)
60+
firstDir := strings.Split(relativePath, string("/"))[1]
61+
name, err := d.path.Abs(topLevelDir + "/" + firstDir)
62+
if err != nil {
63+
return false, ""
64+
}
65+
return true, name
66+
} else {
67+
return false, ""
68+
}
69+
}
70+
71+
func gitRootDir(d *RealDir, path string) (hasDir bool, absPath string) {
72+
isGit, topLevelDir, _ := d.git.ShowTopLevel(path)
73+
if isGit && topLevelDir != "" {
74+
return true, topLevelDir
75+
} else {
76+
return false, ""
77+
}
78+
}

namer/namer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func (n *RealNamer) Name(path string) (string, error) {
3535
}
3636

3737
strategies := []func(*RealNamer, string) (string, error){
38+
gitBareName,
3839
gitName,
3940
dirName,
4041
}

namer/namer_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,26 @@ func TestFromPath(t *testing.T) {
1919

2020
t.Run("name for git repo", func(t *testing.T) {
2121
mockPathwrap.On("EvalSymlinks", "/Users/josh/config/dotfiles/.config/neovim").Return("/Users/josh/config/dotfiles/.config/neovim", nil)
22-
mockGit.On("GitRoot", "/Users/josh/config/dotfiles/.config/neovim").Return(true, "/Users/josh/config/dotfiles", nil)
22+
mockGit.On("ShowTopLevel", "/Users/josh/config/dotfiles/.config/neovim").Return(true, "/Users/josh/config/dotfiles", nil)
23+
// mockGit.On("GitCommonDir", "/Users/josh/config/dotfiles/.config/neovim").Return(true, "", nil)
2324
mockPathwrap.On("Base", "/Users/josh/config/dotfiles").Return("dotfiles")
2425
name, _ := n.Name("/Users/josh/config/dotfiles/.config/neovim")
2526
assert.Equal(t, "dotfiles/_config/neovim", name)
2627
})
2728

2829
t.Run("name for git worktree", func(t *testing.T) {
2930
mockPathwrap.On("EvalSymlinks", "/Users/josh/config/sesh/main").Return("/Users/josh/config/sesh/main", nil)
30-
mockGit.On("GitRoot", "/Users/josh/config/sesh/main").Return(true, "/Users/josh/config/sesh", nil)
31+
mockGit.On("ShowTopLevel", "/Users/josh/config/sesh/main").Return(true, "/Users/josh/config/sesh/main", nil)
32+
// mockGit.On("GitCommonDir", "/Users/josh/config/sesh/main").Return(true, "/Users/josh/config/sesh/.bare", nil)
3133
mockPathwrap.On("Base", "/Users/josh/config/sesh").Return("sesh")
3234
name, _ := n.Name("/Users/josh/config/sesh/main")
3335
assert.Equal(t, "sesh/main", name)
3436
})
3537

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

5154
t.Run("name for symlinked file in symlinked git repo", func(t *testing.T) {
5255
mockPathwrap.On("EvalSymlinks", "/Users/josh/d/.c/neovim").Return("/Users/josh/dotfiles/.config/neovim", nil)
53-
mockGit.On("GitRoot", "/Users/josh/dotfiles/.config/neovim").Return(true, "/Users/josh/dotfiles", nil)
56+
mockGit.On("ShowTopLevel", "/Users/josh/dotfiles/.config/neovim").Return(true, "/Users/josh/dotfiles", nil)
57+
// mockGit.On("GitCommonDir", "/Users/josh/dotfiles/.config/neovim").Return(true, "", nil)
5458
mockPathwrap.On("Base", "/Users/josh/dotfiles").Return("dotfiles")
5559
name, _ := n.Name("/Users/josh/d/.c/neovim")
5660
assert.Equal(t, "dotfiles/_config/neovim", name)
5761
})
5862

5963
t.Run("name for git worktree", func(t *testing.T) {
6064
mockPathwrap.On("EvalSymlinks", "/Users/josh/p/sesh/main").Return("/Users/josh/projects/sesh/main", nil)
61-
mockGit.On("GitRoot", "/Users/josh/projects/sesh/main").Return(true, "/Users/josh/projects/sesh", nil)
65+
mockGit.On("ShowTopLevel", "/Users/josh/projects/sesh/main").Return(true, "/Users/josh/projects/sesh/main", nil)
66+
// mockGit.On("GitCommonDir", "/Users/josh/projects/sesh/main").Return(true, "/Users/josh/projects/sesh/.bare", nil)
6267
mockPathwrap.On("Base", "/Users/josh/projects/sesh").Return("sesh")
6368
name, _ := n.Name("/Users/josh/p/sesh/main")
6469
assert.Equal(t, "sesh/main", name)
6570
})
6671

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

seshcli/root.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,24 @@ package seshcli
33
import (
44
"fmt"
55

6-
"github.com/joshmedeski/sesh/v2/git"
7-
"github.com/joshmedeski/sesh/v2/home"
86
"github.com/joshmedeski/sesh/v2/lister"
7+
"github.com/joshmedeski/sesh/v2/namer"
98
cli "github.com/urfave/cli/v2"
109
)
1110

12-
func Root(l lister.Lister, git git.Git, home home.Home) *cli.Command {
11+
func Root(l lister.Lister, n namer.Namer) *cli.Command {
1312
return &cli.Command{
1413
Name: "root",
1514
Aliases: []string{"r"},
16-
Usage: "Show the root for the active session",
15+
Usage: "Show the root from the active session",
1716
UseShortOptionHandling: true,
1817
Flags: []cli.Flag{},
1918
Action: func(cCtx *cli.Context) error {
2019
session, exists := l.GetAttachedTmuxSession()
2120
if !exists {
22-
return cli.Exit("Not attached to tmux session", 1)
21+
return cli.Exit("No root found for session", 1)
2322
}
24-
_, path, err := git.GitRoot(session.Path)
25-
if err != nil {
26-
return cli.Exit(err, 1)
27-
}
28-
root, err := home.ShortenHome(path)
23+
root, err := n.RootName(session.Path)
2924
if err != nil {
3025
return cli.Exit(err, 1)
3126
}

seshcli/seshcli.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func App(version string) cli.App {
7676
Last(lister, tmux),
7777
Connect(connector, icon, dir),
7878
Clone(cloner),
79-
Root(lister, git, home),
79+
Root(lister, namer),
8080
Preview(previewer),
8181
},
8282
}

0 commit comments

Comments
 (0)