|
| 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 | +} |
0 commit comments