-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_version.scp
executable file
·124 lines (93 loc) · 3.51 KB
/
update_version.scp
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
#! /bin/bash
git fetch --tags
# Get the current branch name from the second argument
BRANCH_NAME=$2
echo "The branch name is: ${BRANCH_NAME}"
# Get the current version from the latest tag on main
VERSION_TEXT=$(git describe --tags origin/main)
MAJOR=0
MINOR=0
PATCH=0
# Get the version number from the version text.
if [[ $VERSION_TEXT =~ v([0-9]+)\.([0-9]+)\.([0-9]+) ]]; then
MAJOR=${BASH_REMATCH[1]}
MINOR=${BASH_REMATCH[2]}
PATCH=${BASH_REMATCH[3]}
fi
echo "Latest version is: ${MAJOR}.${MINOR}.${PATCH}"
# Checkout the branch
git checkout ${BRANCH_NAME}
# Get the latest commit message
LATEST_COMMIT_MESSAGE=$(git log -1 --pretty=%B)
# Check if the latest commit is empty
if ! git diff --quiet HEAD~1 HEAD; then
echo "The latest commit is not empty. Exiting."
exit 1
fi
if [[ "$LATEST_COMMIT_MESSAGE" != "Update Version" ]]; then
echo "The latest commit message is not 'Update Version'. Exiting."
exit 1
fi
# Get the first argument that is given to this script.
# It should describe the type of version update.
ARGUMENT=$1
echo "The first argument is: ${ARGUMENT}"
# Check the type of version update
if [[ $ARGUMENT == *"MAJOR"* ]]; then
MAJOR=$((MAJOR + 1))
MINOR=0
PATCH=0
echo "This is a major version update."
elif [[ $ARGUMENT == *"MINOR"* ]]; then
MINOR=$((MINOR + 1))
PATCH=0
echo "This is a minor version update."
elif [[ $ARGUMENT == *"PATCH"* ]]; then
PATCH=$((PATCH + 1))
echo "This is a patch version update."
else
echo "No version update info given."
exit 1
fi
# Set the new version text
NEW_VERSION="${MAJOR}.${MINOR}.${PATCH}"
# Check that the new version is indeed larger than the latest version
if [[ $MAJOR -lt ${BASH_REMATCH[1]} || ($MAJOR -eq ${BASH_REMATCH[1]} && $MINOR -lt ${BASH_REMATCH[2]}) || ($MAJOR -eq ${BASH_REMATCH[1]} && $MINOR -eq ${BASH_REMATCH[2]} && $PATCH -le ${BASH_REMATCH[3]}) ]]; then
echo "The new version must be greater than the latest version."
exit 1
fi
echo "NEW_VERSION: ${NEW_VERSION}"
NEW_VERSION_TEXT="v${NEW_VERSION}"
echo "The new version is set to ${NEW_VERSION_TEXT}"
# Update the major version in version.txt
sed -i "s/const int major_version = [0-9]\+/const int major_version = ${MAJOR}/" version.txt
# Update the minor version in version.txt
sed -i "s/const int minor_version = [0-9]\+/const int minor_version = ${MINOR}/" version.txt
# Update the PATCH version in version.txt
sed -i "s/const int patch = [0-9]\+/const int patch = ${PATCH}/" version.txt
# Update the version in version.txt
sed -i "s/version: [0-9]\+\.[0-9]\+\.[0-9]\+/version: ${NEW_VERSION}/" version.txt
# Update the version in version.txt
sed -i "s/version: [0-9]\+\.[0-9]\+\.[0-9]\+/version: ${NEW_VERSION}/" CITATION.cff
# Update the date-released in version.txt
CURRENT_DATE=$(date +%F)
sed -i "s/date-released: [0-9]\{4\}\.[0-9]\{2\}\.[0-9]\{2\}/date-released: ${CURRENT_DATE}/" version.txt
sed -i "s/date-released: [0-9]\{4\}\.[0-9]\{2\}\.[0-9]\{2\}/date-released: ${CURRENT_DATE}/" CITATION.cff
GIT_COMMIT_MESSAGE="Updated the version to ${NEW_VERSION}"
# Add the changes to a new commit
git config user.name t8ddy
echo "git add version.txt"
git add version.txt
git add CITATION.cff
echo "git commit -m $GIT_COMMIT_MESSAGE"
git commit -m "$GIT_COMMIT_MESSAGE"
echo "git tag"
# Tag the new version in git
if git tag -a $NEW_VERSION_TEXT -m "Version $NEW_VERSION_TEXT"; then
echo "Tag created successfully"
git push origin ${BRANCH_NAME} --tags
else
echo "Failed to create tag"
exit 1
fi
#echo "Updated version to $NEW_VERSION_TEXT"