Skip to content

Commit 10b1de5

Browse files
committed
bash-functions ๐Ÿ”ฎ๐ŸŽ‰
0 parents  commit 10b1de5

File tree

3 files changed

+374
-0
lines changed

3 files changed

+374
-0
lines changed

โ€Ž.bashrcโ€Ž

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# Open NPM
2+
npmjs() {
3+
gitURL="$(git config --get remote.origin.url)"
4+
gitURL="${gitURL%.git}" # Remove .git from the end of the git URL
5+
if [[ ! -z $1 ]]; then
6+
module="$(echo $1 | tr '[:upper:]' '[:lower:]')" # Lowercase the module name for use with npm
7+
open https://www.npmjs.com/package/"$module"
8+
elif [[ $gitURL =~ ^git@ ]]; then
9+
gitURL="$(echo $gitURL | sed 's/git@//')" # Remove git@ from the start of the git URL
10+
repo="$(echo $gitURL | sed 's/.*\///')" # Pull the repo name from the git URL
11+
repo="$(echo $repo | tr '[:upper:]' '[:lower:]')" # Lowercase the repo for use with npm
12+
open https://www.npmjs.com/package/"$repo"
13+
elif [[ $gitURL =~ ^https?:// ]]; then
14+
repo="$(echo $gitURL | sed 's/.*\.com\///' | sed 's/.*\///')" # Pull the repo name from the git URL
15+
repo="$(echo $repo | tr '[:upper:]' '[:lower:]')" # Lowercase the repo for use with npm
16+
open https://www.npmjs.com/package/"$repo"
17+
else
18+
open https://www.npmjs.com/
19+
fi
20+
echo "๐Ÿ“ฆ Opened npm in browser"
21+
}
22+
23+
# Clone any repo and cd into it
24+
clone() {
25+
cloneUsage() {
26+
echo "\nโš ๏ธ Usage:\nclone <url*> <dir>\nclone <org*>/<repo*> <dir>\nclone <org*> <repo*> <dir>\n* denotes required arguments"
27+
}
28+
29+
gitClone() {
30+
CYAN="\033[1;36m"
31+
NOCOLOR="\033[0m"
32+
echo "๐Ÿค– Cloning $1"
33+
git clone $2 $directory && cd $directory || (cloneUsage && return 1)
34+
# If an error code was returned from the last command, return an error code
35+
if [[ "$?" == 1 ]]; then
36+
return 1
37+
fi
38+
echo "๐Ÿšš Moved to directory ${CYAN}$directory${NOCOLOR}"
39+
}
40+
41+
if [[ -z $1 ]]; then
42+
cloneUsage
43+
return 1
44+
fi
45+
46+
gitURL="$1"
47+
gitURL="${gitURL%.git}" # Remove .git from the end of the git URL
48+
if [[ $gitURL =~ ^git@ ]]; then
49+
gitURL="$(echo $gitURL | sed 's/git@//')" # Remove git@ from the start of the git URL
50+
org="$(echo $gitURL | sed 's/.*\://' | sed 's/\/.*//')" # Pull the org from the git URL
51+
repo="$(echo $gitURL | sed 's/.*\///')" # Pull the repo name from the git URL
52+
directory=${2:-"$repo"}
53+
gitClone "$org/$repo" $1
54+
elif [[ $gitURL =~ ^https?:// ]]; then
55+
# Force SSH
56+
github="$(echo $gitURL | cut -d'/' -f3)"
57+
org="$(echo $gitURL | sed 's/.*\.com\///' | sed 's/\/.*//')" # Pull the org from the git URL
58+
repo="$(echo $gitURL | sed 's/.*\.com\///' | sed 's/.*\///')" # Pull the repo name from the git URL
59+
directory=${2:-"$repo"}
60+
gitClone "$org/$repo" "git@$github:$org/$repo.git"
61+
elif [[ ! -z $1 && ! -z $2 && "$1" != *\/* ]]; then
62+
directory=${3:-"$2"}
63+
gitClone "$1/$2" "git@github.com:$1/$2.git" # Replace with GitHub Enterprise URL (if applicable)
64+
else
65+
repo="$(echo $1 | sed 's/.*\///')"
66+
directory=${2:-"$repo"}
67+
gitClone "$1" "git@github.com:$1.git" # Replace with GitHub Enterprise URL (if applicable)
68+
fi
69+
}
70+
71+
# Fork a GitHub repo
72+
ghfork() {
73+
gitURL=${1:-"$(git config --get remote.origin.url)"}
74+
gitURL="${gitURL%.git}" # Remove .git from the end of the git URL
75+
if [[ $gitURL =~ ^git@ ]]; then
76+
gitURL="$(echo $gitURL | sed 's/git@//')" # Remove git@ from the start of the git URL
77+
repo="$(echo $gitURL | sed 's/.*\///')" # Pull the repo name from the git URL
78+
elif [[ $gitURL =~ ^https?:// ]]; then
79+
repo="$(echo $gitURL | sed 's/.*\.com\///' | sed 's/.*\///')" # Pull the repo name from the git URL
80+
elif [[ ! -z $1 && ! -z $2 && "$1" != *\/* ]]; then
81+
repo="$2"
82+
else
83+
repo="$(echo $1 | sed 's/.*\///')"
84+
fi
85+
github "$@"
86+
echo "๐Ÿด Click 'Fork'"
87+
user=$(whoami)
88+
print -z clone "$user" "$repo"
89+
}
90+
91+
# Open GitHub
92+
github() {
93+
gitURL="$(git config --get remote.origin.url)"
94+
gitURL="${gitURL%.git}" # Remove .git from the end of the git URL
95+
if [[ ! -z $1 && ! -z $2 ]]; then
96+
open https://github.com/"$1"/"$2" # Replace with GitHub Enterprise URL (if applicable)
97+
elif [[ $gitURL =~ ^git@ ]]; then
98+
branch=${1:-"$(git symbolic-ref --short HEAD)"}
99+
branchExists="$(git ls-remote --heads $gitURL $branch | wc -l)"
100+
github="$(echo $gitURL | sed 's/git@//')" # Remove git@ from the start of the git URL
101+
github="$(echo $github | sed 's/\:/\//')" # Replace the : between the URL and org with a / in the URL for GitHub
102+
if [[ $branchExists == " 1" ]]; then
103+
open http://"$github"/tree/"$branch"
104+
else
105+
open http://"$github"
106+
fi
107+
elif [[ $gitURL =~ ^https?:// ]]; then
108+
branch=${1:-"$(git symbolic-ref --short HEAD)"}
109+
branchExists="$(git ls-remote --heads $gitURL $branch | wc -l)"
110+
if [[ $branchExists == " 1" ]]; then
111+
open "$gitURL"/tree/"$branch"
112+
else
113+
open "$gitURL"
114+
fi
115+
else
116+
open https://github.com/"$1" # Replace with GitHub Enterprise URL (if applicable)
117+
fi
118+
echo "๐Ÿ™ Opened GitHub in browser"
119+
}
120+
121+
# Open the pull requests for a repo
122+
pulls() {
123+
gitURL="$(git config --get remote.origin.url)"
124+
gitURL="${gitURL%.git}" # Remove .git from the end of the git URL
125+
if [[ ! -z $1 && ! -z $2 ]]; then
126+
open https://github.com/"$1"/"$2"/pulls # Replace with GitHub Enterprise URL (if applicable)
127+
elif [[ $gitURL =~ ^git@ ]]; then
128+
gitURL="$(echo $gitURL | sed 's/git@//')" # Remove git@ from the start of the git URL
129+
github="$(echo $gitURL | sed 's/\:/\//')" # Replace the : between the URL and org with a / in the URL for GitHub
130+
open http://"$github"/pulls
131+
elif [[ $gitURL =~ ^https?:// ]]; then
132+
open "$gitURL"/pulls
133+
else
134+
open https://github.com/pulls # Replace with GitHub Enterprise URL (if applicable)
135+
fi
136+
echo "๐Ÿ”ƒ Opened GitHub pull requests"
137+
}
138+
139+
# Create a pull request for a repo
140+
pr() {
141+
gitURL="$(git config --get remote.origin.url)"
142+
gitURL="${gitURL%.git}" # Remove .git from the end of the git URL
143+
if [[ $gitURL =~ ^git@ ]]; then
144+
gitURL="$(echo $gitURL | sed 's/git@//')" # Remove git@ from the start of the git URL
145+
github="$(echo $gitURL | sed 's/\:/\//')" # Replace the : between the URL and org with a / in the URL for GitHub
146+
branch="$(git symbolic-ref --short HEAD)"
147+
open http://"$github"/compare/${1:-master}...${2:-"$branch"}?expand=1
148+
elif [[ $gitURL =~ ^https?:// ]]; then
149+
branch="$(git symbolic-ref --short HEAD)"
150+
open "$gitURL"/compare/${1:-master}...${2:-"$branch"}?expand=1
151+
elif [[ ! -z $1 && ! -z $2 ]]; then
152+
open https://github.com/"$1"/"$2"/compare/${3:-master}...${4:-master}?expand=1 # Replace with GitHub Enterprise URL (if applicable)
153+
else
154+
echo "โš ๏ธ Usage: pr <org*> <repo*> <base-branch> <branch-to-compare>"
155+
return 1
156+
fi
157+
echo "โœ๏ธ Creating GitHub pull request"
158+
}
159+
160+
# Review and test a pull request for a repo
161+
review() {
162+
git fetch origin pull/$1/head:${2:-$1}
163+
git checkout ${2:-$1}
164+
}

โ€ŽREADME.mdโ€Ž

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
# bash-functions
2+
3+
A collection of useful Bash/ZSH functions. Just drop the functions from [.bashrc](.bashrc) at the end of your `.bashrc` or `.zshrc`. These work great with the [MacBook touch bar](#-iterm2-touch-bar-support) and [iTerm2](https://www.iterm2.com/).
4+
5+
## Usage
6+
7+
### ๐Ÿ“ฆ Open npmjs.com
8+
9+
From anywhere:
10+
11+
```bash
12+
npmjs # opens npmjs.com
13+
npmjs <module> # opens npmjs.com for npm module <module>
14+
```
15+
16+
From a git repo:
17+
18+
```bash
19+
npmjs # opens npmjs.com for npm module <git-repo>
20+
```
21+
22+
### ๐Ÿค– Clone any repo and ๐Ÿšš cd into it
23+
24+
From anywhere:
25+
26+
```bash
27+
clone url # clone a repo from any git URL and cd into it
28+
clone org repo # clone a repo from GitHub and cd into it
29+
clone org/repo # clone a repo from GitHub and cd into it
30+
```
31+
32+
### ๐Ÿด Fork a GitHub repo
33+
34+
From anywhere:
35+
36+
```bash
37+
ghfork url # fork a repo from any GitHub URL and cd into it
38+
ghfork org repo # fork from GitHub and cd into it
39+
ghfork org/repo # fork from GitHub and cd into it
40+
```
41+
42+
### ๐Ÿ™ Open GitHub
43+
44+
From anywhere:
45+
46+
```bash
47+
github # opens GitHub
48+
github org # opens GitHub for org
49+
github org/repo # opens GitHub for org/repo
50+
github org repo # opens GitHub for org/repo
51+
```
52+
53+
From a git repo:
54+
55+
```bash
56+
github # opens this repo in GitHub for current branch
57+
github <branch> # opens this repo in GitHub for specified branch
58+
```
59+
60+
### ๐Ÿ”ƒ Open the pull requests for a repo
61+
62+
From anywhere:
63+
64+
```bash
65+
pulls # opens GitHub for all pull requests for user
66+
pulls org repo # opens GitHub for all pull requests for org/repo
67+
```
68+
69+
From a git repo:
70+
71+
```bash
72+
pulls # opens GitHub for all pull requests for this repo
73+
```
74+
75+
### โœ๏ธ Create a pull request for a repo
76+
77+
From anywhere:
78+
79+
```bash
80+
pr <org> <repo> <base> <branch> # opens GitHub to draft a pull request in org/repo to merge <branch> into <base>
81+
```
82+
83+
From a git repo:
84+
85+
```bash
86+
pr # opens GitHub to draft a pull request in this repo to merge the current branch into master
87+
pr <base> # opens GitHub to draft a pull request in this repo to merge the current branch into <base>
88+
pr <base> <branch> # opens GitHub to draft a pull request in this repo to merge <branch> into <base>
89+
```
90+
91+
### ๐Ÿ”ฌ Review and test a pull request for a repo
92+
93+
From a git repo:
94+
95+
```bash
96+
review <number> # checks out PR #<number> into a local branch named <number> for review/testing
97+
review <number> <branch> # checks out PR #<number> into a local branch named <branch> for review/testing
98+
```
99+
100+
## ๐Ÿ’ป iTerm2 Touch Bar Support
101+
102+
![iTerm2 Touch Bar](./assets/iterm2-touch-bar.png)
103+
104+
In iTerm2, navigate to `Preferences` > `Keys` and `Add Touch Bar Item` for each of the following items. After, navigate to the `View` dropdown > `Customize Touch Bar...` and drag them where you'd like ๐Ÿ˜Ž.
105+
106+
### Npm
107+
108+
Label: `๐Ÿ“ฆ`
109+
110+
Action: `Send Text with "vim" Special Chars`
111+
112+
`npmjs\r`
113+
114+
### Clone
115+
116+
Label: `๐Ÿค–`
117+
118+
Action: `Send Text`
119+
120+
`clone YourOrg `
121+
122+
### VS Code / Atom / Sublime
123+
124+
Label: `๐Ÿšง` or `๐Ÿ”ญ` or `๐Ÿ‘ฉโ€๐Ÿ’ป` or `๐Ÿ‘จโ€๐Ÿ’ป` or `โš›๏ธ`
125+
126+
Action: `Send Text with "vim" Special Chars`
127+
128+
`code .\r` or `atom .\r` or `subl .\r`
129+
130+
### Git Pull
131+
132+
Label: `๐Ÿ‘‡`
133+
134+
Action: `Send Text with "vim" Special Chars`
135+
136+
`git pull\r`
137+
138+
### Git Push
139+
140+
Label: `๐Ÿ‘†`
141+
142+
Action: `Send Text`
143+
144+
`git push `
145+
146+
### GitHub
147+
148+
Label: `๐Ÿ™`
149+
150+
Action: `Send Text with "vim" Special Chars`
151+
152+
`github\r`
153+
154+
### Git Status
155+
156+
Label: `๐Ÿ“Ÿ`
157+
158+
Action: `Send Text with "vim" Special Chars`
159+
160+
`gst\r`
161+
162+
### Npm Install
163+
164+
Label: `๐Ÿ“€`
165+
166+
Action: `Send Text with "vim" Special Chars`
167+
168+
`npm i\r`
169+
170+
### Npm Start
171+
172+
Label: `โšก๏ธ` or `๐Ÿ`
173+
174+
Action: `Send Text with "vim" Special Chars`
175+
176+
`npm start\r`
177+
178+
### Npm Test
179+
180+
Label: `๐Ÿ”ฌ`
181+
182+
Action: `Send Text with "vim" Special Chars`
183+
184+
`npm t\r`
185+
186+
### Draft PR
187+
188+
Label: `โœ๏ธ`
189+
190+
Action: `Send Text with "vim" Special Chars`
191+
192+
`pr\r`
193+
194+
### View PRs
195+
196+
Label: `๐Ÿ”ƒ`
197+
198+
Action: `Send Text with "vim" Special Chars`
199+
200+
`pulls\r`
201+
202+
### Spotify Play/Pause
203+
204+
NOTE: Requires [shpotify](https://github.com/hnarayanan/shpotify).
205+
206+
Label: `๐ŸŽธ` or `๐ŸŽง` or `๐ŸŽต` or `๐ŸŽท`
207+
208+
Action: `Send Text with "vim" Special Chars`
209+
210+
`spotify pause\r`
34.2 KB
Loading

0 commit comments

Comments
 (0)