Skip to content

Instantly share code, notes, and snippets.

@devtin
Last active May 20, 2022 15:10
Show Gist options
  • Save devtin/c464b93909d8891572151a5f7a210743 to your computer and use it in GitHub Desktop.
Save devtin/c464b93909d8891572151a5f7a210743 to your computer and use it in GitHub Desktop.
github commands
#!/bin/bash
# prints remote github-url
function ghr () {
GITHUB_REMOTE=$(git config --get remote.origin.url)
if [ $? -eq 1 ]; then
echo "either not a github repo or remote not found 😙"
return 1
fi
GITHUB_REMOTE_PATH=$(echo $GITHUB_REMOTE | perl -n -e'/git\@github\.com:(.*)\.git/ && print $1')
echo "https://github.com/$GITHUB_REMOTE_PATH"
}
# prints remote github-url/brach
function ghb () {
GITHUB_REMOTE_BRANCH_URL="$(ghr)/tree/$(git branch --show-current)"
if [ $? -ne 0 ]; then
echo $GITHUB_REMOTE_BRANCH_URL
return $?
fi
echo $GITHUB_REMOTE_BRANCH_URL
}
# prints remote url for current repo / branch / working directory and given path (if any)
function gh () {
printf $(ghb)
GIT_ROOT=$(git rev-parse --show-toplevel)
CURRENT_PATH=$(python -c "import os.path, sys; print(os.path.relpath(sys.argv[1],sys.argv[2]))" "$PWD" "$GIT_ROOT")
if [ "$CURRENT_PATH" != "." ]; then
printf "/$CURRENT_PATH"
fi
if [ "$1" != "" ]; then
printf "/$1"
fi
echo ""
}
# opens working directory and/or given file in a browser
# ex: gho app/main.py
function gho () {
open -n -a "Google Chrome" --args '--new-window' $(gh $1)
}
# copies url in the clipboard
function ghc () {
gh $1 | pbcopy
}

Manifesto

Ever found yourself working on a local github repository and having the need of opening or referencing files on github web? go coconut no more:

PS: mind this was designed for MacOsX

Install

Oneliner:

/bin/bash -c "$(curl -fsSL https://gist.githubusercontent.com/devtin/c464b93909d8891572151a5f7a210743/raw/install.sh)"
  • Download .github file.
  • Place it somewhere in your user path ~/
  • Include a line to load it in your .bashrc or .zshrc file like: source ~/.github

Enjoy using:

  • gh <optional-path> to print remote github url w/ branch of current repository
  • ghr prints github url of the repo (no branch or path)
  • ghb prints github url of the repo including branch
  • ghc copies full github url w/ branch, working dir and optional given path
  • gho opens browser of github url w/ branch, working dir and optional given path
#!/bin/bash
$(curl -sO --output-dir ~/ https://gist.githubusercontent.com/devtin/c464b93909d8891572151a5f7a210743/raw/.github)
INSTALLED=0
LINE_LOADER="source ~/.github"
for profile in ~/.zshrc ~/.bashrc
do
if [ -f $profile ]; then
INSTALLED=1
LINE_EXISTS=$(cat $profile | grep "$LINE_LOADER")
if [ "$LINE_EXISTS" == "" ]; then
echo $LINE_LOADER >> $profile
fi
fi
done
if [ $INSTALLED -eq 1 ]; then
source ~/.github
echo "Installed!"
else
echo "Maybe unable to find ~/.zshrc or ~/.bashrc"
return 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment