🐙 Adds Git methods for opening GitHub

This commit is contained in:
Alicia Sykes 2022-03-22 16:29:52 +00:00
parent b1b9967268
commit 64e127854d

View File

@ -126,14 +126,30 @@ function gfetchrebase {
alias gfrb="gfetchrebase"
# Opens the current repo + branch in GitHub
open-github() {
git_base_url='https://github.com' # Modify this if using GH enterprise
if [[ ! -z $1 && ! -z $2 ]]; then
# User specified a repo
git_url=$git_base_url/$1/$2
elif git rev-parse --git-dir > /dev/null 2>&1; then
# Use current repo
# 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"
}
# Helper function to return URL of current repo (based on origin)
get-repo-url() {
git_base_url='https://github.com'
# Get origin from git repo + remove .git
git_url=${$(git config --get remote.origin.url)%.git}
# Process URL, and append branch / working origin
if [[ $git_url =~ ^git@ ]]; then
@ -155,31 +171,63 @@ open-github() {
git_url="$git_url"
fi
fi
else
# Not in repo, and nothing specified, open homepage
git_url=$git_base_url
fi
# Return URL
echo $git_url
}
# Determine which open commands supported
# Helper function that gets supported open method for system
launch-url() {
if hash open 2> /dev/null; then
open_command=open
elif hash xdg-open 2> /dev/null; then
open_command=xdg-open
elif hash lynx 2> /dev/null; then
open_command=lynx
elif hash w3m 2> /dev/null; then
open_command=w3m
else
echo -e "\033[0;33mUnable to launch browser, open manually instead"
echo -e "\033[1;96m🐙 GitHub URL: \033[0;96m\e[4m$git_url\e[0m"
echo -e "\033[1;96m🌐 URL: \033[0;96m\e[4m$1\e[0m"
return;
fi
echo $open_command
}
# Opens the current repo + branch in GitHub
open-github() {
git_base_url='https://github.com' # Modify this if using GH enterprise
if [[ ! -z $1 && ! -z $2 ]]; then
# User specified a repo
git_url=$git_base_url/$1/$2
elif git rev-parse --git-dir > /dev/null 2>&1; then
# Get URL from current repo's origin
git_url=$(get-repo-url)
else
# Not in repo, and nothing specified, open homepage
git_url=$git_base_url
fi
# Determine which open commands supported
open_command=$(launch-url $git_url)
# Print messages
echo -e "\033[1;96m🐙 Opening in browser: \033[0;96m\e[4m$git_url\e[0m"
# And launch!
$open_command $git_url
}
alias gho='open-github'
# Opens pull request tab for the current GH repo
open-github-pulls() {
# Get Repo URL
if git rev-parse --git-dir > /dev/null 2>&1; then
git_url=$(get-repo-url)
else
git_url='https://github.com'
fi
git_url="$git_url/pulls"
# Get open command
open_command=$(launch-url $git_url)
# Print message, and launch!
echo -e "\033[1;96m🐙 Opening in browser: \033[0;96m\e[4m$git_url\e[0m"
$open_command $git_url
}
alias ghp='open-github-pulls'