-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbash_git
executable file
·205 lines (172 loc) · 5.38 KB
/
bash_git
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
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/bash
##############################################################
_git_current_branch() {
git branch | awk '/\*/ {print $NF}'
}
export DEFAULT_PARENT_BRANCH_PROVIDER=_git_current_branch
###############################################################
# Create a git branch (local & remote) under a given parent.
# If parent not specified tries to read parent value from
# DEFAULT_PARENT_BRANCH environment variable, if this is not set
# creates the branch from current branch.
gb() {
local parent
if [[ $# -lt 1 ]]; then
echo "Usage: gb <branch-name> [parent-branch-name (optional)]"
return
fi
parent=${2:-"$($DEFAULT_PARENT_BRANCH_PROVIDER)"}
git checkout $parent
git pull
git checkout -b $1 refs/heads/$parent
git push -u origin refs/heads/$1
}
# gp - GitHub Pull Request creation cli tool.
# gp -h for more options.
# environment variables to configure the behavior and its default values.
# GIT_BASE_URL - https://github.com
# DEFAULT_PARENT_BRANCH - master
# DEFAULT_PR_LABEL - '' (Not defined by default)
# DEFAULT_PR_TITLE_PROVIDER - '' (Not defined by default)
# - This should be a method which receives SRC branch
# as an argument and should return a title for the PR
#
gp() {
local ORG PROJECT SRC TARGET OPTIND c git_url pr_title
while getopts 'p:s:t:h' c
do
case $c in
o) ORG=$OPTARG ;;
p) PROJECT=$OPTARG ;;
s) SRC=$OPTARG ;;
t) TARGET=$OPTARG ;;
h) echo "Usage: gp [-o ORG] [ -p PROJECT ] [ -s SRC ] [ -t TARGET ]"; return
esac
done
if [ -z "$GIT_BASE_URL" ]; then
GIT_BASE_URL="https://github.com"
fi
if [ -z "$DEFAULT_PARENT_BRANCH" ]; then
DEFAULT_PARENT_BRANCH="master"
fi
if [ -z "$SRC" ]; then
SRC=`git branch | awk '/\*/ {print $NF}'`
fi
if [ -z "$TARGET" ]; then
TARGET=$DEFAULT_PARENT_BRANCH
fi
if [ -z "$PROJECT" ]; then
PROJECT=`git config --get remote.origin.url | awk -F '/' '{print $NF}' | cut -d\. -f1`
fi
if [ -z "$ORG" ]; then
ORG=`git config --get remote.origin.url | awk '{print substr($1, match($1,":.+\/")+1, RLENGTH-2)}'`
fi
if [ "$DEFAULT_PR_TITLE_PROVIDER" ]; then
pr_title=`$DEFAULT_PR_TITLE_PROVIDER $SRC | sed 's/\ /+/g'`
fi
git_url="$GIT_BASE_URL/$ORG/$PROJECT/compare/$TARGET...$SRC?expand=1&labels=$DEFAULT_PR_LABEL&title=$pr_title"
open_url "$git_url"
}
# Deprecated
# git commit with message as task id and title from JIRA.
# Custom commit message also can be appended.
# This depends on the jira_info command (which is a python module by me)
gcm() {
local task_id task_description cm ccm OPTIND
while getopts 'm:h' c
do
case $c in
m) ccm=$OPTARG ;;
h) echo "Usage: gcm [ -m custom message ]"; return
esac
done
task_id=`git branch | awk '/\*/ {print $NF}' | cut -d- -f1-2 | tr '[a-z]' '[A-Z]'`
task_description=`jira_info $task_id`
cm="$task_id $task_description"
if [ -n "$ccm" ]; then
cm="$cm - $ccm"
fi
git commit -m "$cm"
}
# Cleaning local git workspace by deleting all the local branches.
# This will not delete the current branch. Deletes only the pattern
# provided by PROJECT_PREFIX_REGEX environment variable
glc() {
echo "Deleting only the local git branches..."
for i in `git branch | awk -v p="$PROJECT_PREFIX_REGEX" '$0 ~ p && !/\*/ {print $NF}'`; do
git branch -D $i
done
echo "Done"
}
# Caution: This function deletes all the remote and local branches
# identified by PROJECT_PREFIX_REGEX environment variable.
gbc() {
local c
echo "Deleting git branches (both remote and local) with $PROJECT_PREFIX_REGEX identifier"
echo "Are you sure? (y/n)"
read c
if [ $c != "y" ];then
echo "No operation done. quiting..."
return
fi
echo "Started deletion process..."
git checkout master
for i in `git remote show origin | awk -v p="$PROJECT_PREFIX_REGEX" '$0 ~ p {print $1}' | sort | uniq`; do
git branch -D $i
git push origin --delete $i
done
echo "Done"
}
# Delete local and remote branch
gbd() {
if [[ $# != 1 ]]; then
echo "Usage: gbd <branch-name>"
return
fi
echo "Caution:: Deleting the branch $1 ..."
git checkout master
git branch -D $1
git push origin --delete $1
}
# Delete local and remote tag
gtd() {
if [[ $# != 1 ]]; then
echo "Usage: gtd <tag-name>"
return
fi
echo "Caution:: Deleting the tag $1 ..."
git tag -d $1
git push origin :refs/tags/$1
}
# Point existing tag to latest commit
gtl() {
if [[ $# != 1 ]]; then
echo "Usage: gtl <tag-name>"
return
fi
echo "Caution:: Deleting the existing tag locally and remotely $1 ..."
gtd $1
echo "Tag deleted successfully $1"
echo "Creating annoted tag.Add the tag message in the next line.."
git tag -a $1
git push origin $1
}
# Swith to home branch defined by GIT_HOME_BRANCH environment variable.
# and pull the latest changes.
ghome() {
git checkout $GIT_HOME_BRANCH
git pull
}
##### Git aliases ######
gitt() {
echo "Git alias init"
alias s="git status"
alias b="git branch"
alias bv="git branch -vv"
alias d="git diff"
alias dc="git diff --cached"
alias gl="git log --graph --oneline --all"
alias glist='for ref in $(git for-each-ref --sort=-committerdate --format="%(refname)" refs/heads/ refs/remotes ); do git log -n1 $ref --pretty=format:"%Cgreen%cr%Creset %C(yellow)%d%Creset %C(bold blue)<%an>%Creset%n" | cat ; done | awk '"'! a["'$0'"]++'"
alias grblist='git fetch origin >/dev/null 2>/dev/null && for branch in `git branch -r | grep -v HEAD`;do echo -e `git show --format="%ci %cr" $branch | head -n 1` \\t$branch; done | sort -r'
}
##### End of Git #######