Get git branch name on terminal (Used with 'PS1=' to attach to the folder/session name)
# If something weird is happening check:
# <https://unix.stackexchange.com/questions/28827/why-is-my-bash-prompt-getting-bugged-when-i-browse-the-history>
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \\(.*\\)/(\\1) /'
}
is_git_repo() {
git rev-parse --is-inside-work-tree > /dev/null 2>&1
}
get_project_name() {
is_git_repo && basename $(git rev-parse --show-toplevel) || echo ""
}
# WHEN BASH.. USE PS1 OR PROMPT_COMMAND
if [ -n "$BASH_VERSION" ]; then
PROMPT_COMMAND='PS1=$(if [ "$(get_project_name)" != "" ]; then echo -e "\\[\\e[32m\\][$(get_project_name) :: \\W] \\[\\e[34m\\]$(parse_git_branch)\\[\\e[0m\\]>_ "; else echo -e "\\[\\e[36m\\]\\W \\[\\e[0m\\]>_ "; fi)'
fi
# WHEN ZSH.. USE PROMPT
if [ -n "$ZSH_VERSION" ]; then
setopt PROMPT_SUBST
precmd() {
if [ "$(get_project_name)" != "" ]; then
PROMPT='%F{green}[$(get_project_name) :: %1d] %F{blue}$(parse_git_branch)%f>_ '
else
PROMPT='%F{cyan}%1d %f>_ '
fi
}
setopt no_share_history
unsetopt share_history
fi
Force the Git Sync to cleanup your local branch and fix any issues
(Use at your own risk at Friday 5:50PM) ¯\\*(ツ)*/¯
forcesync() {
ACTUAL_BRANCH=$(git branch | grep \\* | cut -d ' ' -f2)
if [ -z "$1" ]
then
#Aux to just show me the command
echo ''
echo 'Actual Branch: $(ACTUAL_BRANCH)'
echo 'git fetch origin && git reset --hard origin/{BRANCH} && git clean -fd'
echo ''
else
echo ''
echo 'Forcing the sync...'
git fetch origin && git reset --hard origin/$1 && git clean -fd
echo 'Your sync was completed!'
echo ''
fi
}
A bit more to help with git
##
# ALIAS | GIT
##
alias amend='git commit --amend --no-edit'
alias this="git branch | grep \\* | cut -d ' ' -f2"
# Get the token from your ~/.npmrc file
get_github_token() {
grep -o 'ghp_[^"]*' ~/.npmrc
}
# Auto git pull for who always forget to stash
pull() {
echo 'Stashing...'
git stash
echo 'Pulling...'
git pull --rebase
echo 'Stash pop...'
git stash pop
echo 'Done~'
}
# YOLO 😈
yolo() {
echo "⚠️ WARNING: This will force push your changes! ⚠️"
echo "
With great power comes great responsibility.
This will overwrite the remote history with your local changes.
"
echo "YOLO? (y/n, default: n):"
read answer
answer=${answer:-n}
if [ "$answer" != "${answer#[Yy]}" ] ;then
if ! amend; then
echo "Error: Failed to amend commit"
return 1
fi
if ! git push --force-with-lease origin HEAD; then
echo "Error: Failed to force push"
return 1
fi
echo "Successfully force pushed changes"
else
echo "Yolo aborted."
fi
}
# You know when you move a file and the git says you've deleted one and created the other..
# it solves it easly
gmv() {
if [ -z "$1" ] || [ -z "$2" ]; then
echo "usage: gmv <new> <old>"
else
mv $1 $2
git mv $2 $1
fi
}
# Add the file to local git ignore, the one you won't commit =P
localIgnore() {
echo "$1" >> .git/info/exclude
echo "File $1 locally ignored"
echo ""
echo "---- .git/info/exclude ----"
cat .git/info/exclude
echo "---- ----------------- ----"
echo ""
}
Some of my aliases
# findh == Find Here
alias findh='find ./ -name'
alias checkdistro='cat /etc/*-release'
# Shortcut to recursive grep without git and svn folders
alias grep='grep --exclude-dir={".svn",".git","node_modules"}'
alias ngrep='grep -rn ./ -e'
# Clean PHP Smarty Cache
alias clean_cache='find . -name *.html.php | xargs rm -rf'
# Show the command to replace a content on multiple files
alias replacer='echo "find <mydir> -type f -exec sed -i '"'"'s'"/"'<String1>'"/"'<String2>'"/"'g'"'"' {} +"'
## Colorize the ls output ##
alias ls='ls --color=auto'
## Use a long listing format ##
alias ll='ls -la'
## Show hidden files ##
alias l.='ls -d .* --color=auto'
Docker related
alias registryGarbageCollector="yes | doctl registry garbage-collection start --include-untagged-manifests"
My SSH selector (since I always forget the VMs/Machine IPs)
# This one.. you can just add. Don't need to modify
select_one() (
# select a line from $1 with optional prompt $2
# (or pass 0 args to enter a key capture test loop for debugging)
# exit early if stdin is not a tty
if ! [ -t 0 ]; then
echo "0"; return 1
fi
# save tty settings
tty_settings=$(stty -g)
stty -icanon min 1 time 0 -echo cbreak
# constants
_TAB="09;"
_ENTER="0a;"
_RIGHT="1b;5b;43;"
_DOWN="1b;5b;42;"
_LEFT="1b;5b;44;"
_UP="1b;5b;41;"
# enter key capture test loop if argc is 0
if [ "$#" -eq 0 ]; then
echo "Collect keystrokes (Press any key; Enter to reset buffer; ^C to exit)" > /dev/tty
bytes=
while true; do
# read one byte of input
byte=$(dd if=/dev/tty bs=1 count=1 2>/dev/null | od -An -vtx1 | tr -d ' \\n')
bytes="$bytes$byte;"
echo "$bytes" > /dev/tty
if [ -z "$byte" ]; then
# ^C arrives here
stty "$tty_settings"; exit
elif [ "$byte;" = "$_ENTER" ]; then
echo "Reset buffer..." > /dev/tty
bytes=
fi
done
fi
# count lines to display in $1 and exit early
lines=$(echo "$1" | awk '{gsub(/\\\\n/,"\\n"); print}')
linect=$(($(echo "$lines" | wc -l)))
if [ "$linect" -eq 0 ]; then
echo "0"; stty "$tty_settings"; return 1
elif [ "$linect" -eq 1 ]; then
echo "0"; stty "$tty_settings"; return 0
fi
# clear the line by printing spaces
cols=$(($(tput cols) - 1))
el=$(printf "%*s" "$cols" "")
# track selection
selected=0
previous=
while true; do
# redraw when selection changes
if [ "$selected" != "$previous" ]; then
# wrap selected index into range
selected=$(((selected + linect) % linect))
# clear the line and display selection
idx=$((selected + 1))
line=$(echo "$lines" | sed -n "${idx}p")
printf "%s%s%s%s(%s/%s): %s" "$(tput cr)" "$el" "$(tput cr)" "$2" "$idx" "$linect" "$line" > /dev/tty
# save previous selection
previous="$selected"
fi
finished=
right="$_RIGHT"
down="$_DOWN"
left="$_LEFT"
up="$_UP"
while true; do
# read one byte of input
byte=$(dd if=/dev/tty bs=1 count=1 2>/dev/null | od -An -vtx1 | tr -d ' \\n')
if [ -z "$byte" ]; then
# ^C arrives here
stty "$tty_settings"; exit
elif [ "$byte;" = "$_TAB" ]; then
selected=$((selected + 1)); break
elif [ "$byte;" = "$_ENTER" ]; then
finished=1; break
else
right=$(echo "$right" | sed "s/^$byte;//")
down=$(echo "$down" | sed "s/^$byte;//")
if [ -z "$down" ] || [ -z "$right" ]; then
selected=$((selected + 1)); break
fi
left=$(echo "$left" | sed "s/^$byte;//")
up=$(echo "$up" | sed "s/^$byte;//")
if [ -z "$up" ] || [ -z "$left" ]; then
selected=$((selected - 1)); break
fi
fi
done
if [ -n "$finished" ]; then
break
fi
done
echo "$selected"; stty "$tty_settings"; return 0
)
# Here, you'll add the options as needed
ssh() {
if [ $# -eq 0 ]; then
local OPTION_A="xxxx-Droplet"
local OPTION_B="yyyy-Droplet"
local OPTION_A_IP="254.254.254.254"
local OPTION_B_IP="1.1.1.1"
local selections="$OPTION_A\\n$OPTION_B\\nQuit"
idx=$(select_one "$selections" "Using the arrows or tab, select a server to SSH into: ")
selected_choice=$(echo "$selections" | awk '{gsub(/\\\\n/,"\\n"); print}' | sed -n "$((idx + 1))p")
echo "\\nSelected choice: $selected_choice"
case $selected_choice in
"$OPTION_A")
/usr/bin/ssh root@$OPTION_A_IP
;;
"$OPTION_B")
/usr/bin/ssh root@$OPTION_B_IP
;;
"Quit")
;;
*)
echo "Invalid choice."
;;
esac
else
/usr/bin/ssh "$@"
fi
}