Skip to content

Commit be98d10

Browse files
committed
changes: Add command to create changelog from git
1 parent b59db40 commit be98d10

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

scripts/changes

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#! /bin/bash
2+
#
3+
# Generates changelog notes from git history
4+
#
5+
# Usage: {{go}} {{cmd}} <start_ref> <end_ref>
6+
#
7+
# Where:
8+
# <start_ref> First commit reference to include (inclusive)
9+
# <end_ref> Last commit reference to include (exclusive)
10+
#
11+
# Example:
12+
# To compile a list of changes for v1.0.1 since v1.0.0:
13+
# {{go}} {{cmd}} v1.0.0 v1.0.1
14+
15+
_changes() {
16+
if [[ "$1" == '--complete' ]]; then
17+
# Tab completions
18+
local word_index="$2"
19+
if [[ "$word_index" -gt 1 ]]; then
20+
return 1
21+
fi
22+
shift
23+
shift
24+
25+
local args=("$@")
26+
compgen -W "$(git tag)" -- "${args[$word_index]}"
27+
return
28+
fi
29+
30+
local start_ref="$1"
31+
local end_ref="$2"
32+
33+
if [[ -z "$start_ref" ]]; then
34+
echo "Start ref not specified." >&2
35+
return 1
36+
elif [[ -z "$end_ref" ]]; then
37+
echo "End ref not specified." >&2
38+
return 1
39+
fi
40+
41+
git log '--pretty=format:%h %an <%ae>%n %s%n' \
42+
"${start_ref}..${end_ref}^"
43+
}
44+
45+
_changes "$@"

tests/changes.bats

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#! /usr/bin/env bats
2+
3+
load environment
4+
load assertions
5+
load script_helper
6+
7+
setup() {
8+
mkdir -p "$TEST_GO_ROOTDIR/bin" "$TEST_GO_SCRIPTS_DIR"
9+
cp "$_GO_ROOTDIR/scripts/changes" "$TEST_GO_SCRIPTS_DIR"
10+
create_test_go_script 'PATH="$_GO_ROOTDIR/bin:$PATH"' '@go "$@"'
11+
}
12+
13+
teardown() {
14+
remove_test_go_rootdir
15+
}
16+
17+
create_fake_git() {
18+
local fake_git_path="$TEST_GO_ROOTDIR/bin/git"
19+
local fake_git_impl=('#! /usr/bin/env bash' "$@")
20+
local IFS=$'\n'
21+
22+
echo "${fake_git_impl[*]}" >"$fake_git_path"
23+
chmod 700 "$fake_git_path"
24+
}
25+
26+
@test "$SUITE: tab completions" {
27+
local versions=('v1.0.0' 'v1.1.0')
28+
local IFS=$'\n'
29+
local fake_git_impl=(
30+
"if [[ \"\$1\" == 'tag' ]]; then"
31+
" echo '${versions[*]}'"
32+
' exit 0'
33+
'fi'
34+
'exit 1'
35+
)
36+
37+
create_fake_git "${fake_git_impl[@]}"
38+
run "$TEST_GO_ROOTDIR/bin/git" 'tag'
39+
assert_success "${versions[*]}"
40+
41+
run "$TEST_GO_SCRIPT" complete 1 changes ''
42+
assert_success "${versions[*]}"
43+
44+
run "$TEST_GO_SCRIPT" complete 1 changes 'v1.0'
45+
assert_success 'v1.0.0'
46+
47+
run "$TEST_GO_SCRIPT" complete 2 changes 'v1.0.0' 'v1.1'
48+
assert_success 'v1.1.0'
49+
50+
run "$TEST_GO_SCRIPT" complete 3 changes 'v1.0.0' 'v1.1.0' ''
51+
assert_failure ''
52+
}
53+
54+
@test "$SUITE: error if no start ref" {
55+
run "$TEST_GO_SCRIPT" changes
56+
assert_failure "Start ref not specified."
57+
}
58+
59+
@test "$SUITE: error if no end ref" {
60+
run "$TEST_GO_SCRIPT" changes v1.0.0
61+
assert_failure "End ref not specified."
62+
}
63+
64+
@test "$SUITE: git log call is well-formed" {
65+
local IFS=$'\n'
66+
local fake_git_impl=(
67+
"if [[ \"\$1\" == 'log' ]]; then"
68+
' shift'
69+
" IFS=\$'\\n'"
70+
' echo "$*"'
71+
' exit 0'
72+
'fi'
73+
'exit 1'
74+
)
75+
76+
create_fake_git "${fake_git_impl[@]}"
77+
run "$TEST_GO_SCRIPT" changes v1.0.0 v1.1.0
78+
assert_success
79+
assert_line_matches 0 '^--pretty=format:'
80+
assert_line_matches 1 '^v1\.0\.0\.\.v1\.1\.0\^$'
81+
}

0 commit comments

Comments
 (0)