Skip to content

Instantly share code, notes, and snippets.

@iurysza
Created February 1, 2021 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iurysza/d12837fe6cc59fb364844987a5cff10d to your computer and use it in GitHub Desktop.
Save iurysza/d12837fe6cc59fb364844987a5cff10d to your computer and use it in GitHub Desktop.
Install git hooks
static def isLinuxOrMacOs() {
def osName = System.getProperty('os.name').toLowerCase(Locale.ROOT)
return osName.contains('linux') || osName.contains('mac os') || osName.contains('macos')
}
task copyGitHooks(type: Copy) {
description 'Copies the git hooks from team-props/git-hooks to the .git folder.'
from("${rootDir}/team-props/git-hooks/") {
include '**/*.sh'
rename '(.*).sh', '$1'
}
into "${rootDir}/.git/hooks"
onlyIf { isLinuxOrMacOs() }
}
task installGitHooks(type: Exec) {
description 'Installs the pre-commit git hooks from team-props/git-hooks.'
group 'git hooks'
workingDir rootDir
commandLine 'chmod'
args '-R', '+x', '.git/hooks/'
dependsOn copyGitHooks
onlyIf { isLinuxOrMacOs() }
doLast { logger.info('Git hook installed successfully.') }
}
afterEvaluate {
// We install the hook at the first occasion
tasks['clean'].dependsOn installGitHooks
tasks['assemble'].dependsOn installGitHooks
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment