Skip to content

Commit

Permalink
feat(config): let users interact with conventional commits regex / se…
Browse files Browse the repository at this point in the history
…ctions to show
  • Loading branch information
rockandska committed Nov 20, 2021
1 parent 525fddf commit bd7a5b9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
45 changes: 29 additions & 16 deletions docs/config.md
@@ -1,7 +1,23 @@
# Configuration

## Custom configuration

Configuration could be changed at the user level or at the repo level

Configuration is done by providing file(s) with BASH variables declarations.

### User configuration

If exists, `~/.config/git-changelog/config` will be loaded.

Use this configuration to change git-changelog behavior for all git-changelog generated

### Repo configuration

If a file named `.git-changelog` is present at the root of your repo, this file will be loaded.

Use this configuration to change git-changelog behavior for a specific repo.

## Git variables

Variables available for use in templates variables.
Expand All @@ -19,6 +35,19 @@ Variables available for use in templates variables.
- Type: string
- current commit hash

## Conventional commits variables

Available variables who could be changed are :

- **`conventional_commit_regex`**
- Type: String
- Used to detect conventionnal commits
- Default! `conventional_commit_regex="^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test){1}(\(([[:alnum:]._-]+)\))?(!)?: ([[:print:]]*)"`
- **`conventional_commit_to_show`**
- Type: Array
- Used to decide which commits type should be shown in CHANGELOG
- Default: `conventional_commit_to_show=("BREAKING_CHANGES" "feat" "fix")`

## Templating variables

Available variables who could be changed are :
Expand Down Expand Up @@ -64,22 +93,6 @@ Available variables who could be changed are :
- Used to convert scope to more friendly names
- Default: `commit_scope_traduction=()`

## Custom configuration

Configuration could be changed at the user level or at the repo level

### User configuration

If exists, `~/.config/git-changelog/config` will be loaded.

Use this configuration to change git-changelog behavior for all git-changelog generated

### Repo configuration

If a file named `.git-changelog` is present at the root of your repo, this file will be loaded.

Use this configuration to change git-changelog behavior for a specific repo.

## Examples

We assume a git log like the one bellow :
Expand Down
22 changes: 13 additions & 9 deletions git-changelog
Expand Up @@ -55,6 +55,8 @@ set_user_config() {
local commit_tpl=('- %s (%.7s)\n' "\${title}" "\${hash}")
declare -A commit_type_traduction
declare -A commit_scope_traduction
local conventional_commit_regex="^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test){1}(\(([[:alnum:]._-]+)\))?(!)?: ([[:print:]]*)"
local conventional_commit_to_show=("BREAKING_CHANGES" "feat" "fix")
${user_conf:-}
${repo_conf:-}
eval \$(declare -p "\$var2get" 2> /dev/null | sed "s/declare .. \$var2get=/\$var2return="/)
Expand All @@ -73,13 +75,17 @@ set_commit_infos() {
local commit_breaking
local commit_scope
local commit_title
local _conventional_commit_regex
get_var conventional_commit_regex _conventional_commit_regex
local _conventional_commit_to_show
get_var conventional_commit_to_show _conventional_commit_to_show
local i=0
while IFS= read -r -d $'\0' commit_infos; do
eval "$commit_infos"
commit_subject="${commit_subject%$'\n'}"
commit_hash="${commit_hash%$'\n'}"
commit_body="${commit_body%$'\n'}"
if [[ "${commit_subject}" =~ $CONVENTIONAL_COMMIT_REGEX ]];then
if [[ "${commit_subject}" =~ ${_conventional_commit_regex} ]];then
debug "OK : conventional commit found in subject : '${commit_subject}' (${commit_hash::7})"
commit_type="${BASH_REMATCH[1]}"
commit_breaking="${BASH_REMATCH[4]}"
Expand All @@ -99,8 +105,8 @@ set_commit_infos() {
done <<<"${commit_body}"
fi
# shellcheck disable=SC2076
if [[ " ${CONVENTIONAL_COMMIT_TO_SHOW[*]} " == *" ${commit_type} "* ]]; then
debug "OK : Commit type '$commit_type' is in \$CONVENTIONAL_COMMIT_TO_SHOW"
if [[ " ${_conventional_commit_to_show[*]} " == *" ${commit_type} "* ]]; then
debug "OK : Commit type '$commit_type' is in \$conventional_commit_to_show"
COMMIT_HASH[$i]="$commit_hash"
COMMIT_TITLE[$i]="$commit_title"
COMMIT_BODY[$i]="$commit_body"
Expand Down Expand Up @@ -132,6 +138,8 @@ set_changelog_release() {
get_var commit_type_traduction _commit_type_traduction
declare -A _commit_scope_traduction
get_var commit_scope_traduction _commit_scope_traduction
local _conventional_commit_to_show
get_var conventional_commit_to_show _conventional_commit_to_show
if [[ -n "${tag:-}" ]];then
# shellcheck disable=SC2059,SC2154
printf -v output -- "${output:-}## ${_release_tpl[0]}" "${_release_tpl[@]:1}"
Expand All @@ -141,7 +149,7 @@ set_changelog_release() {
fi

local type
for type in "${CONVENTIONAL_COMMIT_TO_SHOW[@]}";do
for type in "${_conventional_commit_to_show[@]}";do
debug "Processing commit type: $type"
if [[ -n "${COMMIT_TYPE_LIST[$type]:-}" ]];then
local commit_list
Expand Down Expand Up @@ -358,10 +366,6 @@ git-changelog() {
local CHANGELOG_PATH
CHANGELOG_PATH="${GIT_TOP_LEVEL}/CHANGELOG.md"

# Conventional commits
local CONVENTIONAL_COMMIT_REGEX="^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test){1}(\(([[:alnum:]._-]+)\))?(!)?: ([[:print:]]*)"
local CONVENTIONAL_COMMIT_TO_SHOW=("BREAKING_CHANGES" "feat" "fix" "revert" "test")

# Git infos
local GIT_ACTUAL_TAGS
local GIT_ACTUAL_TAG
Expand All @@ -377,8 +381,8 @@ git-changelog() {
declare -A COMMIT_SCOPE_LIST

set_git_infos
set_commit_infos
set_user_config
set_commit_infos
if [[ "${#COMMIT_HASH[@]}" -gt 0 ]];then
set_changelog_release
update_changelog
Expand Down

0 comments on commit bd7a5b9

Please sign in to comment.