-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-tag.sh
executable file
·157 lines (130 loc) · 4.01 KB
/
git-tag.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
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
#!/bin/bash
# Function to prompt the user to select a repository to tag
select_repository() {
echo -e "\e[32mWhich repository do you want to tag?\e[0m"
echo -e "1. \e[36mCLIENT\e[0m"
echo -e "2. \e[36mSERVICE\e[0m"
echo -en "\e[3m> "
read REPO_NUM
echo -en "\e[23m"
if ! echo "$REPO_NUM" | grep -q "^[1-2]$"; then
echo -e "\e[31mError: Invalid input. Please enter 1 or 2.\e[0m"
exit 1
fi
if [ "$REPO_NUM" -eq 1 ]; then
REPO_NAME="CLIENT"
elif [ "$REPO_NUM" -eq 2 ]; then
REPO_NAME="SERVICE"
fi
echo -e "Selected repository: \e[36m$REPO_NAME\e[0m"
}
# Function to create an initial tag with version 1.0.0
create_initial_tag() {
NEW_TAG="TMS-$REPO_NAME-V1.0.0-$DATE"
COMMIT_MESSAGE="PROD RELEASE $DATE"
echo -e "Created initial tag: \e[32m$NEW_TAG\e[0m"
}
# Function to get the latest tag and extract the version number and date
get_latest_tag_info() {
DATE=$(date +%F)
echo -e "\e[34mFetching from origin\e[0m"
# Fetch the latest changes from origin
if ! git pull > /dev/null 2>&1; then
echo -e "\e[31mError: Failed to fetch from origin.\e[0m"
exit 1
fi
# Fetch all the tags from origin and force update the local tags
if ! git pull --tags --force > /dev/null 2>&1; then
echo -e "\e[31mError: Failed to fetch tags from origin.\e[0m"
exit 1
fi
# Get the latest tag matching the specified pattern
LATEST_TAG=$(git tag -l --sort=-creatordate "TMS-$REPO_NAME-[0-9]*.[0-9]*.[0-9]*-*" | head -n 1)
# If no tags were found, set the flag for an initial release
if [ -z "$LATEST_TAG" ]; then
echo -e "\e[33mNo matching tags found.\e[0m"
IS_INITIAL="true"
else
# Extract the version number from the latest tag
VERSION=${LATEST_TAG##TMS-$REPO_NAME-}
fi
}
# Function to prompt the user for the release type and increment the version number accordingly
increment_version_number() {
echo -e "\e[32mWhat type of release is this?\e[0m"
echo -e "1. \e[36mMajor release\e[0m"
echo -e "2. \e[36mMinor release\e[0m"
echo -e "3. \e[36mPatch release\e[0m"
echo -e "4. \e[36mTest release\e[0m"
echo -en "\e[3m> "
read RELEASE_TYPE
echo -en "\e[23m"
if ! echo "$RELEASE_TYPE" | grep -q "^[1-4]$"; then
echo -e "\e[31mError: Invalid input.\e[0m"
exit 1
fi
case $RELEASE_TYPE in
1)
VERSION=$(echo $VERSION | awk -F. '{$1 = $1 + 1; $2 = 0; $3 = 0;} 1' OFS=.)
NEW_TAG="TMS-$REPO_NAME-V$VERSION-$DATE"
COMMIT_MESSAGE="PROD RELEASE $DATE"
;;
2)
VERSION=$(echo $VERSION | awk -F. '{$2 = $2 + 1; $3 = 0;} 1' OFS=.)
NEW_TAG="TMS-$REPO_NAME-V$VERSION-$DATE"
COMMIT_MESSAGE="PROD RELEASE $DATE"
;;
3)
VERSION=$(echo $VERSION | awk -F. '{$3 = $3 + 1;} 1' OFS=.)
NEW_TAG="TMS-$REPO_NAME-V$VERSION-$DATE"
COMMIT_MESSAGE="PROD RELEASE $DATE"
;;
4)
NEW_TAG="test"
COMMIT_MESSAGE="TEST PROD RELEASE $DATE"
;;
*)
echo -e "\e[31mError: Invalid release type.\e[0m"
exit 1
;;
esac
}
# Function to tag the repository and push the tag
tag_repository() {
git tag -a $NEW_TAG -m "$COMMIT_MESSAGE" > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo -e "\e[31mError: Failed to create tag.\e[0m"
exit 1
fi
git push origin $NEW_TAG > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo -e "\e[31mError: Failed to push tag. Deleting the tag $NEW_TAG.\e[0m"
git tag -d $NEW_TAG
exit 1
fi
GIT_REV=$(git rev-parse $NEW_TAG)
echo -e "Tagged \e[32m$NEW_TAG\e[0m with hash id \e[36m$GIT_REV\e[0m and pushed to origin."
if [ "$RELEASE_TYPE" != "4" ]; then
git tag latest $GIT_REV -f > /dev/null 2>&1
git push origin latest -f > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo -e "\e[31mError: Failed to push latest tag. Deleting the tag latest.\e[0m"
git tag -d latest
exit 1
fi
echo -e "Pushed latest tag to origin."
fi
}
# Main function
main() {
select_repository
get_latest_tag_info
if [ "$IS_INITIAL" = "true" ]; then
create_initial_tag
else
increment_version_number
fi
tag_repository
}
# Call the main function
main