Created
September 12, 2014 22:48
-
-
Save wolever/595eaa338e16ac59ade9 to your computer and use it in GitHub Desktop.
vs: shell functions to transparently call hg or git, whichever is appropriate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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