-
Notifications
You must be signed in to change notification settings - Fork 18
/
release.sh
executable file
·176 lines (160 loc) · 5 KB
/
release.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env bash
#set -x
# SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# PATH="$SCRIPTDIR:$PATH"
SCRIPTNAME=$(basename $0)
version=$(date +'v%Y-%m-%d')
previous_version=$(git describe --tags $(git rev-list --tags --max-count=1))
if [[ -z "$previous_version" ]];then
echo "Could not detect previous version :("
exit 1
fi
usage () {
echo "$SCRIPTNAME [options] <command>"
echo ""
echo "Options:"
echo ""
echo " -V Version to release. Default: $version"
echo " -h Show this help"
echo ""
echo "Commands:"
echo ""
echo " update Update all submodules to most recent default branch"
echo " changelog Generate a changelog for all modified submodules"
echo " release-github Release to GitHub as $version"
echo " release-dockerhub Release ocrd/all:maximum-cuda as ocrd/all:${version#v} to DockerHub"
}
main () {
while [[ "$1" = -* ]];do
case "$1" in
-V) version="$2";
if [[ "$version" != v* ]];then
echo "Version must start with 'v': $version"
exit 1
fi
shift; ;;
-h) usage; exit; ;;
esac
shift
done
if [[ -z "$1" ]];then
usage
exit 1
fi
cmd="$1"
shift
case "$cmd" in
update) update_all_submodules "$@" ;;
changelog) update_changelog "$@" ;;
release-github) release_github ;;
release-dockerhub) release_dockerhub ;;
*) usage; exit 1 ;;
esac
}
loginfo () {
echo >&2 $(date) "$@"
}
submodule_url () {
local sm="$1"
git config --file .gitmodules --get-regexp "\b$sm.url" |cut -d' ' -f 2|sed 's,\.git$,,'
}
list_all_submodules () {
git config --file .gitmodules --get-regexp path | awk '{ print $2 }' |sort -n
}
list_changed_submodules () {
git diff --stat $previous_version -- $(list_all_submodules)|grep -F '|'|cut -d ' ' -f 2
}
update_one_submodule () {
local sm="$1"
(
cd $sm
local branch=$(git remote show origin | sed -n '/HEAD branch/s/.*: //p')
loginfo "Updating submodule $sm / branch $branch"
git pull -q --rebase origin "$branch"
git pull -q --rebase origin "$branch" --tags
git submodule update --init
)
}
update_all_submodules () {
if [[ $# -gt 0 ]];then
sms="$@"
else
sms=($(list_all_submodules))
fi
for sm in "${sms[@]}";do
update_one_submodule "$sm"
done
}
submodule_changelog () {
local sm="$1"
local smurl=$(submodule_url "$sm")
local smtag=$(cd $sm; git describe --abbrev=0 --tags 2>/dev/null|| echo '')
if [[ -n "$smtag" ]];then
smtag="\\n> Release: [$smtag]($smurl/releases/$smtag)\\n"
fi
loginfo "Generating changelog for $sm"
if [ -z "$(git diff --submodule=log $previous_version -- "$sm")" ];then
return
fi
git diff --submodule=log $previous_version -- "$sm" | sed \
-e "s,^Submodule \\([^ ]\\+\\) \\([^\.]\\+\\)..\\([^\.]\\+\\):,### [\1]($smurl) [\2]($smurl/commits/\2)..[\3]($smurl/commits/\3)\\n$smtag," \
-e 's,^\s*>, > *,' | uniq
}
update_changelog () {
if (( $# == 0 ));then
sms=($(list_changed_submodules))
else
sms=($@)
fi
(
echo "# Changelog"
echo ""
echo "## [$version](https://github.com/OCR-D/ocrd_all/releases/$version)"
echo ""
for sm in ${sms[@]};do
submodule_changelog $sm
echo ""
done
sed "/^[#] Changelog/d" CHANGELOG.md
) > CHANGELOG.md.tmp
mv CHANGELOG.md.tmp CHANGELOG.md
}
release_github () {
if [[ "$(git status CHANGELOG.md)" = "" ]];then
echo "CHANGELOG.md is unmodified. Did you update it?"
exit 1
fi
changelog=$(git diff CHANGELOG.md|grep '^+'|sed -e 's,^.,,' -e 's,",\",g' )
git add CHANGELOG.md
git commit -m ":package: $version"
git tag $version
git push
git push --tags
echo -n "(p)rint changelog, (c)opy changelog to clipboard, (i)gnore? > "
read resp;
if [[ $resp = p* || $resp = P* ]];then
echo "$changelog"
elif [[ $resp = c* || $resp = C* ]];then
if command -v pbcopy 2>/dev/null;then
echo "$changelog" | pbcopy
echo "Copied to clipboard"
elif command -v xclip 2>/dev/null;then
echo "$changelog" | xclip -i
echo "Copied to clipboard"
else
echo "!! Neither xclip nor pbcopy available. Install xclip or pbcopy or copy by hand:"
echo "$changelog"
fi
fi
}
release_dockerhub () {
docker tag ocrd/all:maximum-cuda ocrd/all:${version#v}
docker tag ocrd/all:maximum ocrd/all:maximum-${version#v}
docker tag ocrd/all:maximum-cuda ocrd/all:maximum-cuda-${version#v}
docker push ocrd/all:${version#v}
docker push ocrd/all:maximum-${version#v}
docker push ocrd/all:maximum-cuda-${version#v}
docker tag ocrd/all:maximum-cuda ocrd/all:latest
docker push ocrd/all:latest
}
main "$@"