Skip to content

Commit

Permalink
Reuse skip command option
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryTsepelev committed Apr 30, 2021
1 parent ef3ff7b commit 547793c
Showing 1 changed file with 87 additions and 67 deletions.
154 changes: 87 additions & 67 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,33 @@ var (
)

const (
rootConfigKey string = "root"
runnerConfigKey string = "runner"
runConfigKey string = "run" // alias for runner
runnerArgsConfigKey string = "runner_args"
scriptsConfigKey string = "scripts"
commandsConfigKey string = "commands"
includeConfigKey string = "include"
excludeConfigKey string = "exclude"
globConfigKey string = "glob"
skipConfigKey string = "skip"
skipEmptyConfigKey string = "skip_empty"
filesConfigKey string = "files"
colorsConfigKey string = "colors"
parallelConfigKey string = "parallel"
skipOutputConfigKey string = "skip_output"
outputMeta string = "meta"
outputSuccess string = "success"
subFiles string = "{files}"
subAllFiles string = "{all_files}"
subStagedFiles string = "{staged_files}"
pushFiles string = "{push_files}"
runnerWrapPattern string = "{cmd}"
tagsConfigKey string = "tags"
pipedConfigKey string = "piped"
excludeTagsConfigKey string = "exclude_tags"
skipGitStatesConfigKey string = "skip_git_states"
minVersionConfigKey string = "min_version"
execMode os.FileMode = 0751
rootConfigKey string = "root"
runnerConfigKey string = "runner"
runConfigKey string = "run" // alias for runner
runnerArgsConfigKey string = "runner_args"
scriptsConfigKey string = "scripts"
commandsConfigKey string = "commands"
includeConfigKey string = "include"
excludeConfigKey string = "exclude"
globConfigKey string = "glob"
skipConfigKey string = "skip"
skipEmptyConfigKey string = "skip_empty"
filesConfigKey string = "files"
colorsConfigKey string = "colors"
parallelConfigKey string = "parallel"
skipOutputConfigKey string = "skip_output"
outputMeta string = "meta"
outputSuccess string = "success"
subFiles string = "{files}"
subAllFiles string = "{all_files}"
subStagedFiles string = "{staged_files}"
pushFiles string = "{push_files}"
runnerWrapPattern string = "{cmd}"
tagsConfigKey string = "tags"
pipedConfigKey string = "piped"
excludeTagsConfigKey string = "exclude_tags"
minVersionConfigKey string = "min_version"
execMode os.FileMode = 0751
)

// runCmd represents the run command
Expand Down Expand Up @@ -153,19 +152,6 @@ func RunCmdExecutor(args []string, fs afero.Fs) error {
}
}

gitStates := getGitStatesToSkip(hooksGroup)
for _, gitState := range gitStates {
if (gitState == "merge" && isMergeInProgress()) {
log.Println(au.Cyan("SKIP:"), au.Brown("merge is in progress"))
return nil
}

if (gitState == "rebase" && isRebaseInProgress()) {
log.Println(au.Cyan("SKIP:"), au.Brown("rebase is in progress"))
return nil
}
}

commands := getCommands(hooksGroup)
if len(commands) != 0 {
for _, commandName := range commands {
Expand All @@ -190,6 +176,7 @@ func RunCmdExecutor(args []string, fs afero.Fs) error {
}

func executeCommand(hooksGroup, commandName string, wg *sync.WaitGroup, gitArgs []string) {
log.Println("executeCommand", hooksGroup)
defer wg.Done()

if getPiped(hooksGroup) && isPipeBroken {
Expand Down Expand Up @@ -248,6 +235,7 @@ func executeCommand(hooksGroup, commandName string, wg *sync.WaitGroup, gitArgs
}

if isSkipCommmand(hooksGroup, commandName) {
log.Println("skipping!!!!")
mutex.Lock()
spinner.RestartWithMsg(sprintSuccess("\n", au.Bold(commandName), au.Brown("(SKIP BY SETTINGS)")))
mutex.Unlock()
Expand Down Expand Up @@ -460,7 +448,64 @@ func isSkipScript(hooksGroup, executableName string) bool {

func isSkipCommmand(hooksGroup, executableName string) bool {
key := strings.Join([]string{hooksGroup, commandsConfigKey, executableName, skipConfigKey}, ".")
return viper.GetBool(key)
value := viper.Get(key)

switch typedValue := value.(type) {
case bool:
/*
pre-push:
commands:
packages-audit:
skip: true
*/
return typedValue
case string:
/*
pre-push:
commands:
packages-audit:
skip: merge
*/
return isSkippedGitState(typedValue)
case []interface{}:
/*
pre-push:
commands:
packages-audit:
skip:
- merge
- rebase
*/
for _, gitState := range typedValue {
if isSkippedGitState(gitState.(string)) {
return true
}
}
}

return false
}

func isSkippedGitState(state string) bool {
return state == "merge" && isMergeInProgress() || state == "rebase" && isRebaseInProgress()
}

func isMergeInProgress() bool {
if _, err := os.Stat(".git/MERGE_HEAD"); os.IsNotExist(err) {
return false
}

return true
}

func isRebaseInProgress() bool {
if _, mergeErr := os.Stat(".git/rebase-merge"); os.IsNotExist(mergeErr) {
if _, applyErr := os.Stat(".git/rebase-apply"); os.IsNotExist(applyErr) {
return false
}
}

return true
}

// NOTE: confusing option, suppose it unnesecary and should be deleted.
Expand Down Expand Up @@ -501,13 +546,6 @@ func getCommands(hooksGroup string) []string {
return keys
}

func getGitStatesToSkip(hooksGroup string) []string {
key := strings.Join([]string{hooksGroup, skipGitStatesConfigKey}, ".")
states := viper.GetStringSlice(key)

return states
}

func getRoot(hooksGroup string, executableName string) string {
key := strings.Join([]string{hooksGroup, commandsConfigKey, executableName, rootConfigKey}, ".")
return viper.GetString(key)
Expand Down Expand Up @@ -688,21 +726,3 @@ func isVersionOk() bool {

return true
}

func isMergeInProgress() bool {
if _, err := os.Stat(".git/MERGE_HEAD"); os.IsNotExist(err) {
return false
}

return true
}

func isRebaseInProgress() bool {
if _, mergeErr := os.Stat(".git/rebase-merge"); os.IsNotExist(mergeErr) {
if _, applyErr := os.Stat(".git/rebase-apply"); os.IsNotExist(applyErr) {
return false
}
}

return true
}

0 comments on commit 547793c

Please sign in to comment.