-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgit.bash.functions
114 lines (102 loc) · 3.55 KB
/
git.bash.functions
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
#!/bin/echo Do not execute/subshell me. Source me.
__IS_YES='y|Y'
function ghq-cd() {
if [ $# -eq 0 ] ; then
echo Tries to cd into a ghq repo.
echo See: https://github.com/motemen/ghq
echo 'Usage: gh-cl <search pattern>'
else
cd $(ghq list -p $1 | head -n 1)
fi
}
function ghq-cl() {
if [ $# -eq 0 ] ; then
echo Tries to clone a repo from ghq cache.
echo See: https://github.com/motemen/ghq
echo 'Usage: gh-cl <search pattern>'
echo
git clone $(ghq list -p $1 | head -n 1)
fi
}
function git-follow-remote(){
local remote=${1:-origin}
git branch --set-upstream-to=${remote}/$(git_current_branch)
}
function git-reset-to-remote(){
echo "This will blow away this branch, leaving you with only revlist to recover"
if confirm "Do you still want to reset back to remote" ; then
git checkout $(what-is-upstream)
fi
}
function git-clone-private() {
REMOTE_REPO=${1:?Please specify a repo}
REMOTE_HOST=${2:-github.com}
git clone git@${REMOTE_HOST}:${REMOTE_REPO}
}
function git-clean-pullrequest-branches() {
local REMOTE=${1:-origin} IGNORE_PATTERN=${2:-master}
echo I am going to delete these:
prs=( $(git branch -a | grep remotes/$REMOTE | sed -E "/HEAD|$IGNORE_PATTERN/d") )
for p in ${prs[@]} ; do
echo ${p}
done
if confirm "Is this okay" ; then
echo 'Deleting...'
for p in ${prs[@]} ; do
p=$(echo $p | sed "s-remotes/$REMOTE/--")
git push --delete $REMOTE $p
done
echo "You should run 'git remote prune $REMOTE' if you don't need local caches"
fi
}
function git-setup-pullrequest-branch() {
PULLREQUEST=${1:?Please specify a branch name for your pull request}
REMOTE=${2:-origin}
CURRENT=$(git rev-parse --abbrev-ref HEAD)
git branch --set-upstream-to=${REMOTE}/${PULLREQUEST} 2>/dev/null
if [ $? -ne 0 ] ; then
if confirm "Create pull request branch ${REMOTE}/${PULLREQUEST}" ; then
git push -u $REMOTE ${CURRENT}:$PULLREQUEST
fi
fi
PUSH_DEFAULT=$(git config --global push.default)
if [[ "$PUSH_DEFAULT" != "upstream" ]] ; then
echo "Uh oh, you do not have your push settings set to 'upstream'"
echo "You should run 'git config --global push.default upstream'"
echo "Otherwise, when you run 'git push' you might get errors..."
fi
}
function git-close-pullrequest-branch() {
MASTER=${1:-master}
REMOTE=${2:-origin}
UPSTREAM=$(git rev-parse --abbrev-ref --symbolic-full-name @{u})
CURRENT=$(git rev-parse --abbrev-ref HEAD)
if confirm "Push $UPSTREAM to branch $MASTER on ${REMOTE}" ; then
git push $REMOTE ${UPSTREAM}:${MASTER}
else
return 0
fi
if confirm "Would you like to set your upstream back to ${REMOTE}/${MASTER}" ; then
git branch --set-upstream-to=${REMOTE}/${MASTER} 2>/dev/null
fi
}
function git-clean-gone-branches() {
local prs=$(git branch -lvv | grep gone | sed -E '/^\*/d' )
if [ -z "${prs}" ] ; then return 0 ; fi
echo I am going to delete these:
echo ${prs}
if confirm 'Is that okay' ; then
echo 'Deleting...'
git branch -D $(echo $prs | awk '{print $1}')
fi
}
function git-push-bravely() {
local ups=$(what-is-upstream)
if [[ $ups == "master" || $ups == "main" || $ups == "production" || $ups == "prod" ]] ; then
echo 'what-is-upstream == "'$ups'" ? !!!'
echo ' o_O '
echo 'NopeNopeNopeNopeNopeNopeNopeNopeNopeNopeNopeNope'
else
git push -f
fi
}