-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-aliases.sh
157 lines (126 loc) · 3.18 KB
/
git-aliases.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
# 0 = success
# 1 = error
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
NEW_LINE=$'\n'
# function _prompt {
# while true; do
# read -p "$* [y/n]: " yn
# case $yn in
# [Yy]*) return 0 ;;
# [Nn]*) echo -e "${RED}Aborted${NC}" ; return 1 ;;
# esac
# done
# }
function prompt() {
if read -q "choice?$1 [y/n]:"; then
echo -e "${NEW_LINE}${GREEN}Proceed${NC}"
return 0
else
echo -e "${NEW_LINE}${RED}Aborted${NC}"
return 1
fi
}
# get current status of repo
function status() {
if output=$(git status --porcelain) && [ -z "$output" ]; then
return 0
else
git status
return 1
fi
}
# get current branch name
function branchName() {
BRANCH=`git rev-parse --abbrev-ref HEAD`
echo ${BRANCH}
}
# deny if branch is master or develop
function isBranchAllowed() {
BRANCH=`branchName`
if [ "${BRANCH}" = "master" ] || [ "${BRANCH}" = "develop" ]; then
echo -e "${RED}Stop! This action is not allowed on the ${BRANCH} branch${NC}"
echo -e "${RED}Aborted${NC}"
return 1
else
return 0
fi
}
# fetch, rebase
function gitFr() {
isBranchAllowed || return 1
status || return 1
BRANCH=`branchName`
echo -e "${YELLOW}You will update the following branch: ${GREEN}${BRANCH}${NC}"
prompt "Would you like to continue?"
RETURN_STATUS=$?
if [ ${RETURN_STATUS} -eq 0 ]; then
git fetch --prune
git rebase origin/master
fi
return ${RETURN_STATUS}
}
# checkout, fetch, rebase
function gUpdate() {
if [ ! -z "$1" ]; then
git checkout $1
fi
gitFr
RETURN_STATUS=$?
return ${RETURN_STATUS}
}
# checkout, fetch, rebase, push
function gUpdatePush() {
gUpdate "$@" || return 1
echo -e "${YELLOW}Branch updated, Would you like to push it to remote?${NC}"
prompt
if [ $? -eq 0 ]; then
git push --force-with-lease
fi
}
# checkout master, pull, create and checkout new local branch
function gStartWork() {
if [ -z "$1" ]; then
echo -e "${YELLOW}Please provide a branch name${NC}"
echo -e "${RED}Aborted${NC}"
else
git checkout master
git pull
git checkout -b $1
fi
}
# new aliases -----------------
function gLog() {
git log --graph --decorate --pretty=oneline --abbrev-commit
}
function gReset() {
if [ ! -z "$1" ]; then
git checkout $1
fi
BRANCH=`branchName`
echo -e "${YELLOW}You will reset the following branch: ${GREEN}${BRANCH}${NC}"
echo -e "${YELLOW}To match remote. Would you like to continue?${NC}"
prompt
if [ $? -eq 0 ]; then
git reset --hard origin/${BRANCH}
fi
}
function gUpdateSquash() {
if [ ! -z "$1" ]; then
git checkout $1
fi
isBranchAllowed || return 1
status || return 1
BRANCH=`branchName`
echo -e "${YELLOW}You will update and squash the following branch: ${GREEN}${BRANCH}${NC}"
echo -e "${YELLOW}Would you like to continue?${NC}"
prompt
RETURN_STATUS=$?
if [ ${RETURN_STATUS} -eq 0 ]; then
git fetch --prune
git rebase -i origin/master
fi
}