-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.sh
More file actions
109 lines (95 loc) · 3.01 KB
/
main.sh
File metadata and controls
109 lines (95 loc) · 3.01 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
alias gitrequest=_gitrequest
# USAGE: gitrequest BRANCH_NAME COMMIT_MESSAGE
# E.g. gitrequest fixspellingmistakebashtest "FIX: Help page spelling mistake"
function _gitrequest()
{
source ./config.sh
# Brew
hash brew 2>/dev/null 1>&2
if [ "${?:-0}" -eq 0 ]
then
echo -e "${COL_GRN}Detected brew is installed${COL_END}"
else
echo -e ${COL_MAG}'You need to install `brew` to use this alias. This can be done by running: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" '${COL_END}
echo -e ${COL_MAG}'Would you like to install `brew`? [y/n]'${COL_END}
read installhub
if [[ "$installhub" == "y" ]]
then
return 1;
else
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
fi
fi
# Hub
brew ls --versions hub 2>/dev/null 1>&2
if [ "${?:-0}" -eq 0 ]
then
echo -e "${COL_GRN}Detected hub is installed${COL_END}"
else
echo -e ${COL_MAG}'You need to install `hub` to use this alias. This can be done by running `brew install hub`'${COL_END}
echo -e ${COL_MAG}'Would you like to install `hub`? [Y/n]'${COL_END}
read installhub
if [[ "$installhub" == "n" ]]
then
return 1;
else
brew install hub
fi
fi
# Generate random hash
# Credit to 'earthgecko' @ https://gist.github.com/earthgecko/3089509
RAND_HASH=$(cat /dev/urandom | LC_CTYPE=C tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1)
# Determine branch prefix
if [[ $1 != ${KNOWN_PREFIX}* ]] # If the given branch does not start with the prefix, inject prefix
then
gitbranchprefix="$RAND_HASH"_
fi
# Checkout the desired branch
if ! git checkout -B $gitbranchprefix$1
then
echo -e ${COL_RED}$?${COL_END}
return 1
fi
# Commit staged changes
if [ ! -z "$2" ] # If the commit argument has been supplied
then
echo -e "${COL_GRN}Committing changes${COL_END}"
commitmsg="${2:-0}"
if ! git commit -m "$commitmsg" # Commit or echo error
then
echo -e ${COL_RED}$?${COL_END}
return 1
fi
else
echo -e "${COL_GRN}Commit message not supplied - not committing${COL_END}"
fi
# Push the branch from local to remote
echo -e "${COL_GRN}Pushing to branch $gitbranchprefix$1 on origin${COL_END}"
if ! git push -u origin $gitbranchprefix$1
then
echo -e ${COL_RED}$?${COL_END}
return 1
fi
# Create a pull request on remote
echo -e "${COL_GRN}Creating pull request on origin${COL_END}"
hub pull-request
if [ "${?:-0}" -ne 0 ]
then
echo -e "${COL_RED}Failed to create pull request on origin${COL_END}"
return 1
fi
# Checkout back to master
if [[ "$CHECKOUT_MASTER_AFTER_FINISH" == true ]]
then
echo -e "${COL_GRN}Checking out master${COL_END}"
git checkout master
if [ "${?:-0}" -ne 0 ]
then
echo -e "${COL_RED}Failed to checkout master${COL_END}"
return 1
fi
else
echo -e "${COL_YEL}WARNING: Remember that you're still on branch ${gitbranchprefix}${1}!${COL_END}"
fi
return 0
}