Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getGitHooksPath helper function #177

Merged
merged 2 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"io/ioutil"
"log"
"os/exec"
"path/filepath"
"strings"

Expand Down Expand Up @@ -94,7 +93,7 @@ else
fi
`

pathToFile := filepath.Join(getGitHooksDir(), hookName)
pathToFile := filepath.Join(getGitHooksPath(), hookName)

if yes, _ := afero.Exists(fs, pathToFile); yes {
if isLefthookFile(pathToFile) {
Expand Down Expand Up @@ -150,18 +149,6 @@ func addLocalHookDir(hookName string, fs afero.Fs) {
check(err)
}

func getGitHooksDir() string {
cmd := exec.Command("git", "rev-parse", "--git-common-dir")

outputBytes, err := cmd.CombinedOutput()
if err != nil {
panic(err)
}

gitDir := strings.TrimSpace(string(outputBytes))
return filepath.Join(getRootPath(), gitDir, "hooks")
}

func isLefthookFile(pathFile string) bool {
file, err := ioutil.ReadFile(pathFile)
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestInstallCmdExecutor(t *testing.T) {
"prepare-commit-msg",
}

files, err := afero.ReadDir(fs, getGitHooksDir())
files, err := afero.ReadDir(fs, getGitHooksPath())
assert.NoError(t, err)

actualFiles := []string{}
Expand All @@ -60,7 +60,7 @@ func TestAddCmdExecutor(t *testing.T) {
"pre-commit",
}

files, _ := afero.ReadDir(fs, getGitHooksDir())
files, _ := afero.ReadDir(fs, getGitHooksPath())
actualFiles := []string{}
for _, f := range files {
actualFiles = append(actualFiles, f.Name())
Expand All @@ -82,7 +82,7 @@ func TestAddCmdExecutor(t *testing.T) {
"pre-push.old",
}

files, _ = afero.ReadDir(fs, getGitHooksDir())
files, _ = afero.ReadDir(fs, getGitHooksPath())
actualFiles = []string{}
for _, f := range files {
actualFiles = append(actualFiles, f.Name())
Expand Down Expand Up @@ -147,7 +147,8 @@ func presetConfig(fs afero.Fs) {
fs.Mkdir(filepath.Join(getRootPath(), ".lefthook/commit-msg"), defaultFilePermission)
fs.Mkdir(filepath.Join(getRootPath(), ".lefthook/pre-commit"), defaultFilePermission)

fs.MkdirAll(filepath.Join(getRootPath(), ".git", "hooks"), defaultFilePermission)
setGitHooksPath(".git/hooks")
Envek marked this conversation as resolved.
Show resolved Hide resolved
fs.MkdirAll(getGitHooksPath(), defaultFilePermission)
}

func presetExecutable(hookName string, hookGroup string, exitCode string, fs afero.Fs) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func configChecksum(fs afero.Fs) string {
func recordedChecksum() string {
pattern := regexp.MustCompile(`(?:# lefthook_version: )(\w+)`)

file, err := ioutil.ReadFile(filepath.Join(getGitHooksDir(), checkSumHook))
file, err := ioutil.ReadFile(filepath.Join(getGitHooksPath(), checkSumHook))
if err != nil {
return ""
}
Expand Down
35 changes: 28 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"strings"

"github.com/logrusorgru/aurora"
"github.com/spf13/afero"
"github.com/mattn/go-isatty"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -28,11 +28,12 @@ const (
)

var (
Verbose bool
NoColors bool
rootPath string
cfgFile string
originConfig *viper.Viper
Verbose bool
NoColors bool
rootPath string
gitHooksPath string
cfgFile string
originConfig *viper.Viper
configFileExtensions = []string{".yml", ".yaml"}

au aurora.Aurora
Expand All @@ -57,7 +58,7 @@ lefthook install`,
message := `This command must be executed within git repository.
Change working directory or initialize new repository with 'git init'.`

log.Fatal(au.Brown(message))
log.Fatal(au.Brown(message))
},
}

Expand Down Expand Up @@ -92,6 +93,7 @@ func initConfig() {
log.SetFlags(0)

setRootPath(rootExecutionRelPath)
setGitHooksPath(getHooksPathFromGitConfig())

// store original config before merge
originConfig = viper.New()
Expand Down Expand Up @@ -135,6 +137,25 @@ func setRootPath(path string) {
rootPath = strings.TrimSpace(string(outputBytes))
}

func getGitHooksPath() string {
return gitHooksPath
}

func setGitHooksPath(path string) {
gitHooksPath = filepath.Join(getRootPath(), path)
}

func getHooksPathFromGitConfig() string {
cmd := exec.Command("git", "rev-parse", "--git-path", "hooks")

outputBytes, err := cmd.CombinedOutput()
if err != nil {
panic(err)
}

return strings.TrimSpace(string(outputBytes))
}

func getSourceDir() string {
return filepath.Join(getRootPath(), viper.GetString(configSourceDirKey))
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ func deleteSourceDirs(fs afero.Fs) {

// DeleteGitHooks read the config and remove all git hooks except
func DeleteGitHooks(fs afero.Fs) {
hooksPath := filepath.Join(getRootPath(), ".git", "hooks")
hooksPath := getGitHooksPath()

hooks, err := afero.ReadDir(fs, hooksPath)
if (err != nil) {
log.Println("⚠️ ", au.Bold(".git/hooks"), "directory does not exist, creating")
log.Println("⚠️ ", au.Bold(hooksPath), "directory does not exist, creating")
if err := os.Mkdir(hooksPath, os.ModePerm); err != nil {
log.Println(au.Brown("🚨 Failed to create"), au.Bold(".git/hooks"), au.Brown("directory"))
log.Println(au.Brown("🚨 Failed to create"), au.Bold(hooksPath), au.Brown("directory"))
log.Fatal(err)
}
}
Expand All @@ -99,7 +99,7 @@ func revertOldGitHooks(fs afero.Fs) {
return
}

hooksPath := filepath.Join(getRootPath(), ".git", "hooks")
hooksPath := getGitHooksPath()
for _, file := range hookGroups {
hookFilePath := filepath.Join(hooksPath, file.Name()+".old")

Expand Down