Provide X-compatible find_binary function that finds a binary for a given command. Works with zsh/bash

This commit is contained in:
Nathan Broadbent
2012-11-17 11:41:08 +13:00
parent c46e165ca8
commit 0a797fb620
2 changed files with 14 additions and 10 deletions

View File

@@ -29,15 +29,15 @@ if [ "$shell_command_wrapping_enabled" = "true" ] || [ "$bash_command_wrapping_e
if [ "${scmbDebug:-}" = "true" ]; then echo "SCMB: $cmd is an alias"; fi if [ "${scmbDebug:-}" = "true" ]; then echo "SCMB: $cmd is an alias"; fi
# Store original alias # Store original alias
local original_alias="$(whence $cmd)" local original_alias="$(whence $cmd)"
# Remove alias, so that which can return binary # Remove alias, so that we can find binary
unalias $cmd unalias $cmd
# Detect original $cmd type, and escape # Detect original $cmd type, and escape
case "$(type $cmd 2>&1)" in case "$(type $cmd 2>&1)" in
# Escape shell builtins with 'builtin' # Escape shell builtins with 'builtin'
*'is a shell builtin'*) local escaped_cmd="builtin $cmd";; *'is a shell builtin'*) local escaped_cmd="builtin $cmd";;
# Get full path for files with 'which' # Get full path for files with 'find_binary' function
*) local escaped_cmd="$(\which $cmd)";; *) local escaped_cmd="$(find_binary $cmd)";;
esac esac
# Expand original command into full path, to avoid infinite loops # Expand original command into full path, to avoid infinite loops
@@ -62,8 +62,8 @@ if [ "$shell_command_wrapping_enabled" = "true" ] || [ "$bash_command_wrapping_e
*) *)
if [ "${scmbDebug:-}" = "true" ]; then echo "SCMB: $cmd is an executable file"; fi if [ "${scmbDebug:-}" = "true" ]; then echo "SCMB: $cmd is an executable file"; fi
# Otherwise, command is a regular script or binary, # Otherwise, command is a regular script or binary,
# and the full path can be found from 'which' # and the full path can be found with 'find_binary' function
alias $cmd="exec_scmb_expand_args $(\which $cmd)";; alias $cmd="exec_scmb_expand_args $(find_binary $cmd)";;
esac esac
done done
# Clean up # Clean up

View File

@@ -12,11 +12,15 @@ disable_nullglob() { if [ $shell = "zsh" ]; then unsetopt NULL_GLOB; else shopt
# Alias wrapper that ignores errors if alias is not defined. # Alias wrapper that ignores errors if alias is not defined.
_alias(){ alias "$@" 2> /dev/null; } _alias(){ alias "$@" 2> /dev/null; }
find_binary(){
if [ $shell = "zsh" ]; then if [ $shell = "zsh" ]; then
export GIT_BINARY=$(type -p git | sed 's/git is //' | head -1) type -p "$1" | sed "s/$1 is //" | head -1
else else
export GIT_BINARY=$(type -P git) type -P "$1"
fi fi
}
export GIT_BINARY=$(find_binary git)
# Updates SCM Breeze from GitHub. # Updates SCM Breeze from GitHub.
update_scm_breeze() { update_scm_breeze() {