Skip to content

Commit

Permalink
fix: parse git dir from filepath instead of cwd
Browse files Browse the repository at this point in the history
  • Loading branch information
kkga committed Sep 15, 2021
1 parent 1c00791 commit 389ecd0
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 11 deletions.
12 changes: 1 addition & 11 deletions cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"flag"
"fmt"
"os"
"os/exec"
"path"
"strings"

"github.com/kkga/kks/kak"
Expand Down Expand Up @@ -41,7 +39,7 @@ func (c *EditCmd) Run() error {
_, useGitDirSessions := os.LookupEnv("KKS_USE_GITDIR_SESSIONS")

if useGitDirSessions {
gitDirName = parseGitToplevel()
gitDirName = fp.ParseGitDir()
}

if gitDirName != "" {
Expand Down Expand Up @@ -114,11 +112,3 @@ func (c *EditCmd) Run() error {

return nil
}

func parseGitToplevel() string {
gitOut, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
if err != nil {
return ""
}
return strings.TrimSpace(strings.ReplaceAll(path.Base(string(gitOut)), ".", "-"))
}
25 changes: 25 additions & 0 deletions kak/filepath.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kak

import (
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -31,6 +32,30 @@ func NewFilepath(args []string) (fp *Filepath, err error) {
return
}

func (fp *Filepath) Dir() (dir string, err error) {
info, err := os.Stat(fp.Name)
if err != nil {
return "", err
}

if info.IsDir() {
dir = fp.Name
} else {
dir = path.Dir(fp.Name)
}

return
}

func (fp *Filepath) ParseGitDir() string {
dir, _ := fp.Dir()
gitOut, err := exec.Command("git", "-C", dir, "rev-parse", "--show-toplevel").Output()
if err != nil {
return ""
}
return strings.TrimSpace(strings.ReplaceAll(path.Base(string(gitOut)), ".", "-"))
}

func (fp *Filepath) parse() (absName string, line int, col int, err error) {
r := fp.Raw

Expand Down
75 changes: 75 additions & 0 deletions kak/filepath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,81 @@ import (
"github.com/google/go-cmp/cmp"
)

func TestDir(t *testing.T) {
tests := []struct {
fp Filepath
want string
}{
{
Filepath{Name: "/home/kkga"},
"/home/kkga",
},
{
Filepath{Name: "/home/kkga/README.md"},
"/home/kkga",
},
{
Filepath{Name: "/home/kkga/projects/kks/"},
"/home/kkga/projects/kks/",
},
{
Filepath{Name: "/home/kkga/projects/kks/cmd/attach.go"},
"/home/kkga/projects/kks/cmd",
},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
got, err := tt.fp.Dir()
if err != nil {
t.Fatal("can't get Filepath.Dir(): ", err)
}
if !cmp.Equal(tt.want, got) {
t.Errorf("Filepath.Dir() mismatch:\n%s", cmp.Diff(tt.want, got))
}
})
}
}

func TestParseGitDir(t *testing.T) {
tests := []struct {
fp Filepath
want string
}{
{
Filepath{Name: "/home/kkga"},
"",
},
{
Filepath{Name: "/home/kkga/README.md"},
"",
},
{
Filepath{Name: "/home/kkga/projects/kks/"},
"kks",
},
{
Filepath{Name: "/home/kkga/projects/kks/cmd/attach.go"},
"kks",
},
{
Filepath{Name: "/home/kkga/projects/foot.kak/rc/foot.kak"},
"foot-kak",
},
{
Filepath{Name: "/home/kkga/repos/kakoune/rc/detection/editorconfig.kak"},
"kakoune",
},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
got := tt.fp.ParseGitDir()
if !cmp.Equal(tt.want, got) {
t.Errorf("Filepath.ParseGitDir() mismatch:\n%s", cmp.Diff(tt.want, got))
}
})
}
}

func TestNewFilepath(t *testing.T) {
tests := []struct {
args []string
Expand Down

0 comments on commit 389ecd0

Please sign in to comment.