Skip to content

Commit

Permalink
[#98] (snippet,fix) use /tmp for personal snippets when editing outsi…
Browse files Browse the repository at this point in the history
…de of a git repo

before this would just cause a fatal error indicating "not a git repository"
  • Loading branch information
zaquestion committed Jan 16, 2018
1 parent e6c5e00 commit 283c3f8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
11 changes: 10 additions & 1 deletion cmd/snippetCreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 16 additions & 6 deletions internal/git/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
4 changes: 4 additions & 0 deletions internal/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,7 @@ func TestIsRemote(t *testing.T) {
}
require.True(t, res)
}

func TestInsideGitRepo(t *testing.T) {
require.True(t, InsideGitRepo())
}

0 comments on commit 283c3f8

Please sign in to comment.