-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversioning.sh
executable file
·63 lines (52 loc) · 1.84 KB
/
versioning.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
#!/bin/bash
# Set variables
VERSION_FILE="VERSION"
# Fetch the current version
if [[ -f "$VERSION_FILE" ]]; then
VERSION=$(cat "$VERSION_FILE")
else
echo "Version file not found!"
exit 1
fi
# Split the current version into Major, Minor, and Patch
IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION"
# If version format is incorrect, initialize it
if [[ -z "$MAJOR" || -z "$MINOR" || -z "$PATCH" ]]; then
echo "0.0.0" > "$VERSION_FILE"
VERSION="0.0.0"
IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION"
fi
# Check if the repository has any commits
if git rev-parse --verify HEAD >/dev/null 2>&1; then
# There is at least one commit, compare changes with the previous commit
GIT_DIFF_RANGE="HEAD^..HEAD"
DIFF_CMD="git diff --name-status $GIT_DIFF_RANGE"
else
# No commits yet (first commit), compare changes with an empty tree
DIFF_CMD="git diff --cached --name-status"
fi
# Run the diff command to check for changes
FILES_ADDED_REMOVED=$($DIFF_CMD | grep -E '^[A|D]')
FILES_MODIFIED=$($DIFF_CMD | grep -E '^M')
# Determine whether to increase Minor or Patch version
if [[ ! -z "$FILES_ADDED_REMOVED" ]]; then
echo "Files added or removed. Increasing Minor version."
MINOR=$((MINOR + 1))
PATCH=0 # Reset patch to 0 when Minor is incremented
elif [[ ! -z "$FILES_MODIFIED" ]]; then
echo "Files modified. Increasing Patch version."
PATCH=$((PATCH + 1))
else
echo "No changes detected."
exit 0 # Exit if no changes detected
fi
# Construct the new version
NEW_VERSION="$MAJOR.$MINOR.$PATCH"
echo "Updating version: $VERSION -> $NEW_VERSION"
# Write the new version back to the version file
echo "$NEW_VERSION" > "$VERSION_FILE"
# Commit the new version file
git add "$VERSION_FILE"
# Add a tag with the new version
git tag -a $NEW_VERSION -m "v$NEW_VERSION"
git commit -m "Bump version to $NEW_VERSION [CI]"