Be compatible with shells of Ubuntu 14.04

This commit is contained in:
Tom "Ravi" Hale
2018-09-16 20:31:15 +07:00
parent e5da83665a
commit 48302bcd8c
6 changed files with 39 additions and 24 deletions

View File

@@ -202,15 +202,16 @@ _git_index_update_all_branches() {
return
fi
local remotes merges branches
# zsh 5.0.2 requires local separate to assignment for arrays
local remote merge remotes merges branches
# Get branch configuration from .git/config
local IFS=$'\n'
for branch in $($GIT_BINARY branch 2> /dev/null | sed -e 's/.\{2\}\(.*\)/\1/'); do
# Skip '(no branch)'
if [[ "$branch" = "(no branch)" ]]; then continue; fi
local remote=$(git config --get branch.$branch.remote)
local merge=$(git config --get branch.$branch.merge)
remote=$(git config --get "branch.$branch.remote")
merge=$(git config --get "branch.$branch.merge")
# Ignore branch if remote and merge is not configured
if [[ -n "$remote" ]] && [[ -n "$merge" ]]; then

View File

@@ -79,7 +79,8 @@ git_add_shortcuts() {
git_silent_add_shortcuts() {
if [ -n "$1" ]; then
# Expand args and process resulting set of files.
eval "args=$(scmb_expand_args "$@")" # create $args array
local args
eval args="$(scmb_expand_args "$@")" # populate $args array
for file in "${args[@]}"; do
# Use 'git rm' if file doesn't exist and 'ga_auto_remove' is enabled.
if [[ $ga_auto_remove = yes && ! -e $file ]]; then
@@ -108,8 +109,8 @@ git_show_affected_files(){
done; echo "# "
}
# Allows expansion of numbered shortcuts, ranges of shortcuts, or standard paths.
# Return a string which can be `eval`ed like: eval args="$(scmb_expand_args "$@")"
# Numbered shortcut variables are produced by various commands, such as:
# * git_status_shortcuts() - git status implementation
# * git_show_affected_files() - shows files affected by a given SHA1, etc.
@@ -120,7 +121,8 @@ scmb_expand_args() {
shift
fi
local -a args=() # initially empty array
local args
args=() # initially empty array. zsh 5.0.2 from Ubuntu 14.04 requires this to be separated
for arg in "$@"; do
if [[ "$arg" =~ ^[0-9]{0,4}$ ]] ; then # Substitute $e{*} variables for any integers
if [ -e "$arg" ]; then
@@ -137,14 +139,22 @@ scmb_expand_args() {
args+=("$arg")
fi
done
args=$(declare -p args) # Get $args array as a string which can be `eval`-ed to recreate itself
args=${args#*=} # Remove `typeset -a args=` from beginning of string to allow caller to name variable
echo "$args"
# "declare -p" with zsh 5.0.2 on Ubuntu 14.04 creates a string that it cannot process:
# typeset -a args args=(one three six)
# There should be a ; between the two "args" tokens
# "declare -p" with bash 4.3.11(1) on Ubuntu 14.04 creates a string like:
# declare -a a='([0]="a" [1]="b c" [2]="d")'
# The RHS of this string is incompatible with zsh 5.0.2 and "eval args="
# Generate a quoted array string to assign to "eval args="
echo "( $(token_quote "${args[@]}") )"
}
# Expand a variable (named by $2) into a (possibly relative) pathname
_print_path() {
local pathname=$(eval printf '%s' "\"\${$2}\"")
local pathname
pathname=$(eval printf '%s' "\"\${$2}\"")
if [ "$1" = 1 ]; then # print relative
pathname=${pathname#$PWD/} # Remove $PWD from beginning of the path
fi
@@ -154,7 +164,8 @@ _print_path() {
# Execute a command with expanded args, e.g. Delete files 6 to 12: $ ge rm 6-12
# Fails if command is a number or range (probably not worth fixing)
exec_scmb_expand_args() {
eval "args=$(scmb_expand_args "$@")" # create $args array
local args
eval "args=$(scmb_expand_args "$@")" # populate $args array
_safe_eval "${args[@]}"
}
@@ -177,7 +188,8 @@ git_clear_vars() {
_git_resolve_merge_conflict() {
if [ -n "$2" ]; then
# Expand args and process resulting set of files.
eval "args=$(scmb_expand_args "$@")" # create $args array
local args
eval "args=$(scmb_expand_args "$@")" # populate $args array
for file in "${args[@]:2}"; do
git checkout "--$1""s" "$file" # "--$1""s" is expanded to --ours or --theirs
git add "$file"

View File

@@ -23,6 +23,7 @@ git_remove_history() {
return
fi
# Remove all paths passed as arguments from the history of the repo
local files
files=("$@")
$_git_cmd filter-branch --index-filter "$_git_cmd rm -rf --cached --ignore-unmatch ${files[*]}" HEAD
# Remove the temporary history git-filter-branch otherwise leaves behind for a long time

View File

@@ -21,7 +21,8 @@ _alias() {
# Quote the contents of "$@"
function token_quote {
# Older versions of {ba,z}sh don't support the built-in quoting, so fall back to printf %q
local quoted=()
local quoted
quoted=() # Assign separately for zsh 5.0.2 of Ubuntu 14.04
for token; do
quoted+=( "$(printf '%q' "$token")" )
done

View File

@@ -10,7 +10,7 @@ if [ -z "$TEST_SHELLS" ]; then
fi
echo "== Will run all tests with following shells: ${TEST_SHELLS}"
builtin cd -P -- "${0%/*}" # Change to directory this script lives in
cd -P -- "${0%/*}" # Change to directory this script lives in
for test in $(find test/lib -name *_test.sh); do
for shell in $TEST_SHELLS; do
echo "== Running tests with [$shell]: $test"

View File

@@ -52,19 +52,19 @@ test_scmb_expand_args() {
local e1="one" e2="two" e3="three" e4="four" e5="five" e6="six" e7='$dollar' e8='two words'
local error="Args not expanded correctly"
assertEquals "$error" 'one three six' \
"$(eval a=$(scmb_expand_args 1 3 6); token_quote "${a[@]}")"
"$(eval args="$(scmb_expand_args 1 3 6)"; token_quote "${args[@]}")"
assertEquals "$error" 'one two three five' \
"$(eval a=$(scmb_expand_args 1-3 5); token_quote "${a[@]}")"
"$(eval args="$(scmb_expand_args 1-3 5)"; token_quote "${args[@]}")"
assertEquals "$error" '\$dollar two three four one' \
"$(eval a=$(scmb_expand_args 7 2-4 1); token_quote "${a[@]}")"
"$(eval args="$(scmb_expand_args 7 2-4 1)"; token_quote "${args[@]}")"
# Test that any args with spaces remain quoted
assertEquals "$error" '-m Test\ Commit\ Message one' \
"$(eval a=$(scmb_expand_args -m "Test Commit Message" 1); token_quote "${a[@]}")"
"$(eval args="$(scmb_expand_args -m "Test Commit Message" 1)"; token_quote "${args[@]}")"
assertEquals "$error" '-ma Test\ Commit\ Message Unquoted'\
"$(eval a=$(scmb_expand_args -ma "Test Commit Message" "Unquoted"); token_quote "${a[@]}")"
"$(eval args="$(scmb_expand_args -ma "Test Commit Message" "Unquoted")"; token_quote "${args[@]}")"
assertEquals "$error" '\$dollar one two\ words' \
"$(eval a=$(scmb_expand_args 7 1-1 8); token_quote "${a[@]}")"
"$(eval args="$(scmb_expand_args 7 1-1 8)"; token_quote "${args[@]}")"
# Keep this code for use when minimum versions of {ba,z}sh can be increased.
# See token_quote() source and https://github.com/scmbreeze/scm_breeze/issues/260
@@ -89,14 +89,14 @@ test_scmb_expand_args() {
test_exec_scmb_expand_args() {
local e1="one" e2="a b c" e3='$dollar' e4="single'quote" e5='double"quote' e6='a(){:;};a&'
assertEquals "literals with spaces not preserved" 'foo bar\ baz' \
"$(eval a="$(scmb_expand_args foo 'bar baz')"; token_quote "${a[@]}")"
"$(eval args="$(scmb_expand_args foo 'bar baz')"; token_quote "${args[@]}")"
assertEquals "variables with spaces not preserved" 'one a\ b\ c' \
"$(eval a="$(scmb_expand_args 1-2)"; token_quote "${a[@]}")"
"$(eval args="$(scmb_expand_args 1-2)"; token_quote "${args[@]}")"
# Expecting text: '$dollar' "single'quote" 'double"quote'
# Generate quoted expected string with: token_quote "$(cat)" then copy/paste, ^D
assertEquals "special characters are preserved" \
'\$dollar single\'\''quote double\"quote a\(\)\{:\;\}\;a\&' \
"$(eval a="$(scmb_expand_args 3-6)"; token_quote "${a[@]}")"
"$(eval args="$(scmb_expand_args 3-6)"; token_quote "${args[@]}")"
# Keep this code for use when minimum versions of {ba,z}sh can be increased.
# See token_quote() source and https://github.com/scmbreeze/scm_breeze/issues/260