-
Notifications
You must be signed in to change notification settings - Fork 3
/
.bashrc
164 lines (155 loc) ยท 6.89 KB
/
.bashrc
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
# Open NPM
npmjs() {
gitURL="$(git config --get remote.origin.url)"
gitURL="${gitURL%.git}" # Remove .git from the end of the git URL
if [[ ! -z $1 ]]; then
module="$(echo $1 | tr '[:upper:]' '[:lower:]')" # Lowercase the module name for use with npm
open https://www.npmjs.com/package/"$module"
elif [[ $gitURL =~ ^git@ ]]; then
gitURL="$(echo $gitURL | sed 's/git@//')" # Remove git@ from the start of the git URL
repo="$(echo $gitURL | sed 's/.*\///')" # Pull the repo name from the git URL
repo="$(echo $repo | tr '[:upper:]' '[:lower:]')" # Lowercase the repo for use with npm
open https://www.npmjs.com/package/"$repo"
elif [[ $gitURL =~ ^https?:// ]]; then
repo="$(echo $gitURL | sed 's/.*\.com\///' | sed 's/.*\///')" # Pull the repo name from the git URL
repo="$(echo $repo | tr '[:upper:]' '[:lower:]')" # Lowercase the repo for use with npm
open https://www.npmjs.com/package/"$repo"
else
open https://www.npmjs.com/
fi
echo "๐ฆ Opened npm in browser"
}
# Clone any repo and cd into it
clone() {
cloneUsage() {
echo "\nโ ๏ธ Usage:\nclone <url*> <dir>\nclone <org*>/<repo*> <dir>\nclone <org*> <repo*> <dir>\n* denotes required arguments"
}
gitClone() {
CYAN="\033[1;36m"
NOCOLOR="\033[0m"
echo "๐ค Cloning $1"
git clone $2 $directory && cd $directory || (cloneUsage && return 1)
# If an error code was returned from the last command, return an error code
if [[ "$?" == 1 ]]; then
return 1
fi
echo "๐ Moved to directory ${CYAN}$directory${NOCOLOR}"
}
if [[ -z $1 ]]; then
cloneUsage
return 1
fi
gitURL="$1"
gitURL="${gitURL%.git}" # Remove .git from the end of the git URL
if [[ $gitURL =~ ^git@ ]]; then
gitURL="$(echo $gitURL | sed 's/git@//')" # Remove git@ from the start of the git URL
org="$(echo $gitURL | sed 's/.*\://' | sed 's/\/.*//')" # Pull the org from the git URL
repo="$(echo $gitURL | sed 's/.*\///')" # Pull the repo name from the git URL
directory=${2:-"$repo"}
gitClone "$org/$repo" $1
elif [[ $gitURL =~ ^https?:// ]]; then
# Force SSH
github="$(echo $gitURL | cut -d'/' -f3)"
org="$(echo $gitURL | sed 's/.*\.com\///' | sed 's/\/.*//')" # Pull the org from the git URL
repo="$(echo $gitURL | sed 's/.*\.com\///' | sed 's/.*\///')" # Pull the repo name from the git URL
directory=${2:-"$repo"}
gitClone "$org/$repo" "git@$github:$org/$repo.git"
elif [[ ! -z $1 && ! -z $2 && "$1" != *\/* ]]; then
directory=${3:-"$2"}
gitClone "$1/$2" "[email protected]:$1/$2.git" # Replace with GitHub Enterprise URL (if applicable)
else
repo="$(echo $1 | sed 's/.*\///')"
directory=${2:-"$repo"}
gitClone "$1" "[email protected]:$1.git" # Replace with GitHub Enterprise URL (if applicable)
fi
}
# Fork a GitHub repo
ghfork() {
gitURL=${1:-"$(git config --get remote.origin.url)"}
gitURL="${gitURL%.git}" # Remove .git from the end of the git URL
if [[ $gitURL =~ ^git@ ]]; then
gitURL="$(echo $gitURL | sed 's/git@//')" # Remove git@ from the start of the git URL
repo="$(echo $gitURL | sed 's/.*\///')" # Pull the repo name from the git URL
elif [[ $gitURL =~ ^https?:// ]]; then
repo="$(echo $gitURL | sed 's/.*\.com\///' | sed 's/.*\///')" # Pull the repo name from the git URL
elif [[ ! -z $1 && ! -z $2 && "$1" != *\/* ]]; then
repo="$2"
else
repo="$(echo $1 | sed 's/.*\///')"
fi
github "$@"
echo "๐ด Click 'Fork'"
user=$(whoami)
print -z clone "$user" "$repo"
}
# Open GitHub
github() {
gitURL="$(git config --get remote.origin.url)"
gitURL="${gitURL%.git}" # Remove .git from the end of the git URL
if [[ ! -z $1 && ! -z $2 ]]; then
open https://github.com/"$1"/"$2" # Replace with GitHub Enterprise URL (if applicable)
elif [[ $gitURL =~ ^git@ ]]; then
branch=${1:-"$(git symbolic-ref --short HEAD)"}
branchExists="$(git ls-remote --heads $gitURL $branch | wc -l)"
github="$(echo $gitURL | sed 's/git@//')" # Remove git@ from the start of the git URL
github="$(echo $github | sed 's/\:/\//')" # Replace the : between the URL and org with a / in the URL for GitHub
if [[ $branchExists == " 1" ]]; then
open http://"$github"/tree/"$branch"
else
open http://"$github"
fi
elif [[ $gitURL =~ ^https?:// ]]; then
branch=${1:-"$(git symbolic-ref --short HEAD)"}
branchExists="$(git ls-remote --heads $gitURL $branch | wc -l)"
if [[ $branchExists == " 1" ]]; then
open "$gitURL"/tree/"$branch"
else
open "$gitURL"
fi
else
open https://github.com/"$1" # Replace with GitHub Enterprise URL (if applicable)
fi
echo "๐ Opened GitHub in browser"
}
# Open the pull requests for a repo
pulls() {
gitURL="$(git config --get remote.origin.url)"
gitURL="${gitURL%.git}" # Remove .git from the end of the git URL
if [[ ! -z $1 && ! -z $2 ]]; then
open https://github.com/"$1"/"$2"/pulls # Replace with GitHub Enterprise URL (if applicable)
elif [[ $gitURL =~ ^git@ ]]; then
gitURL="$(echo $gitURL | sed 's/git@//')" # Remove git@ from the start of the git URL
github="$(echo $gitURL | sed 's/\:/\//')" # Replace the : between the URL and org with a / in the URL for GitHub
open http://"$github"/pulls
elif [[ $gitURL =~ ^https?:// ]]; then
open "$gitURL"/pulls
else
open https://github.com/pulls # Replace with GitHub Enterprise URL (if applicable)
fi
echo "๐ Opened GitHub pull requests"
}
# Create a pull request for a repo
pr() {
gitURL="$(git config --get remote.origin.url)"
gitURL="${gitURL%.git}" # Remove .git from the end of the git URL
if [[ $gitURL =~ ^git@ ]]; then
gitURL="$(echo $gitURL | sed 's/git@//')" # Remove git@ from the start of the git URL
github="$(echo $gitURL | sed 's/\:/\//')" # Replace the : between the URL and org with a / in the URL for GitHub
branch="$(git symbolic-ref --short HEAD)"
open http://"$github"/compare/${1:-master}...${2:-"$branch"}?expand=1
elif [[ $gitURL =~ ^https?:// ]]; then
branch="$(git symbolic-ref --short HEAD)"
open "$gitURL"/compare/${1:-master}...${2:-"$branch"}?expand=1
elif [[ ! -z $1 && ! -z $2 ]]; then
open https://github.com/"$1"/"$2"/compare/${3:-master}...${4:-master}?expand=1 # Replace with GitHub Enterprise URL (if applicable)
else
echo "โ ๏ธ Usage: pr <org*> <repo*> <base-branch> <branch-to-compare>"
return 1
fi
echo "โ๏ธ Creating GitHub pull request"
}
# Review and test a pull request for a repo
review() {
git fetch origin pull/$1/head:${2:-$1}
git checkout ${2:-$1}
}