Skip to content

Commit

Permalink
feat(output): add capability to show scope section
Browse files Browse the repository at this point in the history
  • Loading branch information
rockandska committed Nov 15, 2021
1 parent e8d1d1e commit d3135ca
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 32 deletions.
42 changes: 24 additions & 18 deletions README.md
Expand Up @@ -40,30 +40,18 @@ docker run --rm -ti -v $(pwd):/git -e CHANGELOG_TAG=1.0.0 rockandska/git-changel

## Configuration

Config variables should be BASH array representig a printf expression.

Example:

```bash
commit_tpl=('- %s (%.7s)\n' "${title:-}" "${hash:-}")
```

would be convert to :

```bash
printf -- '- %s (%.7s)\n' "${title:-}" "${hash:-}"
```

### Variables

In configuration, some variables are availables for use

- tag : current tag if exists
- type: current commit type ( feat, fix, etc.. )
- scope: current commit scope
- hash: current commit hash

Available variables who could be changed are :

- show_scope: Will show commits grouped by scope if set to non empty
- header_tpl: used to generate a HEADER section
- release_tpl: used to generate release section when a tag exists
- unreleased_tpl: used to generate unreleased section when no tag exists
Expand All @@ -73,11 +61,30 @@ Available variables who could be changed are :
### Default configuration

```bash
# Should we show scope section ?
show_scope=

# Templates
header_tpl=('%s\n\n' 'CHANGELOG')
release_tpl=('%s\n\n' "${tag:-}")
release_tpl=('%s\n\n' "${tag}")
unreleased_tpl=('%s\n\n' 'Unreleased')
type_tpl=('%s\n\n' "${type:-}")
commit_tpl=('- %s (%.7s)\n' "${title:-}" "${hash:-}")
scope_tpl=('%s\n\n' "\${scope}")
type_tpl=('%s\n\n' "${type}")
commit_tpl=('- %s (%.7s)\n' "${title}" "${hash}")
```

Templates variables ([var]_tpl) should be BASH array representig a printf expression.

Example:

```bash
commit_tpl=('- %s (%.7s)\n' "${title}" "${hash}")
```

would be convert to :

```bash
printf -- '- %s (%.7s)\n' "${title}" "${hash}"
```

### Global user configuration
Expand All @@ -96,7 +103,6 @@ Use this configuration to change git-changelog behavior for a specific repo.
## TODO

- write documentation
- add Scope section
- add possibility to convert type to a more friendly name (ex: feat -> Features)
- allow to only print generated CHANGELOG
- allow to go in the past
56 changes: 42 additions & 14 deletions git-changelog
Expand Up @@ -37,11 +37,13 @@ set_user_config() {
: \${tag:=}
: \${title:=}
: \${type:=}
# Used to generate new section in CHANGELOG
: \${scope:=}
show_scope=
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}")
${user_conf:-}
${repo_conf:-}
Expand Down Expand Up @@ -88,8 +90,9 @@ set_commit_infos() {
COMMIT_HASH[$i]="$commit_hash"
COMMIT_TITLE[$i]="$commit_title"
COMMIT_BODY[$i]="$commit_body"
COMMIT_SCOPE[$i]="$commit_scope"
COMMIT_SCOPE[$i]="${commit_scope:=no scope}"
COMMIT_TYPE_LIST["$commit_type"]="${COMMIT_TYPE_LIST[$commit_type]:-} $i"
COMMIT_SCOPE_LIST["$commit_scope"]="${COMMIT_SCOPE_LIST[${commit_scope}]:-} $i"
((i=i+1))
else
debug "WARNING : Commit type '${commit_type}' not in \$CONVENTIONAL_COMMIT_TO_SHOW"
Expand All @@ -103,6 +106,7 @@ set_commit_infos() {
set_changelog_release() {
debug "Writing changelog.."
local tag="${GIT_ACTUAL_TAG}"
local show_scope
set_tpl_vars
if [[ -n "${tag:-}" ]];then
# shellcheck disable=SC2059,SC2154
Expand All @@ -122,24 +126,47 @@ set_changelog_release() {
set_tpl_vars
# shellcheck disable=SC2059,SC2154
printf -v output -- "${output}### ${type_tpl[0]}" "${type_tpl[@]:1}"
local i
for i in "${commit_list[@]}";do
local hash="${COMMIT_HASH[$i]}"
debug "commit : ${hash}"
local title="${COMMIT_TITLE[$i]}"
debug "commit message : ${title}"
set_tpl_vars
# shellcheck disable=SC2059,SC2154
printf -v output -- "${output}${commit_tpl[0]}" "${commit_tpl[@]:1}"
done
# shellcheck disable=SC2059
printf -v output -- "${output}\n"
if [ -n "${show_scope}" ];then
while IFS= read -r scope;do
set_tpl_vars
local scope_commits=()
for commit in "${commit_list[@]}";do
if [[ " ${COMMIT_SCOPE_LIST["$scope"]} " == *" ${commit} "* ]];then
scope_commits+=("$commit")
fi
done
debug "commits with scope : $scope" "${scope_commits[@]}"
if [ "${#scope_commits[@]}" -gt 0 ];then
# shellcheck disable=SC2059,SC2154
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)
else
set_changelog_release_commits "${commit_list[@]}"
fi
else
debug "No commit found with type : ${type}"
fi
done
}

set_changelog_release_commits() {
commits=("$@")
local i
for i in "${commits[@]}";do
local hash="${COMMIT_HASH[$i]}"
debug "commit : ${hash}"
local title="${COMMIT_TITLE[$i]}"
debug "commit message : ${title}"
set_tpl_vars
# shellcheck disable=SC2059,SC2154
printf -v output -- "${output}${commit_tpl[0]}" "${commit_tpl[@]:1}"
done
# shellcheck disable=SC2059
printf -v output -- "${output}\n"
}

update_changelog() {
# Used with sed to delete old section in CHANGELOG
local escaped_header
Expand Down Expand Up @@ -223,6 +250,7 @@ git-changelog() {
local COMMIT_BODY=()
local COMMIT_SCOPE=()
declare -A COMMIT_TYPE_LIST
declare -A COMMIT_SCOPE_LIST

set_git_infos
set_commit_infos
Expand Down
41 changes: 41 additions & 0 deletions test/bats/specs/scope.bats
@@ -0,0 +1,41 @@
load fixtures/setup_file.sh

setup() {
cd $BATS_TEST_TMPDIR
}

@test "show_scope should works" {
echo 'show_scope=1'> ${BATS_TEST_TMPDIR}/.git-changelog
git init
git commit --allow-empty -m"fix(config): add fix"
git_commit+=($(git rev-parse --short HEAD))
git commit --allow-empty -m"feat(test): add feat"
git_commit+=($(git rev-parse --short HEAD))
git commit --allow-empty -m"feat: add feat"
git_commit+=($(git rev-parse --short HEAD))
run git-changelog
[ "$output" == "${BATS_TEST_TMPDIR}/CHANGELOG.md updated !" ]
diff "${BATS_TEST_TMPDIR}/CHANGELOG.md" <(cat <<EOF
# CHANGELOG
## Unreleased
### feat
#### no scope
- add feat (${git_commit[2]})
#### test
- add feat (${git_commit[1]})
### fix
#### config
- add fix (${git_commit[0]})
EOF
)
}

0 comments on commit d3135ca

Please sign in to comment.