blob: 2c45e5cab9cb1aa07f223a7e8ba6c2922ae6c45f (
plain) (
blame)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
#!/usr/bin/env bash
set -euo pipefail
main() {
if [ $# -ne 1 ]; then
usage
fi
local version="$1"
if ! git rev-parse --git-dir >/dev/null 2>&1; then
echo "Error: Not in a git repository" >&2
exit 1
fi
if ! git diff-index --quiet HEAD --; then
echo "Error: You have uncommitted changes. Please commit or stash them first." >&2
exit 1
fi
validate_version "$version"
echo "Creating release $version..."
update_cmake_version "$version"
update_man_version "$version"
update_changelog "$version"
create_commit "$version"
create_tag "$version"
push_changes "$version"
echo ""
echo "Release $version created successfully!"
}
usage() {
cat <<EOF
Usage: $0 <version>
Create a new release by:
- Bumping version in CMakeLists.txt
- Bumping version in doc/justify.1 man page
- Updating CHANGELOG.md with version and date
- Creating a git commit
- Creating a git tag
- Pushing commit and tag to remote
Arguments:
version Semantic version number (e.g., 0.2.0, 1.0.0)
Example:
$0 0.2.0
EOF
exit 1
}
validate_version() {
local version="$1"
if ! [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Error: Invalid version format. Expected semver (e.g., 0.2.0)" >&2
exit 1
fi
}
update_cmake_version() {
local version="$1"
local file="CMakeLists.txt"
local cmake_version_regex="^project(justify VERSION [0-9.]*)"
local cmake_new_version_regex="project(justify VERSION $version)"
if ! [ -f "$file" ]; then
echo "Error: $file not found" >&2
exit 1
fi
if ! grep -q "$cmake_version_regex" "$file"; then
echo "Error: Could not find version pattern in $file" >&2
exit 1
fi
if ! sed "s/$cmake_version_regex/$cmake_new_version_regex/" "$file" >"$file.tmp"; then
echo "Error: Failed to update version in $file" >&2
exit 1
fi
mv "$file.tmp" "$file"
if ! grep -q "^$cmake_new_version_regex" "$file"; then
echo "Error: Version update in $file failed verification" >&2
exit 1
fi
echo "Updated version in $file to $version"
}
update_man_version() {
local version="$1"
local date_str
date_str=$(date +"%B %Y")
local file="doc/justify.1"
local man_page_version_regex="^\.TH JUSTIFY 1 \"[^\"]*\" \"justify [0-9.]*\""
local man_page_new_version_regex="\.TH JUSTIFY 1 \"$date_str\" \"justify $version\""
if ! [ -f "$file" ]; then
echo "Error: $file not found" >&2
exit 1
fi
if ! grep -q "$man_page_version_regex" "$file"; then
echo "Error: Could not find version pattern in $file" >&2
exit 1
fi
if ! sed "s/$man_page_version_regex/$man_page_new_version_regex/" "$file" >"$file.tmp"; then
echo "Error: Failed to update version in $file" >&2
exit 1
fi
mv "$file.tmp" "$file"
if ! grep -q "$man_page_new_version_regex" "$file"; then
echo "Error: Version update in $file failed verification" >&2
exit 1
fi
echo "Updated version in $file to $version"
}
update_changelog() {
local version="$1"
local date
date=$(date +%Y-%m-%d)
local file="CHANGELOG.md"
if ! [ -f "$file" ]; then
echo "Error: $file not found" >&2
exit 1
fi
if ! grep -q "## \[Unreleased\]" "$file"; then
echo "Error: No [Unreleased] section found in $file" >&2
exit 1
fi
if grep -q "^## $version - " "$file"; then
echo "Error: Version $version already exists in $file" >&2
exit 1
fi
local temp_file
temp_file=$(mktemp)
if ! awk -v version="$version" -v date="$date" '
/## \[Unreleased\]/ {
print "## [Unreleased]\n"
print "## " version " - " date
found = 1
next
}
!found || !/## \[Unreleased\]/ {
print
}
' "$file" >"$temp_file"; then
echo "Error: Failed to process $file" >&2
rm -f "$temp_file"
exit 1
fi
if ! mv "$temp_file" "$file"; then
echo "Error: Failed to write updated $file" >&2
rm -f "$temp_file"
exit 1
fi
if ! grep -q "^## $version - $date" "$file"; then
echo "Error: Changelog update failed verification" >&2
exit 1
fi
echo "Updated $file with version $version and date $date"
}
create_commit() {
local version="$1"
if ! git add .; then
echo "Error: Failed to stage files" >&2
exit 1
fi
if ! git diff --cached --quiet; then
echo ""
echo "About to create commit with the following changes:"
git diff --cached --stat
echo ""
read -p "Create commit for version $version? (y/N): " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Commit cancelled. Restoring all changes..."
git reset --hard HEAD
exit 1
fi
if ! git commit -m "Release version $version"; then
echo "Error: Failed to create commit" >&2
git reset --hard HEAD
exit 1
fi
echo "Created commit for version $version"
else
echo "Error: No changes to commit" >&2
exit 1
fi
}
create_tag() {
local version="$1"
if git rev-parse "v$version" >/dev/null 2>&1; then
echo "Error: Tag v$version already exists" >&2
exit 1
fi
if ! git tag -a "v$version" -m "Release version $version"; then
echo "Error: Failed to create tag v$version" >&2
exit 1
fi
echo "Created tag v$version"
}
push_changes() {
local version="$1"
echo ""
read -p "Push commit and tag v$version to remote? (y/N): " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Push cancelled. Commit and tag created locally but not pushed."
echo "To push later, run:"
echo " git push && git push origin v$version"
exit 0
fi
echo "Pushing commit and tag to remote..."
if ! git push; then
echo "Error: Failed to push commit" >&2
exit 1
fi
if ! git push origin "v$version"; then
echo "Error: Failed to push tag v$version" >&2
echo "Note: Commit was pushed successfully, but tag push failed." >&2
exit 1
fi
echo "Successfully pushed version $version"
}
main "$@"
|