feat(scripts): support stdin/out instead of message file
This commit is contained in:
parent
64f54294dc
commit
cb6c04708b
|
@ -5,6 +5,7 @@
|
||||||
# branch name. Can be used as a prepare-commit-msg hook.
|
# branch name. Can be used as a prepare-commit-msg hook.
|
||||||
#
|
#
|
||||||
# TODO: support globs in aliases
|
# TODO: support globs in aliases
|
||||||
|
# TODO: combine shared prefixes (src/foo/bar and src/foo/bin share src/foo)
|
||||||
###
|
###
|
||||||
|
|
||||||
declare -A SCOPE_ALIAS
|
declare -A SCOPE_ALIAS
|
||||||
|
@ -12,19 +13,17 @@ SCOPE_ALIAS=(
|
||||||
['README.md']='docs' # with extension matches raw filename, pre-filter
|
['README.md']='docs' # with extension matches raw filename, pre-filter
|
||||||
['README']='docs' # without extension matches subdir or filename post-filter
|
['README']='docs' # without extension matches subdir or filename post-filter
|
||||||
|
|
||||||
|
['.codeclimate.yml']='build'
|
||||||
|
['.eslintrc.json']='build'
|
||||||
|
['.github']='build'
|
||||||
['.gitlab']='build'
|
['.gitlab']='build'
|
||||||
['.gitlab-ci.yml']='build'
|
['.gitlab-ci.yml']='build'
|
||||||
|
['.mdlrc']='build'
|
||||||
|
['.npmignore']='build'
|
||||||
|
['.npmrc']='build'
|
||||||
['Makefile']='build'
|
['Makefile']='build'
|
||||||
|
['renovate.json']='build'
|
||||||
['.codeclimate.yml']='config'
|
['tsconfig.json']='build'
|
||||||
['.dockerignore']='config'
|
|
||||||
['.eslintrc.json']='config'
|
|
||||||
['.github']='config'
|
|
||||||
['.mdlrc']='config'
|
|
||||||
['.npmignore']='config'
|
|
||||||
['.npmrc']='config'
|
|
||||||
['renovate.json']='config'
|
|
||||||
['tsconfig.json']='config'
|
|
||||||
|
|
||||||
['package.json']='deps'
|
['package.json']='deps'
|
||||||
['yarn.lock']='deps'
|
['yarn.lock']='deps'
|
||||||
|
@ -32,19 +31,29 @@ SCOPE_ALIAS=(
|
||||||
|
|
||||||
['LICENSE.md']='docs'
|
['LICENSE.md']='docs'
|
||||||
|
|
||||||
|
['.dockerignore']='image'
|
||||||
['Dockerfile.alpine']='image'
|
['Dockerfile.alpine']='image'
|
||||||
['Dockerfile.stretch']='image'
|
['Dockerfile.stretch']='image'
|
||||||
)
|
)
|
||||||
|
|
||||||
SCOPE_ALLOW=(
|
SCOPE_ALLOW=(
|
||||||
|
# aliases
|
||||||
'build'
|
'build'
|
||||||
'config'
|
|
||||||
'deps'
|
'deps'
|
||||||
'docs'
|
|
||||||
'image'
|
'image'
|
||||||
'src'
|
# dirs
|
||||||
|
'docs'
|
||||||
|
'rules'
|
||||||
'scripts'
|
'scripts'
|
||||||
'test'
|
'test'
|
||||||
|
# src subdirs
|
||||||
|
'config'
|
||||||
|
'parser'
|
||||||
|
'reporter'
|
||||||
|
'rule'
|
||||||
|
'visitor'
|
||||||
|
# misc
|
||||||
|
'lint'
|
||||||
)
|
)
|
||||||
|
|
||||||
function filter_scope() {
|
function filter_scope() {
|
||||||
|
@ -89,9 +98,12 @@ function head_path() {
|
||||||
parts=( $@ ) # Deliberately unquoted
|
parts=( $@ ) # Deliberately unquoted
|
||||||
set +f
|
set +f
|
||||||
|
|
||||||
if [[ ${#parts[@]} -gt 2 ]];
|
if [[ ${#parts[@]} -gt 3 ]];
|
||||||
then
|
then
|
||||||
printf '%s/' "${parts[@]:0:2}"
|
printf '%s/' "${parts[@]:1:2}"
|
||||||
|
elif [[ ${#parts[@]} -gt 2 ]];
|
||||||
|
then
|
||||||
|
printf '%s/' "${parts[@]:1:1}"
|
||||||
elif [[ ${#parts[@]} -gt 1 ]];
|
elif [[ ${#parts[@]} -gt 1 ]];
|
||||||
then
|
then
|
||||||
printf '%s/' "${parts[@]:0:1}"
|
printf '%s/' "${parts[@]:0:1}"
|
||||||
|
@ -101,26 +113,34 @@ function head_path() {
|
||||||
}
|
}
|
||||||
|
|
||||||
MESSAGE_FILE="$1"
|
MESSAGE_FILE="$1"
|
||||||
MESSAGE_TYPE="$2"
|
MESSAGE_SOURCE="$2"
|
||||||
|
|
||||||
debug_log "$(printf 'message file: %s\n' "$MESSAGE_FILE")"
|
debug_log "$(printf 'message file: %s\n' "$MESSAGE_FILE")"
|
||||||
|
|
||||||
DEFAULT_MESSAGE="$(cat ${MESSAGE_FILE})"
|
MESSAGE_BODY=""
|
||||||
DEFAULT_TYPE=""
|
MESSAGE_TYPE=""
|
||||||
|
|
||||||
|
if [[ "${MESSAGE_FILE}" == "-" ]];
|
||||||
|
then
|
||||||
|
echo -n 'message body: '
|
||||||
|
read -e MESSAGE_BODY
|
||||||
|
else
|
||||||
|
MESSAGE_BODY="$(cat ${MESSAGE_FILE})"
|
||||||
|
fi
|
||||||
|
|
||||||
# split up default message into segments
|
# split up default message into segments
|
||||||
if [[ "${DEFAULT_MESSAGE}" =~ [a-z]+\([a-z\/]+\)\:[\ ]+[-a-zA-Z0-9\.\(\)]+ ]];
|
if [[ "${MESSAGE_BODY}" =~ [a-z]+\([a-z\/]+\)\:[\ ]+[-a-zA-Z0-9\.\(\)]+ ]];
|
||||||
then
|
then
|
||||||
debug_log "default message is already conventional"
|
debug_log "default message is already conventional"
|
||||||
exit 0
|
exit 0
|
||||||
elif [[ "${DEFAULT_MESSAGE}" =~ [a-z]+(\(\))*\:[\ ]+[-a-zA-Z0-9\.\(\)]+ ]];
|
elif [[ "${MESSAGE_BODY}" =~ [a-z]+(\(\))*\:[\ ]+[-a-zA-Z0-9\.\(\)]+ ]];
|
||||||
then
|
then
|
||||||
debug_log "default message is missing scope"
|
debug_log "default message is missing scope"
|
||||||
DEFAULT_TYPE="$(echo "${DEFAULT_MESSAGE}" | sed 's/:.*$//' | sed 's/()//')"
|
MESSAGE_TYPE="$(echo "${MESSAGE_BODY}" | sed 's/:.*$//' | sed 's/()//')"
|
||||||
DEFAULT_MESSAGE="$(echo "${DEFAULT_MESSAGE}" | sed 's/^.*://' | sed 's/^[ ]*//')"
|
MESSAGE_BODY="$(echo "${DEFAULT_MESSAGE}" | sed 's/^.*://' | sed 's/^[ ]*//')"
|
||||||
|
|
||||||
debug_log "default type: ${DEFAULT_TYPE}"
|
debug_log "default type: ${MESSAGE_TYPE}"
|
||||||
debug_log "default message: ${DEFAULT_MESSAGE}"
|
debug_log "default message: ${MESSAGE_BODY}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# git ls-files -m for modified but unstaged
|
# git ls-files -m for modified but unstaged
|
||||||
|
@ -142,6 +162,7 @@ do
|
||||||
# reduce filenames to <= 2 segments
|
# reduce filenames to <= 2 segments
|
||||||
path="$(head_path "$file")"
|
path="$(head_path "$file")"
|
||||||
path="${path%/}"
|
path="${path%/}"
|
||||||
|
debug_log "$(printf 'prefile: %s\n' "$path")"
|
||||||
|
|
||||||
# post-filter truncated paths
|
# post-filter truncated paths
|
||||||
path="$(filter_scope "$path")"
|
path="$(filter_scope "$path")"
|
||||||
|
@ -161,7 +182,7 @@ debug_log "$(printf 'unique scopes: %s\n' "${UNIQUE_SCOPES[@]}")"
|
||||||
GIT_BRANCH="$(git rev-parse --abbrev-ref HEAD)"
|
GIT_BRANCH="$(git rev-parse --abbrev-ref HEAD)"
|
||||||
GIT_PREFIX="$(printf '%s\n' "${GIT_BRANCH}" | sed 's:/.*$::g')"
|
GIT_PREFIX="$(printf '%s\n' "${GIT_BRANCH}" | sed 's:/.*$::g')"
|
||||||
|
|
||||||
COMMIT_TYPE="${DEFAULT_TYPE:-${GIT_PREFIX}}"
|
COMMIT_TYPE="${MESSAGE_TYPE:-${GIT_PREFIX}}"
|
||||||
COMMIT_MESSAGE=""
|
COMMIT_MESSAGE=""
|
||||||
|
|
||||||
debug_log "branch: $GIT_BRANCH"
|
debug_log "branch: $GIT_BRANCH"
|
||||||
|
@ -170,18 +191,23 @@ debug_log "prefix: $COMMIT_TYPE"
|
||||||
if [[ ${#UNIQUE_SCOPES[@]} -gt 1 ]];
|
if [[ ${#UNIQUE_SCOPES[@]} -gt 1 ]];
|
||||||
then
|
then
|
||||||
debug_log "many scopes"
|
debug_log "many scopes"
|
||||||
COMMIT_MESSAGE="${COMMIT_TYPE}: ${DEFAULT_MESSAGE}"
|
COMMIT_MESSAGE="${COMMIT_TYPE}: ${MESSAGE_BODY}"
|
||||||
else
|
else
|
||||||
if [[ -z "${UNIQUE_SCOPES[0]}" ]];
|
if [[ -z "${UNIQUE_SCOPES[0]}" ]];
|
||||||
then
|
then
|
||||||
debug_log "empty scope"
|
debug_log "empty scope"
|
||||||
COMMIT_MESSAGE="${COMMIT_TYPE}(???): ${DEFAULT_MESSAGE}"
|
COMMIT_MESSAGE="${COMMIT_TYPE}(???): ${MESSAGE_BODY}"
|
||||||
else
|
else
|
||||||
debug_log "single scope"
|
debug_log "single scope"
|
||||||
COMMIT_MESSAGE="${COMMIT_TYPE}(${UNIQUE_SCOPES[0]}): ${DEFAULT_MESSAGE}"
|
COMMIT_MESSAGE="${COMMIT_TYPE}(${UNIQUE_SCOPES[0]}): ${MESSAGE_BODY}"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
debug_log "message: $COMMIT_MESSAGE"
|
debug_log "message: $COMMIT_MESSAGE"
|
||||||
|
|
||||||
echo "${COMMIT_MESSAGE}" > "${MESSAGE_FILE}"
|
if [[ "${MESSAGE_FILE}" == "-" ]];
|
||||||
|
then
|
||||||
|
echo "${COMMIT_MESSAGE}" > /dev/stdout
|
||||||
|
else
|
||||||
|
echo "${COMMIT_MESSAGE}" > "${MESSAGE_FILE}"
|
||||||
|
fi
|
||||||
|
|
Loading…
Reference in New Issue