-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathprint-changelog.sh
139 lines (126 loc) · 3.88 KB
/
print-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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env bash
# Writes changelog in markdown format to std out.
# Example usage:
# local changelog
# if ! changelog=$(bash "print-changelog.sh" --repository "undergroundwires/privacy.sexy"); then
# echo "Could not create changelog"
# fi
# Prerequisites:
# - Ensure your current folder is the repository root
# - Ensure all tags are fetched
# Dependencies:
# - External: git
# - Local: ./log-commits.sh, ./shared/utilities.sh
# Globals
readonly SCRIPTS_DIRECTORY=$(dirname "$0")
readonly LOG_COMMITS_SCRIPT_PATH="$SCRIPTS_DIRECTORY/shared/log-commits.sh"
# Import dependencies
# shellcheck source=shared/utilities.sh
source "$SCRIPTS_DIRECTORY/shared/utilities.sh"
print_title() {
local -r tag="$1"
local tag_date
if ! tag_date=$(git log -1 --pretty=format:'%ad' --date=short "$tag") \
|| utilities::is_empty_or_null "$tag_date"; then
echo "Cannot get tag date for $tag"
exit 1
fi
printf "\n## %s (%s)\n\n" "$tag" "$tag_date"
}
print_all_versions_from_latest() {
git tag | sort -V -r
}
print_tags_except_first() {
local -r repository="$1"
local tags
if ! tags=$(print_all_versions_from_latest); then
echo "Could not list & sort tags"
exit 1;
fi
local current_tag=0;
for next_tag in $tags
do
if [ "$current_tag" != 0 ]; then
print_title "$current_tag"
local changes
if ! changes=$(bash "$LOG_COMMITS_SCRIPT_PATH" \
--repository "$repository" \
--current "$current_tag" \
--previous "$next_tag"); then
echo "$LOG_COMMITS_SCRIPT_PATH has failed:"
echo "$changes"
exit 1
fi
if [[ $changes ]]; then
printf "%s\n\n" "$changes"
fi
printf "[compare](/~https://github.com/%s/compare/%s...%s)" "$repository" "$next_tag" "$current_tag"
printf "\n"
fi
current_tag=${next_tag}
done
}
print_initial_commit_sha() {
git rev-list --max-parents=0 HEAD
}
print_first_version() {
git tag | sort -V | head -1
}
print_commit_sha_of_tag() {
local -r tag="$1"
git rev-list -n 1 "$tag" \
|| { echo "Cannot get the comit for tag $tag"; exit 1; }
}
print_first_tag() {
local -r repository="$1"
local first_tag
if ! first_tag=$(print_first_version) \
|| utilities::is_empty_or_null "$first_tag"; then
echo "Cannot get the first tag"
exit 1
fi
print_title "$first_tag"
local first_commit_with_tag_sha
if ! first_commit_with_tag_sha=$(print_commit_sha_of_tag "$first_tag") \
|| utilities::is_empty_or_null "$first_commit_with_tag_sha"; then
echo "Cannot get first commit with tag: $first_tag"
exit 1
fi
local initial_commit_sha
if ! initial_commit_sha=$(print_initial_commit_sha) \
|| utilities::is_empty_or_null "$initial_commit_sha"; then
echo "Cannot get initial commit";
exit 1;
fi
if [ "$first_commit_with_tag_sha" == "$initial_commit_sha" ]; then
printf "%s | [commits](/~https://github.com/%s/commit/%s)" \
"Initial release" "$repository" "$initial_commit_sha"
return 0
fi
local changes
if ! changes=$(bash "$LOG_COMMITS_SCRIPT_PATH" \
--repository "$repository" \
--current "$first_commit_with_tag_sha" \
--previous "$initial_commit_sha"); then
echo "$LOG_COMMITS_SCRIPT_PATH has failed"
exit 1;
fi
if ! utilities::is_empty_or_null "$changes"; then
printf "%s\n" "$changes"
fi
printf "[compare](/~https://github.com/%s/compare/%s...%s)" \
"$repository" "$initial_commit_sha" "$first_commit_with_tag_sha"
}
main() {
local -r repository="$1"
if utilities::is_empty_or_null "$repository"; then echo "Repository name is not set."; exit 1; fi;
printf "# Changelog\n"
print_tags_except_first "$repository"
print_first_tag "$repository"
printf "\n\n"
}
while [[ "$#" -gt 0 ]]; do case $1 in
--repository) REPOSITORY="$2"; shift;;
*) echo "Unknown parameter passed: $1"; exit 1;;
esac; shift; done
main "$REPOSITORY"