-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-basic-changelog.sh
executable file
·110 lines (86 loc) · 3.47 KB
/
git-basic-changelog.sh
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env bash
set -e
# utf8 icons: https://www.utf8icons.com
INFO_TEXT="Plugin $(basename "${0%.*}")"
# =========================================================================================================
# functions()
say()
{
if [ -n "$2" ]; then
printf "🛈\e[32m ${INFO_TEXT}\e[0m\e[36m[⚒️ %s]\e[0m: %s \n" "$2" "$1"
else
printf "🛈\e[32m ${INFO_TEXT}\e[0m: %s \n" "$1"
fi
return 0
}
sayE()
{
if [ -n "$2" ]; then
printf "✘\e[31m ${INFO_TEXT}\e[0m\e[36m[⚒️ %s]\e[0m: %s \n" "$2" "$1" 1>&2
else
printf "✘\e[31m ${INFO_TEXT}\e[0m: %s \n" "$1" 1>&2
fi
exit 1
}
sayW()
{
if [ -n "$2" ]; then
printf "⚠\e[33m ${INFO_TEXT}\e[0m\e[36m[⚒️ %s]\e[0m: %s \n" "$2" "$1" 1>&2
else
printf "⚠\e[33m ${INFO_TEXT}\e[0m: %s \n" "$1" 1>&2
fi
return 0
}
generate_changelog()
{
if eval git describe --tags --abbrev=0 &> /dev/null; then
# print commit log lines to CI_COMMIT_TAG
if [ -n "${CI_COMMIT_TAG}" ]; then
PREVIOUS_TAG="$(git describe --tags --abbrev=0 "${CI_COMMIT_TAG}^" 2> /dev/null || return 1)"
# from previous tag
if [ -n "${PREVIOUS_TAG}" ]; then
${PLUGIN_DEBUG:-false}&& say "Previous tag: ${PREVIOUS_TAG}"
${PLUGIN_DEBUG:-false}&& say "New tag: ${CI_COMMIT_TAG}"
say "Generate changelog from '${PREVIOUS_TAG}' to '${CI_COMMIT_TAG}'..."
git log "${PREVIOUS_TAG}..${CI_COMMIT_TAG}" --no-merges --pretty="- %s" | tee -a CHANGELOG.md &> /dev/null || return 1
echo -e "\n_**Compare**_: [${PREVIOUS_TAG}...${CI_COMMIT_TAG}](${CI_REPO_URL}/compare/${PREVIOUS_TAG}...${CI_COMMIT_TAG})" >> CHANGELOG.md
# for all commits
else
${PLUGIN_DEBUG:-false}&& say "New tag: ${CI_COMMIT_TAG}"
say "Generate changelog from first commit to '${CI_COMMIT_TAG}'..."
git log --no-merges --pretty="- %s" | tee -a CHANGELOG.md &> /dev/null
fi
# print all commits log lines to last commit
else
LAST_TAG="$(git describe --tags --abbrev=0 2> /dev/null || return 1)"
# from last tag
if [ -n "${LAST_TAG}" ]; then
say "Last tag: ${LAST_TAG}"
say "Generate changelog from '${LAST_TAG}' to last commit..."
git log "${LAST_TAG}"..HEAD --no-merges --pretty="- %s" | tee -a CHANGELOG.md &> /dev/null || return 1
# for all commits
else
say "Found any tag in this repository... Generate full changelog..."
git log --no-merges --pretty="- %s" | tee -a CHANGELOG.md &> /dev/null || return 1
fi
fi
else
# all commits log lines
say "Found any tag in this repository... Generate full changelog..."
git log --no-merges --pretty="- %s" | tee -a CHANGELOG.md &> /dev/null || return 1
fi
return 0
}
# =========================================================================================================
# main()
# fix perm in Windows Container
uname -a | grep -q Windows && git config --global --add safe.directory "${CI_WORKSPACE}"
# changelog headers
echo -e "# What Changed\n" > CHANGELOG.md
generate_changelog || sayE "Git changelog generated with error! 💣"
say "Git changelog successfully generated. ✅"
if ${PLUGIN_DEBUG:-false}; then
sayW "Debug mode enable... Generated changelog:"
cat CHANGELOG.md
fi
exit 0