# ============================================= #
# ------------------ Helpers ------------------ #
# ============================================= #
_git_get_default_branch_name() {
git symbolic-ref refs/remotes/origin/HEAD | sed s@^refs/remotes/origin/@@
}
_git_get_current_branch_name() {
git branch 2>/dev/null | sed -n "/^\*/s/^\* //p"
}
_git_get_revision_count() {
git rev-list --count HEAD ^origin/HEAD 2>/dev/null
}
_git_get_touched_files() {
git status --porcelain | sed s/^...//
}
_git_get_modified_files() {
echo -e "$(_git_get_touched_files)\n$(git diff --name-only HEAD ^origin/HEAD)" | sort | uniq
}
_git_does_branch_exist() {
git show-ref --quiet refs/heads/$1
}
# ============================================= #
# ------------------ Aliases ------------------ #
# ============================================= #
# Important
alias s="subl"
alias x="xargs"
alias xs="x subl"
alias v='ls -abGHhlOT'
# General
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias f="find . | g"
alias g="grep"
alias mkdir="mkdir -p"
# Git
alias gb="git branch"
alias gd="git diff --binary --color"
alias gf="git fetch"
alias gh="git show --binary --color --format=medium"
alias gp="git pull --rebase"
alias gr="git rebase"
alias gs="git status"
alias gv="git ls-files"
# ============================================= #
# ----------------- Functions ----------------- #
# ============================================= #
# Permissions
p644() {
if [ $1 ]; then
find $1 -type f -exec chmod 644 {} +
else
find . -type f -exec chmod 644 {} +
fi
}
p755() {
if [ $1 ]; then
find $1 -type d -exec chmod 755 {} +
else
find . -type d -exec chmod 755 {} +
fi
}
# Management
make() {
/usr/bin/make $@
osascript <<END
tell application "System Events"
set activeApp to name of first application process whose frontmost is true
if "Terminal" is not in activeApp then
display notification "$@" with title "make" sound name "Morse"
end if
end tell
END
}
xcodebuild() {
/usr/bin/xcodebuild $@
osascript <<END
tell application "System Events"
set activeApp to name of first application process whose frontmost is true
if "Terminal" is not in activeApp then
display notification "$@" with title "xcodebuild" sound name "Morse"
end if
end tell
END
}
rd() {
if [ $1 ]; then
find . -name $1 -type f -delete
else
echo "Usage: rd filename.xyz"
fi
}
o() {
if [ $1 ]; then
open $1
else
open .
fi
}
path() {
if [ $1 ]; then
echo $PWD/$1
else
pwd
fi
}
extract() {
if [ -f $1 ]; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.jar) jar xf $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*) echo "\"$1\" cannot be extracted via >extract<" ;;
esac
else
echo "\"$1\" is not a valid file"
fi
}
# Git
gl() {
local count=$(_git_get_revision_count)
if [[ $count != "0" ]] && [[ $count != "" ]]; then
git log -$((count + 5)) ${@:1}
else
git log -10 ${@:1}
fi
}
ga() {
if [ $1 ]; then
if [ -f $1 ]; then
git apply -v $1 ${@:2}
else
curl -L $1 | git apply -v ${@:2}
fi
else
echo "Usage: ga path [...flags]"
fi
}
gu() {
local branch=$(_git_get_current_branch_name)
git push origin $branch ${@:1}
}
gc() {
local default_branch_name=$(_git_get_default_branch_name)
if [ $# == 0 ]; then
git checkout $default_branch_name
elif [ $1 ] && [ $1 != $default_branch_name ] && _git_does_branch_exist $1; then
git rebase $default_branch_name $1
else
git checkout $@
fi
}
gri() {
local count=$(_git_get_revision_count)
if [[ $count != "0" ]] && [[ $count != "" ]]; then
git rebase -i HEAD~$((count)) ${@:1}
else
git rebase -i HEAD~10 ${@:1}
fi
}
gdf() {
local branch=$(_git_get_current_branch_name)
if [ $1 ]; then
if [ $2 ]; then
gd --no-color ${@:1:$#-1} > "$DATA/${!#}.diff"
else
if git cat-file -e $1 2>/dev/null >&2; then
gd --no-color $1 > "$DATA/$branch.diff"
else
gd --no-color > "$DATA/$1.diff"
fi
fi
else
gd --no-color > "$DATA/$branch.diff"
fi
}
ghf() {
local branch=$(_git_get_current_branch_name)
if [ $1 ]; then
if [ $2 ]; then
gh --no-color --format="" ${@:1:$#-1} > "$DATA/${!#}.diff"
else
if git cat-file -e $1 2>/dev/null >&2; then
gh --no-color --format="" $1 > "$DATA/$branch.diff"
else
gh --no-color --format="" > "$DATA/$1.diff"
fi
fi
else
gh --no-color --format="" > "$DATA/$branch.diff"
fi
}
# Bazel
bb() {
local args=""
local flags=""
for arg in "$@"; do
if [[ $arg == -* ]]; then
flags="$flags $arg"
else
args="$args $arg"
fi
done
if [[ $args == "" ]]; then
args="..."
fi
bazel build $args $flags
}
bt() {
local args=""
local flags=""
for arg in "$@"; do
if [[ $arg == -* ]]; then
flags="$flags $arg"
else
args="$args $arg"
fi
done
if [[ $args == "" ]]; then
args="..."
fi
bazel test $args $flags
}
# Web Development
u() {
local server=$(dirname $PWD | xargs basename)
local folder=$(basename $PWD)
for path in "${@}"; do
if [[ $folder == "site" ]]; then
scp -r $path $server:/var/www/html/$path
else
scp -r $path $server:$path
fi
done
}
# ============================================= #
# ------------------ WebKit+ ------------------ #
# ============================================= #
alias mc="make-filtered clean"
alias mr="make-filtered release"
alias md="make-filtered debug"
alias ml="$WebKit/Tools/Scripts/extract-localizable-js-strings --utf8 $WebKit/Source/WebInspectorUI/Localizations/en.lproj/localizedStrings.js $WebKit/Source/WebInspectorUI/UserInterface"
alias sr="$WebKit/Tools/Scripts/run-safari --release"
alias sd="$WebKit/Tools/Scripts/run-safari --debug"
alias gw="git webkit"
make-filtered() {
make $@ | $WebKit/Tools/Scripts/filter-build-webkit
}
xcodebuild-filtered() {
xcodebuild $@ | $WebKit/Tools/Scripts/filter-build-webkit
}
wtl() {
local flags=""
local tests=""
for arg in "$@"; do
if [[ $arg == -* ]]; then
flags="$flags $arg"
else
tests="$tests $arg"
fi
done
if [[ $tests == "" ]]; then
tests=$(_git_get_modified_files | g LayoutTests | g ".html" | x)
fi
$WebKit/Tools/Scripts/run-webkit-tests --no-build --no-sample --no-retry-failures --time-out-ms=5000 --verbose $flags $tests
}
alias wta="$WebKit/Tools/Scripts/run-api-tests --no-build --timestamps --verbose $flags"
alias wtjs="$WebKit/Tools/Scripts/run-jsc-stress-tests --verbose $WebKit/JSTests/stress --filter"
# ============================================= #
# ---------------- Completions ---------------- #
# ============================================= #
completion_aliases=()
_color_red="\001\033[31m\002"
_color_green="\001\033[32m\002"
_color_blue="\001\033[34m\002"
_color_cyan="\001\033[36m\002"
_color_sgr0="\001\033[0m\002"
# Prompt
_ps1_color_git() {
local branch=$(_git_get_current_branch_name)
if [[ $branch ]]; then
local unstaged=""
# if git diff --quiet 2>/dev/null >&2; then
unstaged="${_color_sgr0}"
# else
# unstaged="${_color_red}"
# fi
local upstream=""
# if [[ $(_git_get_touched_files) == "" ]]; then
if git diff --cached --quiet 2>/dev/null >&2; then
upstream="${_color_sgr0}"
else
upstream="${_color_red}"
fi
local count=$(_git_get_revision_count)
if [[ $count == "" ]] || [[ $count == "0" ]]; then
count=""
else
count=" +${count}"
fi
echo -ne "${upstream}[${unstaged}${branch}${_color_sgr0}${count}${upstream}]${_color_sgr0}"
else
return 0
fi
}
if [ "$SSH_CONNECTION" ] || [ "$SSH_TTY" ] || [ "$SSH_CLIENT" ]; then
export PS1="[\u@${_color_blue}\h${_color_sgr0}] ${_color_cyan}(\t) ${_color_green}\w${_color_sgr0} \$(_ps1_color_git): "
else
export PS1="${_color_cyan}(\t) ${_color_green}\w${_color_sgr0} \$(_ps1_color_git): "
fi
# General
complete -cf sudo
complete -cf man
complete -c which
complete -a type
_complete_ssh()
{
COMPREPLY=()
local cur="${COMP_WORDS[COMP_CWORD]}"
local comp_ssh_hosts=`
cat $HOME/.ssh/known_hosts | \
cut -f 1 -d ' ' | \
sed -e s/,.*//g | \
grep -v ^# | \
uniq | \
grep -v "\[" ;
cat $HOME/.ssh/config | \
grep "^Host " | \
awk '{print $2}'
`
COMPREPLY=( $(compgen -W "${comp_ssh_hosts}" -- $cur))
return 0
}
complete -F _complete_ssh ssh
# Bash
if [ -f $(brew --prefix)/etc/bash_completion ]; then
source $(brew --prefix)/etc/bash_completion
fi
# Git
if [ -f $HOME/.git-completion.sh ]; then
source $HOME/.git-completion.sh
# Aliases
completion_aliases+=("gb") # _git_branch
completion_aliases+=("gd") # _git_diff
completion_aliases+=("gf") # _git_fetch
completion_aliases+=("gh") # _git_show
completion_aliases+=("gp") # _git_pull
completion_aliases+=("gr") # _git_rebase
completion_aliases+=("gv") # _git_ls_files
# Functions
completion_aliases+=("gl=git log") # _git_log
completion_aliases+=("ga=git apply") # _git_apply
completion_aliases+=("gu=git push") # _git_push
completion_aliases+=("gc=git checkout") # _git_checkout
completion_aliases+=("gri=git rebase -i") # _git_rebase
completion_aliases+=("gdf=git diff") # _git_diff
completion_aliases+=("ghf=git show") # _git_show
fi
# Bazel
if [ -r $HOME/.cache/bash-completion/bazel.sh ]; then
source $HOME/.cache/bash-completion/bazel.sh
# Functions
completion_aliases+=("bb=bazel build")
completion_aliases+=("bt=bazel test")
fi
# WebKit
if [ -f $WebKit/Tools/Scripts/webkit-tools-completion.sh ]; then
source $WebKit/Tools/Scripts/webkit-tools-completion.sh
completion_aliases+=("wta") # "--platform --ios-simulator --iphone-simulator --ipad-simulator --simulator --gtk --wpe --wincairo --ftw --maccatalyst -t --target --debug --release --64-bit --32-bit --arm --architecture --model -q --quiet -v --verbose --timestamps --json_output -g --guard-malloc --root --wtf-only --webkit-only --web-core-only --webkit-legacy-only -d --dump --build --no-build --timeout --no-timeout --iterations --repeat-each --child-processes --run-singly --force --additional-env-var"
completion_aliases+=("wtl=$WebKit/Tools/Scripts/run-webkit-tests") # "--add-platform-exceptions --complex-text --configuration --debug --exit-after-n-crashes --exit-after-n-failures --force --guard-malloc --help --http --ignore-tests --iterations --launch-safari --leaks --merge-leak-depth --new-test-results --no-build --no-http --no-show-results --no-new-test-results --no-retry-failures --no-sample-on-timeout --no-strip-editing-callbacks --pixel-tests --platform --port --quiet --random --release --reset-results --results-directory --reverse --root --sample-on-timeout --singly --skipped --slowest --strict --strip-editing-callbacks --threaded --time-out-ms --timeout --tolerance --use-remote-links-to-tests --valgrind --verbose -1 -c -g -h -i -l -m -o -p -q -t -v"
fi
# Aliases
_complete_alias() {
local alias_name=$1
local alias_value=${@:2:$#-1}
local alias_value_array
read -r -a alias_value_array <<< "$alias_value"
local comp_words=()
for word in "${COMP_WORDS[@]}"; do
if [[ $word == "$alias_name" ]]; then
comp_words+=("${alias_value_array[@]}")
else
comp_words+=("$word")
fi
done
COMP_WORDS=("${comp_words[@]}")
COMP_LINE=${COMP_LINE//${alias_name}/${alias_value}}
COMP_CWORD=$(( ${#COMP_WORDS[@]} - 1 ))
COMP_POINT=${#COMP_LINE}
local current_word=${COMP_WORDS[$COMP_CWORD]}
local previous_word
if [[ ${#COMP_WORDS[@]} -ge 2 ]]; then
previous_word=${COMP_WORDS[$(( COMP_CWORD - 1 ))]}
fi
local command=${COMP_WORDS[0]}
comp_definition=$(complete -p "$command")
comp_function=$(sed -n "s/^complete .* -F \(.*\) ${command}/\1/p" <<< "$comp_definition")
"$comp_function" "${command}" "${current_word}" "${previous_word}"
}
for ((i = 0; i < ${#completion_aliases[@]}; ++i)); do
IFS="=" read -r -a alias <<< "${completion_aliases[$i]}"
alias_name=${alias[0]}
alias_value=""
if [[ ${#alias[@]} == 1 ]]; then
alias_definition=$(alias "$alias_name")
alias_value=$(dequote "${alias_definition//alias ${alias_name}=}")
else
alias_value=${alias[1]}
fi
eval "_complete_$alias_name() {
_complete_alias ${alias_name} ${alias_value}
}"
eval "complete -o default -o nospace -F _complete_$alias_name $alias_name"
done
unset completion_aliases