Skip to content

Instantly share code, notes, and snippets.

@jkingsman
Last active June 2, 2022 19:47
Show Gist options
  • Save jkingsman/d0c095fc0ade883a51f5a1aa69d76b26 to your computer and use it in GitHub Desktop.
Save jkingsman/d0c095fc0ade883a51f5a1aa69d76b26 to your computer and use it in GitHub Desktop.
Handy Functions & Git Prompts
# checks for apps I know I want wherever I go
checkApps(){
echo "====== CHECKING INSTALLED APPS ======"
declare -a expected=("apm" "atom" "atom" "bower" "brew" "eslint" "ffmpeg" "gem" "jshint" "jslint" "mocha" "nmap" "node" "npm" "pip" "pip3" "python" "python2" "python3" "ruby" "vim")
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
for i in "${expected[@]}"
do
location=`which ${i}`
if [ $? -eq 0 ]
then
echo -e "$i -- ${GREEN}${location}${NC}"
else
echo -e "$i -- ${RED}MISSING${NC}"
fi
done
}
checkApps
# Make branch creation from reference branches and rolling around branches super easy with auto-stash of changes
# You'll need my unfuck alias in your ~/.gitconfig:
# [alias]
# unfuck = !git reset --hard @{u} && git clean -f
gcoc(){
if test "$#" -eq 0; then
echo "Usage: gcoc <story # without main> [base branch]"
echo "gcoc 8493 # makes a new branch MAIN-8493 off master"
echo "gcoc 8493-patch release-1440 # makes a new branch MAIN-8493-patch based off release-1440"
return
fi
git fetch
git stash
if test "$#" -eq 1; then
echo "Basing branch off master..."
git checkout master
fi
if test "$#" -eq 2; then
echo "Basing branch off $2..."
git checkout $2
fi
git pull
if git ls-remote jwkingsman@bitbucket.org:chartio/chartio-castle.git | grep -sw MAIN-$1 2>&1>/dev/null;
then
echo "Checking out..."
git checkout MAIN-$1
git unfuck
git push --set-upstream origin MAIN-$1
git pull
else
echo "Creating branch..."
git checkout -b MAIN-$1
git push --set-upstream origin MAIN-$1
fi
}
# git commit and push; supports barewords commit
gcp(){
git commit -am "$@"
git push
}
#!/usr/bin/env bash
# Shell prompt based on the Solarized Dark theme.
# Screenshot: http://i.imgur.com/EkEtphC.png
# Heavily inspired by @necolas’s prompt: https://github.com/necolas/dotfiles
# iTerm → Profiles → Text → use 13pt Monaco with 1.1 vertical spacing.
if [[ $COLORTERM = gnome-* && $TERM = xterm ]] && infocmp gnome-256color >/dev/null 2>&1; then
export TERM='gnome-256color';
elif infocmp xterm-256color >/dev/null 2>&1; then
export TERM='xterm-256color';
fi;
prompt_git() {
local s='';
local branchName='';
# Check if the current directory is in a Git repository.
if [ $(git rev-parse --is-inside-work-tree &>/dev/null; echo "${?}") == '0' ]; then
# check if the current directory is in .git before running git checks
if [ "$(git rev-parse --is-inside-git-dir 2> /dev/null)" == 'false' ]; then
# Ensure the index is up to date.
git update-index --really-refresh -q &>/dev/null;
# Check for uncommitted changes in the index.
if ! $(git diff --quiet --ignore-submodules --cached); then
s+='+';
fi;
# Check for unstaged changes.
if ! $(git diff-files --quiet --ignore-submodules --); then
s+='!';
fi;
# Check for untracked files.
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
s+='?';
fi;
# Check for stashed files.
if $(git rev-parse --verify refs/stash &>/dev/null); then
s+='$+';
stashCount=$(git stash list | wc -l | awk '{print $1}');
s+="${stashCount}"
fi;
fi;
# Get the short symbolic ref.
# If HEAD isn’t a symbolic ref, get the short SHA for the latest commit
# Otherwise, just give up.
branchName="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \
git rev-parse --short HEAD 2> /dev/null || \
echo '(unknown)')";
[ -n "${s}" ] && s=" [${s}]";
echo -e "${1}${branchName}${2}${s}";
else
return;
fi;
}
if tput setaf 1 &> /dev/null; then
tput sgr0; # reset colors
bold=$(tput bold);
reset=$(tput sgr0);
# Solarized colors, taken from http://git.io/solarized-colors.
black=$(tput setaf 0);
blue=$(tput setaf 33);
cyan=$(tput setaf 37);
green=$(tput setaf 64);
orange=$(tput setaf 166);
purple=$(tput setaf 125);
red=$(tput setaf 124);
violet=$(tput setaf 61);
white=$(tput setaf 15);
yellow=$(tput setaf 136);
else
bold='';
reset="\e[0m";
black="\e[1;30m";
blue="\e[1;34m";
cyan="\e[1;36m";
green="\e[1;32m";
orange="\e[1;33m";
purple="\e[1;35m";
red="\e[1;31m";
violet="\e[1;35m";
white="\e[1;37m";
yellow="\e[1;33m";
fi;
# Highlight the user name when logged in as root.
if [[ "${USER}" == "root" ]]; then
userStyle="${red}";
else
userStyle="${orange}";
fi;
# Highlight the hostname when connected via SSH.
if [[ "${SSH_TTY}" ]]; then
hostStyle="${bold}${red}";
else
hostStyle="${yellow}";
fi;
# Set the terminal title and prompt.
PS1="\[\033]0;\W\007\]"; # working directory base name
PS1+="\[${bold}\]\n"; # newline
PS1+="\[${userStyle}\]\u"; # username
PS1+="\[${white}\] at ";
PS1+="\[${hostStyle}\]\h"; # host
PS1+="\[${white}\] in ";
PS1+="\[${green}\]\w"; # working directory full path
PS1+="\$(prompt_git \"\[${white}\] on \[${violet}\]\" \"\[${blue}\]\") ${reset}\n"; # Git repository details
# PS1+="\[${white}\] Δ \[${reset}\]"; # `$` (and reset color)
export PS1;
PS2="\[${yellow}\]→ \[${reset}\]";
export PS2;
# Turbopowered multi-term history grep with pretty colors
hgrep(){
[ -z "$1" ] && echo "Syntax: ${FUNCNAME[0]} <PATTERN> [<PATTERN>...]" 1>&2 && return 1;
local colorCount=31
local grepper="history | GREP_COLOR='1;$colorCount' grep --color=always '$1'";
while shift; do
colorCount=$((colorCount+1))
[ -z "$1" ] && continue;
grepper="$grepper | GREP_COLOR='1;$colorCount' grep --color=always '$1'";
done;
eval "$grepper"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment