From 283c3f87b52b2b5b91d6f53bfeddfc93b53c304b Mon Sep 17 00:00:00 2001 From: Zaq? Wiedmann Date: Mon, 15 Jan 2018 17:39:48 -0800 Subject: [PATCH] [#98] (snippet,fix) use /tmp for personal snippets when editing outside of a git repo before this would just cause a fatal error indicating "not a git repository" --- cmd/snippetCreate_test.go | 11 ++++++++++- internal/git/edit.go | 22 ++++++++++++++++------ internal/git/git.go | 10 ++++++++++ internal/git/git_test.go | 4 ++++ 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/cmd/snippetCreate_test.go b/cmd/snippetCreate_test.go index 7842296f..07f12223 100644 --- a/cmd/snippetCreate_test.go +++ b/cmd/snippetCreate_test.go @@ -43,7 +43,16 @@ func Test_snippetCreate(t *testing.T) { func Test_snippetCreate_Global(t *testing.T) { t.Parallel() repo := copyTestRepo(t) - cmd := exec.Command("../lab_bin", "snippet", "create", "-g", + + // Remove .git dir forcing the cmd to exec outside of a git repo + cmd := exec.Command("rm", "-rf", ".git") + cmd.Dir = repo + err := cmd.Run() + if err != nil { + t.Fatal(err) + } + + cmd = exec.Command("../lab_bin", "snippet", "create", "-g", "-m", "personal snippet title", "-m", "personal snippet description") cmd.Dir = repo diff --git a/internal/git/edit.go b/internal/git/edit.go index 61dd8c0f..e61dfb19 100644 --- a/internal/git/edit.go +++ b/internal/git/edit.go @@ -10,20 +10,30 @@ import ( "strings" ) -// Edit opens a file in the users editor and returns the title and body +// Edit opens a file in the users editor and returns the title and body. It +// store a temporary file in your .git directory or /tmp if accessed outside of +// a git repo. func Edit(filePrefix, message string) (string, string, error) { - gitDir, err := GitDir() - if err != nil { - return "", "", err + var ( + dir string + err error + ) + if InsideGitRepo() { + dir, err = GitDir() + if err != nil { + return "", "", err + } + } else { + dir = "/tmp" } - filePath := filepath.Join(gitDir, fmt.Sprintf("%s_EDITMSG", filePrefix)) + filePath := filepath.Join(dir, fmt.Sprintf("%s_EDITMSG", filePrefix)) editorPath, err := editorPath() if err != nil { return "", "", err } defer os.Remove(filePath) - // Write generated/tempate message to file + // Write generated/template message to file if _, err := os.Stat(filePath); os.IsNotExist(err) && message != "" { err = ioutil.WriteFile(filePath, []byte(message), 0644) if err != nil { diff --git a/internal/git/git.go b/internal/git/git.go index 6473e60b..a97dc880 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -186,3 +186,13 @@ func IsRemote(remote string) (bool, error) { return bytes.Contains(remotes, []byte(remote+"\n")), nil } + +// InsideGitRepo returns true when the current working directory is inside the +// working tree of a git repo +func InsideGitRepo() bool { + cmd := New("rev-parse", "--is-inside-work-tree") + cmd.Stdout = nil + cmd.Stderr = nil + out, _ := cmd.CombinedOutput() + return bytes.Contains(out, []byte("true\n")) +} diff --git a/internal/git/git_test.go b/internal/git/git_test.go index 8ab014cc..02a02c7f 100644 --- a/internal/git/git_test.go +++ b/internal/git/git_test.go @@ -94,3 +94,7 @@ func TestIsRemote(t *testing.T) { } require.True(t, res) } + +func TestInsideGitRepo(t *testing.T) { + require.True(t, InsideGitRepo()) +}