From 389ecd070386f27e2bc863825606d5e410169802 Mon Sep 17 00:00:00 2001 From: Gadzhi Kharkharov Date: Thu, 16 Sep 2021 01:43:58 +0300 Subject: [PATCH] fix: parse git dir from filepath instead of cwd --- cmd/edit.go | 12 +------ kak/filepath.go | 25 +++++++++++++++ kak/filepath_test.go | 75 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 11 deletions(-) diff --git a/cmd/edit.go b/cmd/edit.go index e93b466..d2456e9 100644 --- a/cmd/edit.go +++ b/cmd/edit.go @@ -4,8 +4,6 @@ import ( "flag" "fmt" "os" - "os/exec" - "path" "strings" "github.com/kkga/kks/kak" @@ -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 != "" { @@ -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)), ".", "-")) -} diff --git a/kak/filepath.go b/kak/filepath.go index 1e8b3f8..59030a1 100644 --- a/kak/filepath.go +++ b/kak/filepath.go @@ -2,6 +2,7 @@ package kak import ( "os" + "os/exec" "path" "path/filepath" "strconv" @@ -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 diff --git a/kak/filepath_test.go b/kak/filepath_test.go index b73b85e..02a7567 100644 --- a/kak/filepath_test.go +++ b/kak/filepath_test.go @@ -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