summaryrefslogtreecommitdiff
path: root/bin/auto-changelog
blob: d672b5f65d028548831e034b878bd83102053163 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env bash
set -e

list-files() {
	git show --pretty="" --name-only HEAD
}

list-articles() {
	list-files | grep -E '^src/.*[.](md|org)$' | grep -v -e '/index[.]md$' -e '/changelog[.]md$'
}	

should-insert() {
	test -n "$(list-articles)"
}

generate-entry() {
	git log -n1 --stat --date='format:%Y-%m-%d' --format=$'## %ad  %an <%ae>\n\n%B' | cat -s
}

html_escape() {
	sed '
/^\S/ {
    s/&/\&amp;/g
    s/</\&lt;/g
    s/>/\&gt;/g
}
s/^ \S/   &/
'
}

insert() {
	{
		printf '%s\n' \
		       'ChangeLog' \
		       '=========' \
		       ''

		generate-entry | html_escape
		echo
		cat src/changelog.md | sed '1,3d'
	} | bin/write-atomic src/changelog.md
}

main() {
	cd "$(dirname -- "$0")/.."
	if should-insert; then
		insert
		git add src/changelog.md
		git commit -m 'Auto-insert entry to ChangeLog' --author='Logger <logger@andrewdm.me>'
	fi
}

main "$@"