|
| 1 | +# shellcheck shell=bash |
| 2 | +# Version: 0.1.2 |
| 3 | + |
| 4 | +# @name Hookah lib.sh |
| 5 | +# @brief Hookah: An elegantly minimal solution for Git hooks |
| 6 | +# @description Hookah streamlines the process of managing Git hooks. This file is a |
| 7 | +# library of functions that can easily be used by hooks written in Bash. Use it by |
| 8 | +# prepending your script with the following |
| 9 | +# |
| 10 | +# ```bash |
| 11 | +# #!/usr/bin/env bash |
| 12 | +# |
| 13 | +# source "${0%/*}/.hookah/lib.sh" |
| 14 | +# hookah.init |
| 15 | +# ``` |
| 16 | +# |
| 17 | +# Learn more about it [on GitHub](https://github.com/hyperupcall/hookah) |
| 18 | + |
| 19 | +if [ -z "$BASH_VERSION" ]; then |
| 20 | + printf '%s\n' "Error: lib.sh: This script is only compatible with Bash. Exiting" >&2 |
| 21 | + exit 1 |
| 22 | +fi |
| 23 | + |
| 24 | +# @description Initiates the environment, sets up stacktrace printing on the 'ERR' trap, |
| 25 | +# and sets the directory to the root of the Git repository |
| 26 | +# @noargs |
| 27 | +hookah.init() { |
| 28 | + set -Eeo pipefail |
| 29 | + shopt -s dotglob extglob globasciiranges globstar lastpipe shift_verbose |
| 30 | + export LANG='C' LC_CTYPE='C' LC_NUMERIC='C' LC_TIME='C' LC_COLLATE='C' LC_MONETARY='C' \ |
| 31 | + LC_MESSAGES='C' LC_PAPER='C' LC_NAME='C' LC_ADDRESS='C' LC_TELEPHONE='C' \ |
| 32 | + LC_MEASUREMENT='C' LC_IDENTIFICATION='C' LC_ALL='C' |
| 33 | + trap '__hookah_trap_err' 'ERR' |
| 34 | + |
| 35 | + while [ ! -d '.git' ] && [ "$PWD" != / ]; do |
| 36 | + if ! cd ..; then |
| 37 | + __hookah_die "Failed to cd to nearest Git repository" |
| 38 | + fi |
| 39 | + done |
| 40 | + if [ "$PWD" = / ]; then |
| 41 | + __hookah_die "Failed to cd to nearest Git repository" |
| 42 | + fi |
| 43 | + |
| 44 | + # Prevent any possibilities of 'stdin in is not a tty' |
| 45 | + if ! exec </dev/tty; then |
| 46 | + __hookah_die "Failed to redirect tty to standard input" |
| 47 | + fi |
| 48 | + |
| 49 | + __hookah_print "Running ${BASH_SOURCE[1]##*/}" |
| 50 | + |
| 51 | +} |
| 52 | + |
| 53 | +# @description Prints a command before running it |
| 54 | +# #args $@ Command to execute |
| 55 | +hookah.run() { |
| 56 | + printf '%s\n' "Hookah: Running command: '$*'" |
| 57 | + "$@" |
| 58 | +} |
| 59 | + |
| 60 | +# @description Prints a command before running it. But, if the command fails, do not abort execution |
| 61 | +# @args $@ Command to execute |
| 62 | +hookah.run_allow_fail() { |
| 63 | + if ! hookah.run "$@"; then |
| 64 | + printf '%s\n' "Hookah: Command failed" |
| 65 | + fi |
| 66 | +} |
| 67 | + |
| 68 | +# @description Scans environment variables to determine if script is in a CI environment |
| 69 | +# @exitcode 0 If in CI |
| 70 | +# @exitcode 1 If not in CI |
| 71 | +# @set REPLY Current provider for CI |
| 72 | +hookah.is_ci() { |
| 73 | + unset -v REPLY; REPLY= |
| 74 | + |
| 75 | + if [[ -v 'APPVEYOR' ]]; then |
| 76 | + REPLY='AppVeyor' |
| 77 | + elif [[ -v 'SYSTEM_TEAMFOUNDATIONCOLLECTIONURI' ]]; then |
| 78 | + REPLY='Azure Pipelines' |
| 79 | + elif [[ -v 'AC_APPCIRCLE' ]]; then |
| 80 | + REPLY='Appcircle' |
| 81 | + elif [[ -v 'bamboo_planKey' ]]; then |
| 82 | + REPLY='Bamboo' |
| 83 | + elif [[ -v 'BITBUCKET_COMMIT' ]]; then |
| 84 | + REPLY='Bitbucket Pipelines' |
| 85 | + elif [[ -v 'BITRISE_IO' ]]; then |
| 86 | + REPLY='Bitrise' |
| 87 | + elif [[ -v 'BUDDY_WORKSPACE_ID' ]]; then |
| 88 | + REPLY='Buddy' |
| 89 | + elif [[ -v 'BUILDKITE' ]]; then |
| 90 | + REPLY='Buildkite' |
| 91 | + elif [[ -v 'CIRCLECI' ]]; then |
| 92 | + REPLY='CircleCI' |
| 93 | + elif [[ -v 'CIRRUS_CI' ]]; then |
| 94 | + REPLY='Cirrus CI' |
| 95 | + elif [[ -v 'CODEBUILD_BUILD_ARN' ]]; then |
| 96 | + REPLY='AWS CodeBuild' |
| 97 | + elif [[ -v 'CF_BUILD_ID' ]]; then |
| 98 | + REPLY='Codefresh' |
| 99 | + elif [[ -v '[object Object]' ]]; then |
| 100 | + REPLY='Codeship' |
| 101 | + elif [[ -v 'DRONE' ]]; then |
| 102 | + REPLY='Drone' |
| 103 | + elif [[ -v 'DSARI' ]]; then |
| 104 | + REPLY='dsari' |
| 105 | + elif [[ -v 'EAS_BUILD' ]]; then |
| 106 | + REPLY='Expo Application Services' |
| 107 | + elif [[ -v 'GITHUB_ACTIONS' ]]; then |
| 108 | + REPLY='GitHub Actions' |
| 109 | + elif [[ -v 'GITLAB_CI' ]]; then |
| 110 | + REPLY='GitLab CI' |
| 111 | + elif [[ -v 'GO_PIPELINE_LABEL' ]]; then |
| 112 | + REPLY='GoCD' |
| 113 | + elif [[ -v 'LAYERCI' ]]; then |
| 114 | + REPLY='LayerCI' |
| 115 | + elif [[ -v 'HUDSON_URL' ]]; then |
| 116 | + REPLY='Hudson' |
| 117 | + elif [[ -v 'JENKINS_URL,BUILD_ID' ]]; then |
| 118 | + REPLY='Jenkins' |
| 119 | + elif [[ -v 'MAGNUM' ]]; then |
| 120 | + REPLY='Magnum CI' |
| 121 | + elif [[ -v 'NETLIFY' ]]; then |
| 122 | + REPLY='Netlify CI' |
| 123 | + elif [[ -v 'NEVERCODE' ]]; then |
| 124 | + REPLY='Nevercode' |
| 125 | + elif [[ -v 'RENDER' ]]; then |
| 126 | + REPLY='Render' |
| 127 | + elif [[ -v 'SAILCI' ]]; then |
| 128 | + REPLY='Sail CI' |
| 129 | + elif [[ -v 'SEMAPHORE' ]]; then |
| 130 | + REPLY='Semaphore' |
| 131 | + elif [[ -v 'SCREWDRIVER' ]]; then |
| 132 | + REPLY='Screwdriver' |
| 133 | + elif [[ -v 'SHIPPABLE' ]]; then |
| 134 | + REPLY='Shippable' |
| 135 | + elif [[ -v 'TDDIUM' ]]; then |
| 136 | + REPLY='Solano CI' |
| 137 | + elif [[ -v 'STRIDER' ]]; then |
| 138 | + REPLY='Strider CD' |
| 139 | + elif [[ -v 'TASK_ID,RUN_ID' ]]; then |
| 140 | + REPLY='TaskCluster' |
| 141 | + elif [[ -v 'TEAMCITY_VERSION' ]]; then |
| 142 | + REPLY='TeamCity' |
| 143 | + elif [[ -v 'TRAVIS' ]]; then |
| 144 | + REPLY='Travis CI' |
| 145 | + elif [[ -v 'NOW_BUILDER' ]]; then |
| 146 | + REPLY='Vercel' |
| 147 | + elif [[ -v 'APPCENTER_BUILD_ID' ]]; then |
| 148 | + REPLY='Visual Studio App Center' |
| 149 | + else |
| 150 | + return 1 |
| 151 | + fi |
| 152 | +} |
| 153 | + |
| 154 | +# @description Test whether color should be outputed |
| 155 | +# @exitcode 0 if should print color |
| 156 | +# @exitcode 1 if should not print color |
| 157 | +# @internal |
| 158 | +__hookah_is_color() { |
| 159 | + if [[ -v NO_COLOR || $TERM == dumb ]]; then |
| 160 | + return 1 |
| 161 | + else |
| 162 | + return 0 |
| 163 | + fi |
| 164 | +} |
| 165 | + |
| 166 | +# @internal |
| 167 | +__hookah_internal_error() { |
| 168 | + printf '%s\n' "Internal Error: $1" >&2 |
| 169 | +} |
| 170 | + |
| 171 | +# @internal |
| 172 | +__hookah_die() { |
| 173 | + __hookah_error "$1" |
| 174 | + exit 1 |
| 175 | +} |
| 176 | + |
| 177 | +# @internal |
| 178 | +__hookah_error() { |
| 179 | + printf '%s\n' "Hookah: lib.sh: Error: $1. Exiting" |
| 180 | + exit 1 |
| 181 | +} |
| 182 | + |
| 183 | +# @internal |
| 184 | +__hookah_print() { |
| 185 | + printf '%s\n' "Hookah: $1" |
| 186 | +} |
| 187 | + |
| 188 | +# @internal |
| 189 | +__hookah_trap_err() { |
| 190 | + local error_code=$? |
| 191 | + |
| 192 | + __hookah_internal_error "Your hook did not exit successfully" |
| 193 | + __hookah_print_stacktrace |
| 194 | + |
| 195 | + exit $error_code |
| 196 | +} >&2 |
| 197 | + |
| 198 | +# @internal |
| 199 | +__hookah_print_stacktrace() { |
| 200 | + if __hookah_is_color; then |
| 201 | + printf '\033[4m%s\033[0m\n' 'Stacktrace:' |
| 202 | + else |
| 203 | + printf '%s\n' 'Stacktrace:' |
| 204 | + fi |
| 205 | + |
| 206 | + local i= |
| 207 | + for ((i=0; i<${#FUNCNAME[@]}-1; ++i)); do |
| 208 | + local __bash_source="${BASH_SOURCE[$i]}"; __bash_source=${__bash_source##*/} |
| 209 | + printf '%s\n' " in ${FUNCNAME[$i]} ($__bash_source:${BASH_LINENO[$i-1]})" |
| 210 | + done; unset -v i __bash_source |
| 211 | +} >&2 |
0 commit comments