-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtag_release.sh
More file actions
executable file
·179 lines (149 loc) · 4.28 KB
/
Copy pathtag_release.sh
File metadata and controls
executable file
·179 lines (149 loc) · 4.28 KB
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
#!/bin/bash
set -euo pipefail
usage() {
echo "Usage:"
echo " $0 <version> [message] # e.g. $0 1.0.2.5 \"Bugfix release\""
echo " $0 bump <major|minor|patch> [msg]"
echo " $0 -f <release_notes_file>"
echo " $0 <version> -f <release_notes_file>"
exit 1
}
extract_latest_version_header() {
grep -m1 -oE '^[[:space:]]*##[[:space:]]*[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' "$1" \
| sed 's/[^0-9.]*//g'
}
verify_version_exists_in_notes() {
local version="$1"
local file="$2"
grep -qE "^[[:space:]]*##[[:space:]]*$version([[:space:]]|$)" "$file" \
|| { echo "❌ Error: version $version not found in $file"; exit 1; }
}
get_latest_tag() {
git fetch --tags >/dev/null 2>&1
git tag | grep '^release-' | sed 's/release-//' | sort -V | tail -n 1
}
bump_version() {
local current="$1"
local part="$2"
IFS='.' read -r major minor patch build <<<"$current"
case "$part" in
major) ((major++)); minor=0; patch=0 ;;
minor) ((minor++)); patch=0 ;;
patch) ((patch++)) ;;
*) echo "Unknown bump type: $part"; exit 1 ;;
esac
((build++)) # always increment build number
echo "$major.$minor.$patch.$build"
}
extract_notes_for_version() {
local file="$1"
local version="$2"
if ! [ -f "$file" ]; then
echo "❌ Error: $file not found." >&2
return 1
fi
local version_pattern="^##[[:space:]]*$version([[:space:]]|$)"
awk -v pattern="$version_pattern" '
$0 ~ pattern { in_section=1; next }
in_section && /^##[[:space:]]*[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+[[:space:]]*$/ { exit }
in_section { print }
' "$file"
}
extract_latest_notes() {
local file="$1"
awk '
/^##[[:space:]]*[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+[[:space:]]*$/ && ++section > 1 { exit }
/^##[[:space:]]*[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+[[:space:]]*$/ { in_section=1; next }
in_section { print }
' "$file"
}
# --- Parse arguments ---
if [ $# -lt 1 ]; then
usage
fi
VERSION=""
MESSAGE=""
FILE=""
VERSION_HEADER_FILE="include/Termin8or/version/version.h"
case "$1" in
bump)
[ $# -lt 2 ] && usage
LATEST=$(get_latest_tag)
[ -z "$LATEST" ] && LATEST="0.0.0.0"
VERSION=$(bump_version "$LATEST" "$2")
shift 2
MESSAGE="$*"
;;
-f)
[ $# -lt 2 ] && usage
FILE="$2"
VERSION=$(extract_latest_version_header "$FILE")
if [ -z "$VERSION" ]; then
echo "❌ Error: No version header found in $FILE"
exit 1
fi
shift 2
;;
*)
VERSION="$1"
shift 1
if [ "${1:-}" = "-f" ]; then
FILE="$2"
verify_version_exists_in_notes "$VERSION" "$FILE"
shift 2
else
MESSAGE="$*"
fi
;;
esac
# --- Create version.h ---
mkdir -p "$(dirname "$VERSION_HEADER_FILE")"
cat > "$VERSION_HEADER_FILE" <<EOF
#pragma once
#define TERMIN8OR_VERSION_STR "${VERSION}"
#define TERMIN8OR_VERSION_MAJOR $(echo "$VERSION" | cut -d. -f1)
#define TERMIN8OR_VERSION_MINOR $(echo "$VERSION" | cut -d. -f2)
#define TERMIN8OR_VERSION_PATCH $(echo "$VERSION" | cut -d. -f3)
#define TERMIN8OR_VERSION_BUILD $(echo "$VERSION" | cut -d. -f4)
EOF
git add "$VERSION_HEADER_FILE"
if ! git diff --cached --quiet; then
git commit -m "Automatic bumping of version in version.h."
git push
else
echo "No changes in $VERSION_HEADER_FILE; skipping commit/push and continuing to retag..."
fi
# --- Determine message ---
if [ -n "$FILE" ]; then
if [ -n "$VERSION" ]; then
MESSAGE=$(extract_notes_for_version "$FILE" "$VERSION" || true)
fi
if [ -z "$MESSAGE" ]; then
MESSAGE=$(extract_latest_notes "$FILE" || true)
fi
fi
# If no message provided, default to last commit message
if [ -z "$MESSAGE" ]; then
MESSAGE=$(git log -1 --pretty=%B)
fi
# Convert literal \n to real newlines
MESSAGE="${MESSAGE//\\n/$'\n'}"
TAG="release-$VERSION"
# Delete local tag if it exists
if git rev-parse "$TAG" >/dev/null 2>&1; then
echo "Deleting existing local tag $TAG..."
git tag -d "$TAG"
fi
# Delete remote tag if it exists
if git ls-remote --tags origin | grep -q "refs/tags/$TAG"; then
echo "Deleting remote tag $TAG..."
git push --delete origin "$TAG"
fi
# Create and push new annotated tag
echo "Creating annotated tag $TAG with message:"
echo "-----------------------------------------"
echo "$MESSAGE"
echo "-----------------------------------------"
git tag -a "$TAG" -m "$MESSAGE"
git push origin "$TAG"
echo "✅ Done — created $TAG"