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

Add stage_fixed flag for hooks #109

Closed
Closed
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
2 changes: 1 addition & 1 deletion cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestInstallCmdExecutor(t *testing.T) {
"prepare-commit-msg",
}

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

actualFiles := []string{}
Expand Down
3 changes: 3 additions & 0 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,18 @@ func AddConfigYaml(fs afero.Fs) {
# commands:
# eslint:
# glob: "*.{js,ts}"
# stage_fixed: true
# run: yarn eslint {staged_files}
# rubocop:
# tags: backend style
# glob: "*.rb"
# exclude: "application.rb|routes.rb"
# stage_fixed: false
# run: bundle exec rubocop --force-exclusion {all_files}
# govet:
# tags: backend style
# files: git ls-files -m
# stage_fixed: true
# glob: "*.go"
# run: go vet {files}
# scripts:
Expand Down
10 changes: 5 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ const (
)

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

au aurora.Aurora
Expand Down
37 changes: 33 additions & 4 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -63,6 +64,7 @@ const (
pipedConfigKey string = "piped"
excludeTagsConfigKey string = "exclude_tags"
minVersionConfigKey string = "min_version"
stageFixedConfigKey string = "stage_fixed"
execMode os.FileMode = 0751
)

Expand Down Expand Up @@ -247,21 +249,37 @@ func executeCommand(hooksGroup, commandName string, wg *sync.WaitGroup) {

log.Println(au.Cyan("\n EXECUTE >"), au.Bold(commandName))
if err != nil {
failList = append(failList, commandName)
setPipeBroken()
log.Println(err)
processError(commandName, err)
return
}

// pty part start
defer func() { ptyOut.Close() }() // Make sure to close the pty at the end.
// Copy stdin to the pty and the pty to stdout.
go func() { io.Copy(ptyOut, os.Stdin) }()
io.Copy(os.Stdout, ptyOut)
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

_, err = io.Copy(os.Stdout, ptyOut)
if err != nil {
processError(commandName, err)
return
}

w.Close()
fileNames, _ := ioutil.ReadAll(r)
os.Stdout = rescueStdout
log.Println(string(fileNames))
// pty part end

if command.Wait() == nil {
okList = append(okList, commandName)
if isStageFixedFiles(hooksGroup, commandName) {
log.Println("\n", au.Bold(commandName), au.Brown("(STAGE FIXED FILES)"))
log.Println(fmt.Sprintf("%#v", string(fileNames)))
context.ExecGitCommand(fmt.Sprintf("git add %v", strings.Replace(string(fileNames), "\r\n", " ", -1)))
}
} else {
failList = append(failList, commandName)
setPipeBroken()
Expand Down Expand Up @@ -405,6 +423,11 @@ func isSkipCommmand(hooksGroup, executableName string) bool {
return viper.GetBool(key)
}

func isStageFixedFiles(hooksGroup, executableName string) bool {
key := strings.Join([]string{hooksGroup, commandsConfigKey, executableName, stageFixedConfigKey}, ".")
return viper.GetBool(key)
}

// NOTE: confusing option, suppose it unnesecary and should be deleted.
func isSkipEmptyCommmand(hooksGroup, executableName string) bool {
key := strings.Join([]string{hooksGroup, commandsConfigKey, executableName, skipEmptyConfigKey}, ".")
Expand Down Expand Up @@ -613,3 +636,9 @@ func isVersionOk() bool {

return true
}

func processError(commandName string, err error) {
failList = append(failList, commandName)
setPipeBroken()
log.Println(err)
}
25 changes: 21 additions & 4 deletions cmd/run_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const (
pipedConfigKey string = "piped"
excludeTagsConfigKey string = "exclude_tags"
execMode os.FileMode = 0751
stageFixedConfigKey string = "stage_fixed"
)

// runCmd represents the run command
Expand Down Expand Up @@ -245,18 +246,23 @@ func executeCommand(hooksGroup, commandName string, wg *sync.WaitGroup) {

log.Println(au.Cyan("\n EXECUTE >"), au.Bold(commandName))
if err != nil {
failList = append(failList, commandName)
setPipeBroken()
log.Println(err)
processError(commandName, err)
return
}
fileNames, err := command.Output()
if err != nil {
processError(commandName, err)
return
}

// io.Copy(os.Stdout, ptyOut) // win specific
if command.Wait() == nil {
okList = append(okList, commandName)
context.ExecGitCommand(fmt.Sprintf("git add %v", strings.Replace(string(fileNames), "\r\n", " ", -1)))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like output logging is inconsistent with bash command.


} else {
failList = append(failList, commandName)
setPipeBroken()

}
}

Expand Down Expand Up @@ -384,6 +390,11 @@ func isScriptExist(hooksGroup, executableName string) bool {
return viper.IsSet(key)
}

func isStageFixedFiles(hooksGroup, executableName string) bool {
key := strings.Join([]string{hooksGroup, commandsConfigKey, executableName, stageFixedConfigKey}, ".")
return viper.GetBool(key)
}

func isSkipScript(hooksGroup, executableName string) bool {
key := strings.Join([]string{hooksGroup, scriptsConfigKey, executableName, skipConfigKey}, ".")
return viper.GetBool(key)
Expand Down Expand Up @@ -576,3 +587,9 @@ func makeExecutable(path string) {
log.Fatal(err)
}
}

func processError(commandName string, err error) {
failList = append(failList, commandName)
setPipeBroken()
log.Println(err)
}