Skip to content

Instantly share code, notes, and snippets.

Created July 17, 2014 18:45
Show Gist options
  • Save anonymous/5b52dd807f571cbb4790 to your computer and use it in GitHub Desktop.
Save anonymous/5b52dd807f571cbb4790 to your computer and use it in GitHub Desktop.
rw aliases
top_gitdir() {
local git_dir=$(git rev-parse --git-dir 2>/dev/null)
if [ -n "${git_dir}" ]; then
git_dir=$(realpath ${git_dir})
echo ${git_dir/\/.git*/}
fi
}
git_directories() {
gtop || return
local gdirs=$(git ls-files | grep '/' | xargs dirname | sort -u)
local smdirs=$(git submodule foreach -q \
'for f in $(git ls-files | grep '/' | xargs dirname | sort -u);do echo ${prefix}${f};done')
echo "${gdirs} ${smdirs}"
cd - >/dev/null
}
gtop() {
# Goto the top level directory of this git repository
local tdir=$(top_gitdir)
[ -n "${tdir}" ] && cd "${tdir}"
}
__rw_cd() {
local git_dir=$(top_gitdir)
[ -z "${git_dir}" ] && return
local d=$(find ${git_dir}/${1} -type d -iname $2)
if [ -n "${d}" ]; then
cd "$(for path in ${d}; do echo ${#path} ${path};done | sort -n | head -n 1 | cut -d' ' -f2)"
fi
}
# Rift.io Aliases
# Find and cd to a module directory
rwfm() {
__rw_cd "modules" $1
}
# Find the module directory matching the cwd and cd to it
rwcdm() {
rwfm $(basename $(pwd))
}
# Find and cd to a build directory
rwfb() {
__rw_cd ".build" $1
}
# Find the build directory matching the cwd and cd to it
rwcdb() {
rwfb $(basename $(pwd))
}
# Find and cd to an install directory
rwfi() {
__rw_cd ".install" $1
}
# Find the install directory matching the cwd and cd to it
rwcdi() {
rwfi $(basename $(pwd))
}
__rw_dir_completion() {
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts=$(for d in $(git_directories | grep "/${cur}[^/]*$"); do echo ${d##*/};done | sort -u)
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
}
complete -F __rw_dir_completion rwfm rwfb rwfi
# Update the rift repository and all submodules.
rwgpr() {
gtop || return
git pull --rebase || return
git submodule foreach "git checkout master && git pull --rebase"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment