# ============================================= #
# ------------------ 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
}
# 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 ---------------- #
# ============================================= #
_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_hosts ()
{
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_hosts ssh
# Git
if [ -f $HOME/.git-completion.sh ]; then
source $HOME/.git-completion.sh
# Aliases
complete -o default -o nospace -F _git_branch gb
complete -o default -o nospace -F _git_diff gd
complete -o default -o nospace -F _git_fetch gf
complete -o default -o nospace -F _git_show gh
complete -o default -o nospace -F _git_pull gp
complete -o default -o nospace -F _git_rebase gr
complete -o default -o nospace -F _git_ls_files gv
# Functions
complete -o default -o nospace -F _git_log gl
complete -o default -o nospace -F _git_apply ga
complete -o default -o nospace -F _git_push gu
complete -o default -o nospace -F _git_checkout gc
complete -o default -o nospace -F _git_rebase gc
complete -o default -o nospace -F _git_rebase gri
complete -o default -o nospace -F _git_diff gdf
complete -o default -o nospace -F _git_show ghf
# WebKit
complete -o default -o nospace -F _git_checkout gwc
fi
# WebKit
if [ -f $WebKit/Tools/Scripts/webkit-tools-completion.sh ]; then
source $WebKit/Tools/Scripts/webkit-tools-completion.sh
complete -o default -W "--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" wta
complete -o default -W "--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" wtl
fi