Skip to content

Commit

Permalink
#105464 new git-hooks generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Koryukov committed Apr 15, 2023
1 parent 32623f9 commit 9529430
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 32 deletions.
21 changes: 1 addition & 20 deletions actions/component_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"github.com/madridianfox/elc/core"
"path"
)

func resolveCompNames(ws *core.Workspace, options *core.GlobalOptions, namesFromArgs []string) ([]string, error) {
Expand Down Expand Up @@ -327,28 +326,10 @@ func RunAction(options *core.GlobalOptions) error {
}

func SetGitHooksAction(scriptsFolder string, elcBinary string) error {
folders, err := core.Pc.ReadDir(scriptsFolder)
err := core.GenerateHookScripts(elcBinary, scriptsFolder)
if err != nil {
return err
}
for _, folder := range folders {
if !folder.IsDir() {
continue
}
files, err := core.Pc.ReadDir(path.Join(scriptsFolder, folder.Name()))
if err != nil {
return err
}
hookScripts := make([]string, 0)
for _, file := range files {
hookScripts = append(hookScripts, path.Join(scriptsFolder, folder.Name(), file.Name()))
}
script := core.GenerateHookScript(hookScripts, elcBinary)
err = core.Pc.WriteFile(fmt.Sprintf(".git/hooks/%s", folder.Name()), []byte(script), 0755)
if err != nil {
return err
}
}

return nil
}
Expand Down
12 changes: 0 additions & 12 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,3 @@ func substVars(expr string, ctx *Context) (string, error) {

return expr, nil
}

func GenerateHookScript(scripts []string, elcBinary string) string {
result := make([]string, 0)
result = append(result, "#!/bin/bash")
result = append(result, "set -e")
result = append(result, `printf "\x1b[0;34m%s\x1b[39;49;00m\n" "Run hook in ELC"`)
for _, script := range scripts {
result = append(result, fmt.Sprintf("%s --mode=hook --no-tty %s", elcBinary, script))
}

return strings.Join(result, "\n")
}
87 changes: 87 additions & 0 deletions core/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package core

import (
"fmt"
)

var hookNames = []string{
"applypatch-msg",
"pre-applypatch",
"post-applypatch",
"pre-commit",
"pre-merge-commit",
"prepare-commit-msg",
"commit-msg",
"post-commit",
"pre-rebase",
"post-checkout",
"post-merge",
"pre-push",
"pre-receive",
"update",
"proc-receive",
"post-receive",
"post-update",
"reference-transaction",
"push-to-checkout",
"pre-auto-gc",
"post-rewrite",
"sendemail-validate",
"fsmonitor-watchman",
"p4-changelist",
"p4-prepare-changelist",
"p4-post-changelist",
"p4-pre-submit",
"post-index-change",
}

var hookScript = `#!/bin/bash
set -e
ELC_BINARY="%s"
HOOKS_FOLDER="%s"
HOOK_NAME="%s"
if command -v $ELC_BINARY &> /dev/null; then
$ELC_BINARY --mode=hook --no-tty $0
else
for script in ./$HOOKS_FOLDER/$HOOK_NAME/* ; do
if [ -f $script ]; then
$script
fi
done
fi
`

func GenerateHookScripts(elcBinary string, hooksFolder string) error {
for _, hookName := range hookNames {
scriptContent := fmt.Sprintf(hookScript, elcBinary, hooksFolder, hookName)
scriptPath := fmt.Sprintf(".git/hooks/%s", hookName)

if Pc.FileExists(".git/hooks") == false {
err := Pc.CreateDir(".git/hooks")
if err != nil {
return err
}
}

if Pc.FileExists(scriptPath) == false {
err := Pc.CreateFile(scriptPath)
if err != nil {
return err
}

err = Pc.Chmod(scriptPath, 0775)
if err != nil {
return err
}
}

err := Pc.WriteFile(scriptPath, []byte(scriptContent), 0775)
if err != nil {
return err
}
}

return nil
}
42 changes: 42 additions & 0 deletions core/mock_pc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions core/pc.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ type PC interface {
FileExists(filepath string) bool
ReadFile(filename string) ([]byte, error)
ReadDir(dirname string) ([]os.FileInfo, error)
CreateFile(filename string) error
Chmod(filename string, mode os.FileMode) error
CreateDir(path string) error
WriteFile(filename string, data []byte, perm os.FileMode) error
Printf(format string, a ...interface{}) (n int, err error)
Println(a ...interface{}) (n int, err error)
Expand Down Expand Up @@ -94,6 +97,24 @@ func (r *RealPC) ReadDir(dirname string) ([]os.FileInfo, error) {
return ioutil.ReadDir(dirname)
}

func (r *RealPC) CreateFile(filename string) error {
_, err := os.Create(filename)

return err
}

func (r *RealPC) Chmod(filename string, mode os.FileMode) error {
err := os.Chmod(filename, mode)

return err
}

func (r *RealPC) CreateDir(path string) error {
err := os.Mkdir(path, 0755)

return err
}

func (r *RealPC) WriteFile(filename string, data []byte, perm os.FileMode) error {
return ioutil.WriteFile(filename, data, perm)
}
Expand Down

0 comments on commit 9529430

Please sign in to comment.