Skip to content

Instantly share code, notes, and snippets.

@wolever
Created September 12, 2014 22:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolever/595eaa338e16ac59ade9 to your computer and use it in GitHub Desktop.
Save wolever/595eaa338e16ac59ade9 to your computer and use it in GitHub Desktop.
vs: shell functions to transparently call hg or git, whichever is appropriate
vs() {
# Guesses which version control system is correct for the current
# directory, then executes it with "${@}":
# $ cd hg_repo/
# $ vs version
# Mercurial Distributed SCM
# ...
# $ cd ../git_repo
# $ vs version
# git version 1.8.1.1
local vcs
vcs="$(vsname)" || {
echo "error: not in a VCS repository." >&2
return 1
}
"$vcs" "${@}"
return $?
}
vsname() {
# Guesses which version control system is correct for the current
# directory:
# $ cd hg_repo/
# $ vsname
# hg
# $ cd ../git_repo
# $ vsname
# git
# $ cd /tmp/
# $ vsname
# $ echo $?
# 1
local test_vcses=( "hg" "git" )
local vcs=""
local dir="$PWD"
while [[ "$dir" > "/" ]]; do
local test_vcs
for test_vcs in "${test_vcses[@]}"; do
if [[ -d "$dir/.$test_vcs" ]]; then
vcs="$test_vcs"
break;
fi
done
dir="${dir%/*}"
done
if [[ -z "$vcs" ]]; then
return 1
fi
echo "$vcs"
return 0
}
for vscmd in di ci st pull push co show
do
alias $vscmd="vs $vscmd"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment