Skip to content

Commit

Permalink
fix: make config variables local
Browse files Browse the repository at this point in the history
  • Loading branch information
rockandska committed Nov 20, 2021
1 parent 6f4403f commit 525fddf
Showing 1 changed file with 65 additions and 37 deletions.
102 changes: 65 additions & 37 deletions git-changelog
Expand Up @@ -35,22 +35,29 @@ set_user_config() {
repo_conf=$(<"${GIT_TOP_LEVEL}/.git-changelog")
fi
source /dev/stdin <<-EOF
set_tpl_vars() {
: \${hash:=}
: \${tag:=}
: \${title:=}
: \${type:=}
: \${scope:=}
show_scope=
show_body=
header_tpl=('%s\n\n' 'CHANGELOG')
release_tpl=('%s\n\n' "\${tag}")
unreleased_tpl=('%s\n\n' 'Unreleased')
type_tpl=('%s\n\n' "\${type}")
scope_tpl=('%s\n\n' "\${scope}")
commit_tpl=('- %s (%.7s)\n' "\${title}" "\${hash}")
get_var() {
local var2get="\${1:-}"
local var2return="\${2:-}"
local -r hash="\${hash:-}"
local -r tag="\${tag:-}"
local -r title="\${title:-}"
local -r "type=\${type:-}"
local -r "scope=\${scope:-}"
local show_scope=
local show_body=
local header_tpl=('%s\n\n' 'CHANGELOG')
local release_tpl=('%s\n\n' "\${tag}")
local unreleased_tpl=('%s\n\n' 'Unreleased')
local type_tpl=('%s\n\n' "\${type}")
local scope_tpl=('%s\n\n' "\${scope}")
local commit_tpl=('- %s (%.7s)\n' "\${title}" "\${hash}")
declare -A commit_type_traduction
declare -A commit_scope_traduction
${user_conf:-}
${repo_conf:-}
eval \$(declare -p "\$var2get" 2> /dev/null | sed "s/declare .. \$var2get=/\$var2return="/)
}
EOF
}
Expand Down Expand Up @@ -113,17 +120,24 @@ set_commit_infos() {
set_changelog_release() {
debug "Writing changelog.."
local tag="${GIT_ACTUAL_TAG}"
local show_scope
local show_body
declare -A commit_type_traduction
declare -A commit_scope_traduction
set_tpl_vars
local _show_scope
get_var show_scope _show_scope
local _show_body
get_var show_body _show_body
local _unreleased_tpl
get_var unreleased_tpl _unreleased_tpl
local _release_tpl
get_var release_tpl _release_tpl
declare -A _commit_type_traduction
get_var commit_type_traduction _commit_type_traduction
declare -A _commit_scope_traduction
get_var commit_scope_traduction _commit_scope_traduction
if [[ -n "${tag:-}" ]];then
# shellcheck disable=SC2059,SC2154
printf -v output -- "${output:-}## ${release_tpl[0]}" "${release_tpl[@]:1}"
printf -v output -- "${output:-}## ${_release_tpl[0]}" "${_release_tpl[@]:1}"
else
# shellcheck disable=SC2059,SC2154
printf -v output -- "${output:-}## ${unreleased_tpl[0]}" "${unreleased_tpl[@]:1}"
printf -v output -- "${output:-}## ${_unreleased_tpl[0]}" "${_unreleased_tpl[@]:1}"
fi

local type
Expand All @@ -134,11 +148,12 @@ set_changelog_release() {
IFS=" " read -r -a commit_list <<< "${COMMIT_TYPE_LIST[$type]}"
debug "commits with type : $type" "${commit_list[@]}"
# traduction
type="${commit_type_traduction["$type"]:-$type}"
set_tpl_vars
type="${_commit_type_traduction["$type"]:-$type}"
local _type_tpl
get_var type_tpl _type_tpl
# shellcheck disable=SC2059,SC2154
printf -v output -- "${output}### ${type_tpl[0]}" "${type_tpl[@]:1}"
if [ -n "${show_scope}" ];then
printf -v output -- "${output}### ${_type_tpl[0]}" "${_type_tpl[@]:1}"
if [ -n "${_show_scope}" ];then
while IFS= read -r scope;do
local scope_commits=()
for commit in "${commit_list[@]}";do
Expand All @@ -149,10 +164,10 @@ set_changelog_release() {
debug "commits with scope : $scope" "${scope_commits[@]}"
if [ "${#scope_commits[@]}" -gt 0 ];then
# traduction
scope="${commit_scope_traduction["$scope"]:-$scope}"
set_tpl_vars
scope="${_commit_scope_traduction["$scope"]:-$scope}"
get_var scope_tpl _scope_tpl
# shellcheck disable=SC2059,SC2154
printf -v output -- "${output}#### ${scope_tpl[0]}" "${scope_tpl[@]:1}"
printf -v output -- "${output}#### ${_scope_tpl[0]}" "${_scope_tpl[@]:1}"
set_changelog_release_commits "${scope_commits[@]}"
fi
done < <(printf '%s\n' "${!COMMIT_SCOPE_LIST[@]}" | LC_ALL=C sort)
Expand All @@ -167,18 +182,22 @@ set_changelog_release() {

set_changelog_release_commits() {
commits=("$@")
declare -A _commit_scope_traduction
get_var commit_scope_traduction _commit_scope_traduction
local _show_body
get_var show_body _show_body
local i
for i in "${commits[@]}";do
local hash="${COMMIT_HASH[$i]}"
debug "commit : ${hash}"
local title="${COMMIT_TITLE[$i]}"
debug "commit title : ${title}"
local scope="${commit_scope_traduction["${COMMIT_SCOPE[$i]}"]:-${COMMIT_SCOPE[$i]}}"
local scope="${_commit_scope_traduction["${COMMIT_SCOPE[$i]}"]:-${COMMIT_SCOPE[$i]}}"
debug "commit scope : ${scope}"
set_tpl_vars
get_var commit_tpl _commit_tpl
# shellcheck disable=SC2059,SC2154
printf -v output -- "${output}${commit_tpl[0]}" "${commit_tpl[@]:1}"
if [ -n "${show_body}" ] && [ -n "${COMMIT_BODY[$i]}" ];then
printf -v output -- "${output}${_commit_tpl[0]}" "${_commit_tpl[@]:1}"
if [ -n "${_show_body}" ] && [ -n "${COMMIT_BODY[$i]}" ];then
# shellcheck disable=SC2016,SC2001
printf -v output -- "${output}"' ```\n%s\n ```\n' "$(echo "${COMMIT_BODY[$i]}" | sed 's/^/ /g')"
fi
Expand All @@ -193,22 +212,31 @@ update_changelog() {
local escaped_release
local escaped_unreleased
local changelog_cleaned
local line
local _header_tpl
get_var header_tpl _header_tpl
# shellcheck disable=SC2059
escaped_header=$(printf -- "${header_tpl[0]:-}" "${header_tpl[@]:1}" | sed 's/[.[\/\*^$]/\\&/g')
escaped_header=$(printf -- "${_header_tpl[0]:-}" "${_header_tpl[@]:1}" | sed 's/[.[\/\*^$]/\\&/g')
local _release_tpl
get_var release_tpl _release_tpl
# shellcheck disable=SC2059
escaped_release=$(printf -- "${release_tpl[0]:-}" "${release_tpl[@]:1}" | sed 's/[.[\/\*^$]/\\&/g')
escaped_release=$(printf -- "${_release_tpl[0]:-}" "${_release_tpl[@]:1}" | sed 's/[.[\/\*^$]/\\&/g')
local _unreleased_tpl
get_var unreleased_tpl _unreleased_tpl
# shellcheck disable=SC2059
escaped_unreleased=$(printf -- "${unreleased_tpl[0]:-}" "${unreleased_tpl[@]:1}" | sed 's/[.[\/\*^$]/\\&/g')
escaped_unreleased=$(printf -- "${_unreleased_tpl[0]:-}" "${_unreleased_tpl[@]:1}" | sed 's/[.[\/\*^$]/\\&/g')
touch "${CHANGELOG_PATH}"
# header
local line
while IFS= read -r line;do
changelog_cleaned+=( "$line" )
done < <(
# shellcheck disable=SC2059
printf -- "# ${header_tpl[0]}" "${header_tpl[@]:1}" \
printf -- "# ${_header_tpl[0]}" "${_header_tpl[@]:1}" \
&& printf '\0'
)

Expand Down

0 comments on commit 525fddf

Please sign in to comment.