-
Notifications
You must be signed in to change notification settings - Fork 4
/
makeversion
executable file
·152 lines (136 loc) · 4.42 KB
/
makeversion
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
#!/bin/bash
if [ $# -eq 0 ]; then
echo >&2 "makeversion: creates new version tag and changelog"
echo >&2 "Usage: $0 {[0-9]+.[0-9]+.[0-9]+(-beta)?}"
exit 1
fi
getversion () {
git describe --tags --match "v*" --long --dirty --abbrev=8 2>/dev/null
}
checkversion () {
echo "$1" | grep -E "^v[0-9]+\.[0-9]+\.[0-9]+(-beta)?" >/dev/null
}
dissectversion () {
echo "$1" | sed -r 's/^v([0-9]+)\.([0-9]+)\.([0-9]+)(-?(beta))?(-([0-9]+)-g([a-fA-F0-9]+)(-dirty)?)?$/\1:\2:\3:\5:\7:\8:\9:/'
}
get () {
echo "$2" | awk -F: '{ print $'$1' }'
}
color () {
fname=$(mktemp)
cat - > $fname
tput setaf $1
cat $fname
tput sgr0
rm -f $fname
}
red () { color 1; }
green () { color 2; }
blue () { color 4; }
compareversions () {
_old="$1"
_new="$2"
oldM=$(get 1 "$_old")
oldm=$(get 2 "$_old")
oldp=$(get 3 "$_old")
newM=$(get 1 "$_new")
newm=$(get 2 "$_new")
newp=$(get 3 "$_new")
dM=$((newM-oldM))
dm=$((newm-oldm))
dp=$((newp-oldp))
echo "$dM:$dm:$dp"
}
makechangelog () {
printf "\n# Enter here the informations about changes in your program in version %s.\n" "$newv" > release/changelog
tag=$(git describe --tags --abbrev=0);
git log --oneline "$tag"..HEAD | sed 's/^/# /' >> release/changelog
vim -c 'set ft=gitcommit | set tw=80 | set cc=+1 | startinsert' release/changelog
wc=$(sed '1d; 2d; /^#/ d' release/changelog | wc -w)
if [ $wc -gt 0 ]; then
{
cat release/changelog_before
cat release/changelog_template
sed '1d; 2d; /^#/ d' release/changelog
echo ""
cat release/changelog_after
} | uniq > CHANGELOG.md
rm -f release/changelog_before
rm -f release/changelog_template
rm -f release/changelog_after
sed -i '/^#/ d' release/changelog
else
printf "Refusing to make version '%s' with empty changelog.\n" "$newv" | red; exit 1;
fi;
}
newv=v$1; shift;
if { checkversion $newv; }; then
printf "Valid version '%s'.\n" "$newv" | green;
else
printf "Invalid version '%s'.\n" "$newv" | red; exit 1;
fi
oldv=$(getversion);
printf "Current version '%s'.\n" "$oldv" | blue;
newd=$(dissectversion "$newv");
oldd=$(dissectversion "$oldv");
delta=$(compareversions "$oldd" "$newd");
x=$(get 7 "$oldd");
if [ -n "$x" ]; then
printf "Refusing to make version '%s' on a dirty repository.\n" "$newv" | red; exit 1;
fi;
mkdir -p release/
x=$(get 1 "$delta");
if [ $x -lt 0 ]; then
printf "Refusing to make version '%s' with older Major than '%s'.\n" "$newv" "$oldv" | red; exit 1;
elif [ $x -gt 0 ]; then
printf "New Major.\n" | green;
y=$(get 1 "$oldd");
sed '/# '"$y"'$/,// d' CHANGELOG.md > release/changelog_before
sed -n '/# '"$y"'$/,// p' CHANGELOG.md > release/changelog_after
M=$(get 1 "$newd");
m=$M.$(get 2 "$newd");
p=${newv:1};
echo -e "# $M\n\n## $m\n\n### $p\n" > release/changelog_template
else
printf "Same Major.\n" | blue;
x=$(get 2 "$delta");
if [ $x -lt 0 ]; then
printf "Refusing to make version '%s' with older minor than '%s'.\n" "$newv" "$oldv" | red; exit 1;
elif [ $x -gt 0 ]; then
printf "New minor.\n" | green;
y=$(get 1 "$oldd")'\.'$(get 2 "$oldd");
sed '/## '"$y"'$/,// d' CHANGELOG.md > release/changelog_before;
sed -n '/## '"$y"'$/,// p' CHANGELOG.md > release/changelog_after;
m=$(get 1 "$newd").$(get 2 "$newd");
p=${newv:1};
echo -e "## $m\n\n### $p\n" > release/changelog_template
else
printf "Same minor.\n" | blue;
x=$(get 3 "$delta");
if [ $x -lt 0 ]; then
printf "Refusing to make version '%s' with older patch than '%s'.\n" "$newv" "$oldv" | red; exit 1;
elif [ $x -gt 0 ]; then
printf "New patch.\n" | green;
y=$(get 1 "$oldd")'\.'$(get 2 "$oldd")'\.'$(get 3 "$oldd");
sed '/### '"$y"'[^0-9]*$/,// d' CHANGELOG.md > release/changelog_before;
sed -n '/### '"$y"'[^0-9]*$/,// p' CHANGELOG.md > release/changelog_after;
p=${newv:1};
echo -e "### $p\n" > release/changelog_template
else
printf "Refusing to make already existing version '%s'.\n" "$newv" | red; exit 1;
fi;
fi;
fi;
makechangelog;
find . -type f -name '*.sty' | xargs -n1 -d'\n' sed -i '/\\ProvidesPackage/ s/\[[^,]*,/\['"$(date +%Y\\/%m\\/%d)"' '"$newv"',/'
find . -type f -name '*.sty' | xargs -n1 -d'\n' git add
git add CHANGELOG.md && {
nparents=$(git cat-file -p HEAD | sed -n '/^parent/ p' | wc -l)
if [ $nparents -gt 1 ]; then
flags=--amend;
else
flags=;
fi;
} &&
git commit $flags -S -m "releasing $newv" &&
git tag "$newv" -s -F release/changelog