-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgitgit
More file actions
executable file
·189 lines (177 loc) · 4.34 KB
/
gitgit
File metadata and controls
executable file
·189 lines (177 loc) · 4.34 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
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
# Check for install
which git > /dev/null
if [[ $? != 0 ]]
then
echo "git is not installed!"
exit 1
fi
# Check for minimum version
GITVER=$(git --version | awk '{print $3}' | awk -F'.' '{print $1"."$2}')
GITMIN="2.25"
if (( $(echo "$GITVER < $GITMIN" | bc -l) ))
then
echo "git version is too low! Update now!"
echo "Your version: $GITVER | Minimum version required: $GITMIN"
echo "sudo add-apt-repository ppa:git-core/ppa"
echo "sudo apt update"
echo "sudo apt install git"
exit 1
fi
# Get current branch
TREEBRANCH=$(git branch --show-current)
THEBRANCH=$(git branch | sed 's/ //g' | grep -E "^(main|master)$" | head -1)
additional_commands()
{
echo "Found additional commands. Running:"
echo "$GITGITCMD"
ORIGIFS=$IFS
IFS=';'
for COMMAND in "$GITGITCMD"
do
eval "$COMMAND"
done
IFS=$ORIGIFS
}
branchit()
{
if [[ "$NB" =~ ^[-_A-Za-z0-9\(\)\/\\\$\%\#]+$ ]]
then
git pull --ff-only
echo "Creating and switching to new feature branch: $NB"
git checkout -b $NB
else
echo "Feature branch name required and can only contain allowed characters."
echo "Try: gitgit -b my-branch"
exit 1
fi
}
commit()
{
if [ ! -z "$GITGITCMD" ]
then
additional_commands
else
echo "No additional commands found... continuing."
fi
echo "On Branch: $TREEBRANCH"
git add .
if [ -z "$CM" ]
then
COMMITNOW="chore: patch"
else
COMMITNOW="$CM"
fi
git commit -m "$COMMITNOW"
git push origin $TREEBRANCH
echo "Thanks for using gitgit! Don't forget to delete this branch when finished: gitgit -d"
}
if [ $# -gt 1 ] && [[ "$1" != -b ]] && [[ "$1" != --branch ]]
then
echo "Only 1 agument is supported."
exit 1
elif [ $# -gt 2 ] && ([[ "$1" == -b ]] || [[ "$1" == --branch ]])
then
echo "Only 2 arguments are supported for checking out a branch."
echo "-b <name>"
elif [[ "$1" == -h ]] || [[ "$1" == --help ]]
then
echo "Usage:"
echo "gitgit Add, commit and push with generic message"
echo "gitgit \"<message>\" Add, commit and push with custom message"
echo "gitgit <option>"
echo ""
echo "Options:"
echo "-d|--delete Delete the current feature branch and switch to main/master"
echo "-c|--cleanup Remove any non-main/master branches"
echo "-b|--branch <name> Create and checkout a new feature branch"
echo "-h|--help Display this help"
echo "-v|--version Display the version"
echo
elif [[ "$1" == -v ]] || [[ "$1" == --version ]]
then
echo "gitgit v0.0.69"
echo "git $GITVER"
elif [[ "$1" == -c ]] || [[ "$1" == --cleanup ]]
then
git checkout $THEBRANCH
if [[ $? != 0 ]]
then
echo "Failed to checkout the main/master branch, exiting..."
exit 1
fi
git pull --ff-only
git fetch --prune
BRANCHES=$(git branch | sed 's/ //g' | grep -vE "^\*?(main|master)$")
for BRANCH in $BRANCHES
do
echo "Deleting branch $BRANCH"
git branch -D $BRANCH
done
exit 0
elif [[ "$TREEBRANCH" =~ ^(main|master)$ ]]
then
git status -s | grep -E "^ M|^\?\?" > /dev/null
if [[ $? == 0 ]]
then
echo "You are on the main/master branch, HOWEVER changes have been detected!"
if [[ "$1" == -b ]] || [[ "$1" == --branch ]]
then
NB="$2"
else
read -p "Would you like to create a new branch to move the changes to? (y/n): " ANS
if [[ "$ANS" =~ ^(y|Y|)$ ]]
then
read -p "Enter a new branch name or leave blank to use the commit message: " NB
if [[ -z "$NB" ]]
then
NB=${1//[\ \:]/-}
fi
else
echo "Leaving changes on the main/master branch alone and exiting!"
exit 1
fi
fi
if [[ "$NB" =~ ^[-_A-Za-z0-9\(\)\/\\\$\%\#]+$ ]]
then
echo "Creating new branch '$NB' and migrating changes to this new feature branch."
git switch -c "$NB"
if [[ "$1" == -b ]] || [[ "$1" == --branch ]]
then
echo "Run gitgit 'commit description' to push the changes"
else
sleep 1
TREEBRANCH="$NB"
CM="$1"
commit
fi
exit 0
else
echo "Invalid Branch Name, exiting! You still have changes on your main/master branch!"
exit 1
fi
else
if [[ "$1" == -b ]] || [[ "$1" == --branch ]]
then
NB="$2"
branchit
else
echo "You are on the main branch, exiting..."
exit 1
fi
fi
elif [[ "$1" == -d ]] || [[ "$1" == --delete ]]
then
echo "Switching to $THEBRANCH, updating and DELETING $TREEBRANCH"
git checkout $THEBRANCH
git pull --ff-only
git fetch --prune
git branch -D $TREEBRANCH
elif [[ "$1" == -b ]] || [[ "$1" == --branch ]]
then
NB="$2"
branchit
else
CM="$1"
commit
fi