-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgit-bot.sh
More file actions
167 lines (143 loc) · 3.64 KB
/
git-bot.sh
File metadata and controls
167 lines (143 loc) · 3.64 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
#!/usr/bin/env bash
# title : git-bot.sh
# description : Automates committing and pushing of code
# author : tbking <[email protected]>
# usage : bash mkscript.sh
# licence : MIT
# Continue the process if success
proceedIfSuccess() {
if [ 0 -eq $? ]; then
echo "SUCCESS: $1"
else
echo "FAILURE: $1"
exit 1
fi;
}
# Log the status
logStatus() {
if [ 0 -eq $? ]; then
echo "SUCCESS: $1"
else
echo "FAILURE: $1"
fi;
}
# Returns current date
getDate() {
date +%d/%m/%Y
}
# Returns current time
getTime() {
date +"%T"
}
# Asserts the existance of Git
assertGit() {
echo "checking git..."
git --version
proceedIfSuccess "checking git"
}
## cd to project repo
changeToProjectRepo() {
cd $LOCAL_REPO
if [ -z "$LOCAL_REPO" ]; then
cd $1
else
cd $LOCAL_REPO
fi
}
# Get current git branch name
getCurrentGitBranch() {
echo `git branch | sed -n -e 's/^\* \(.*\)/\1/p'`
}
# Check for uncommitted changes in the repo
checkForUncommitedChanges() {
echo "checking for uncommited changes..." >&2
# Checks the output of git diff to determine if there's any uncommitted change
if (git diff --exit-code &>/dev/null && git diff --cached --exit-code &> /dev/null); then
echo "no uncommited changes found" >&2
echo 0
else
echo "uncommited changes found" >&2
echo 1
fi
}
# Check for local commits in the repo
checkForLocalCommits() {
echo "checking for local commits..." >&2
# Checks if git status shows that the current branch is 'ahead' of remote one
RESULT=`git status -sb`
if grep -q "ahead" <<<$RESULT; then
echo "local commits found" >&2
echo 1
else
echo "no local commits found" >&2
echo 0
fi
}
# Generate a new branch name for today
generateNewBranch() {
echo "automatic-branch-`getDate`"
}
# Generate a new commit message for now
generateCommitMsg() {
echo "Automatic interim commit - `getDate` `getTime`"
}
# Switches to provided branch name
switchToBranch() {
echo "switching to branch: $1..."
# If branch doesn't exist, create one
git checkout "$1" &>/dev/null || git checkout -b "$1" &>/dev/null
logStatus "switching to branch: $1"
}
# Commits code
commitCode() {
echo "committing code..."
git commit -a -m "$1" --no-gpg-sign
logStatus "committing code"
}
# Pushes code
pushCode() {
echo "pushing code..."
git push origin $GIT_NEW_BRANCH -f
logStatus "pushing code"
}
# Announce the running of the script
echo "=-=-=-=-= git-bot `getDate` `getTime` =-=-=-=-="
# Make sure git exists on the system
assertGit
# Take the first argument as path to
changeToProjectRepo $1
# Get the current git branch before script runs
GIT_BRANCH=`getCurrentGitBranch`
# Stores 1 for uncommitted changes and 0 for vice versa
UNCOMMITTED_CHANGES=`checkForUncommitedChanges`
if [ 1 -eq "$UNCOMMITTED_CHANGES" ]; then
# Generate new branch name
GIT_NEW_BRANCH=`generateNewBranch`
# Switch to new branch
switchToBranch "$GIT_NEW_BRANCH"
# Generate commit message
GIT_COMMIT_MSG=`generateCommitMsg`
# Commit the uncommitted code
commitCode "$GIT_COMMIT_MSG"
# Push the new branch
pushCode
# Switch back to previous branch
switchToBranch "$GIT_BRANCH"
# Exit the script successfully
exit 0
fi
# Stores 1 for uncommitted changes and 0 for vice versa
LOCAL_COMMITS=`checkForLocalCommits $GIT_BRANCH`
if [ 1 -eq "$LOCAL_COMMITS" ]; then
# Generate new branch name
GIT_NEW_BRANCH=`generateNewBranch`
# Switch to new branch
switchToBranch "$GIT_NEW_BRANCH"
# Push the new branch
pushCode
# Switch back to previous branch
switchToBranch "$GIT_BRANCH"
# Exit the script successfully
exit 0
fi
echo "nothing to do here. good dev!"