Skip to content

Instantly share code, notes, and snippets.

@gitaarik
Last active May 17, 2019 23:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gitaarik/6103330 to your computer and use it in GitHub Desktop.
Save gitaarik/6103330 to your computer and use it in GitHub Desktop.
Alters bash to make working with Git in the shell easier. Will set the current repository and branch name in the prompt and makes `g` an alias to `git`.
#!/bin/bash
_git_has_commits() {
if git rev-parse --verify HEAD > /dev/null 2>&1; then
return 0
else
return 1
fi
}
_git_current_branch_name() {
if _git_has_commits ; then
git rev-parse --short --abbrev-ref HEAD
else
echo "master"
fi
}
_git_current_remote() {
remote=$(git config --get branch.$(_git_current_branch_name).remote)
if [[ -z "$remote" ]]; then
remote=origin
fi
echo $remote
}
_git_current_remote_branch_name() {
git config --get branch.$(_git_current_branch_name).merge | sed 's/[a-z]\+\/[a-z]\+\///g'
}
_git_current_remote_url() {
git config --get remote.$(_git_current_remote).url
}
_git_current_repos_name() {
if _git_has_commits ; then
_git_current_remote_url | sed 's/^.*\/\(..*\)\.git/\1/'
else
echo "unknown"
fi
}
_git_state() {
if _git_has_commits ; then
if ! git diff --quiet 2> /dev/null ; then
echo "not-staged"
elif ! git diff --staged --quiet 2> /dev/null ; then
echo "not-committed"
elif ! git diff $(_git_current_remote)/$(_git_current_remote_branch_name) --quiet 2> /dev/null ; then
echo "not-pushed"
elif [[ -n "$(git log $(_git_current_remote)/$(_git_current_remote_branch_name)..$(_git_current_branch_name))" ]] ; then
echo "diverged"
else
echo "clean"
fi
else
echo "not-initialized"
fi
}
_git_activate_branch_color() {
state=$(_git_state)
if [[ $state == "not-initialized" ]] ; then
tput setaf 7 # grey
elif [[ $state == "not-staged" ]] ; then
tput setaf 1 # red
elif [[ $state == "not-committed" ]] ; then
tput setaf 3 # yellow
elif [[ $state == "not-pushed" ]] ; then
tput setaf 6 # blue
elif [[ $state == "diverged" ]] ; then
tput setaf 5 # purple
else
tput setaf 2 # green
fi
}
_git_stash_indicator() {
stash_count=$(git stash list | wc -l | sed 's/ //g')
if [[ $stash_count -gt 0 ]] ; then
echo " - $(tput setaf 1)$stash_count$(tput setaf 4)"
fi
}
_git_weblink() {
link_start=https://$(git remote get-url origin | cut -d '@' -f 2 | sed 's/:/\//g' | cut -d '.' -f 1,2)
commit_hash=$(git log -1 --format='%H')
if [[ -n "$1" ]]; then
echo "$link_start"/tree/"$commit_hash"/$(git ls-tree --full-name --name-only $commit_hash $(echo "$1" | sed 's/\/$//'))
else
echo "$link_start"/commit/"$commit_hash"/
fi
}
# The `\` before the `$` in `\$(func_name)` will make the `func_name`
# function execute every time the prompt is printed.
export PS1="\
\[$(tput bold)\]\
\[$(tput setaf 4)\]\
git [\
\[$(tput setaf 2)\]\$(_git_current_repos_name)\[$(tput setaf 4)\] - \
\[\$(_git_activate_branch_color)\]\$(_git_current_branch_name)\[$(tput setaf 4)\]\
\$(_git_stash_indicator)\
] \$ \[$(tput sgr0)\]\
"
# Short alias for git stuff
alias g=git
# Make autocomplete also work fo the `g` alias
eval $(complete -p git | sed 's/git$/g/g')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment