🐙 Adds function to open current git repo

This commit is contained in:
Alicia Sykes 2022-03-21 21:21:54 +00:00
parent e2fc1690c8
commit b1b9967268

View File

@ -48,3 +48,138 @@ alias gu="git reset" # Reset HEAD pointer to a <commit>, perserves changes
alias gua="git reset --hard HEAD" # Resets all uncommited changes alias gua="git reset --hard HEAD" # Resets all uncommited changes
alias gnewmsg="git commit --amend -m" # Update <message> of previous commit alias gnewmsg="git commit --amend -m" # Update <message> of previous commit
alias gclean="git clean -df" # Remove all untracked files alias gclean="git clean -df" # Remove all untracked files
# Shorthand clone (e.g. $ clone lissy93/dotfiles)
function clone {
default_service='github.com' # Used if full URL isn't specified
default_username='lissy93' # Used if repo org / username isn't specified
user_input=$1
target=${2:-''}
# Help flag passed, show manual and exit
if [[ $user_input == --help ]] || [[ $user_input == -h ]]; then
echo -e 'This will clone a git repo';
echo -e 'Either specify repo name, user/repo, or a full URL'
echo -e 'E.g. `$ clone lissy93/dotfiles`'
return;
# No input specified, prompt user
elif [ $# -eq 0 ]; then
echo 'Enter a user/repo or full URL: ';
read user_input;
fi
echo "$target"
# Determine input type, and make clone url
if [[ $user_input == git@* || $user_input == *://* ]]
then
# Full URL was provided
REPO_URL=$user_input;
elif [[ $user_input == */* ]]; then
# Username/repo was provided
REPO_URL="https://$default_service/$user_input.git";
else
# Just repo name was provided
REPO_URL="https://$default_service/$default_username/$user_input.git";
fi
# Clone repo
git clone $REPO_URL $target;
}
# Sync fork against upstream repo
function gsync {
# If no upstream origin provided, prompt user for it
if ! git remote -v | grep -q 'upstream'; then
echo 'Enter the upstream git url: ';
read url;
git remote add upstream "$url"
fi
git remote -v
git fetch upstream
git pull upstream master
git checkout master
git rebase upstream/master
}
# Make git commit with -m
function gcommit {
commit_msg=$@
if [ $# -eq 0 ]; then
echo 'Enter a commit message';
read commit_msg;
fi
git commit -m "$commit_msg"
}
alias gcm="gcommit"
# Fetch, rebase and push updates to current branch
# Optionally specify target, defaults to 'master'
function gfetchrebase {
if ! [ -z "$1" ]; then
branch=$1
else
branch='master'
fi
git fetch upstream
git rebase upstream/$branch
git push
}
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
git_url=${$(git config --get remote.origin.url)%.git}
# Process URL, and append branch / working origin
if [[ $git_url =~ ^git@ ]]; then
branch=${1:-"$(git symbolic-ref --short HEAD)"}
branchExists="$(git ls-remote --heads $git_url $branch | wc -l)"
github="$(echo $git_url | sed 's/git@//')" # Remove git@ from the start
github="$(echo $github | sed 's/\:/\//')" # Replace : with /
if [[ $branchExists == " 1" ]]; then
git_url="http://$github/tree/$branch"
else
git_url="http://$github"
fi
elif [[ $git_url =~ ^https?:// ]]; then
branch=${1:-"$(git symbolic-ref --short HEAD)"}
branchExists="$(git ls-remote --heads $git_url $branch | wc -l)"
if [[ $branchExists == " 1" ]]; then
git_url="$git_url/tree/$branch"
else
git_url="$git_url"
fi
fi
else
# Not in repo, and nothing specified, open homepage
git_url=$git_base_url
fi
# Determine which open commands supported
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"
return;
fi
# 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'