From 5f286eaaaa587ad96de2d61aa03a17ddcf4c0395 Mon Sep 17 00:00:00 2001 From: "Tom \"Ravi\" Hale" Date: Fri, 24 Aug 2018 08:32:23 +0700 Subject: [PATCH] Quote arrays to avoid splitting by $IFS --- lib/git/aliases.sh | 2 +- lib/git/branch_shortcuts.sh | 2 +- lib/git/repo_index.sh | 14 +++++++------- lib/git/shell_shortcuts.sh | 4 ++-- lib/git/status_shortcuts.sh | 8 ++++---- lib/git/tools.sh | 4 ++-- test/lib/git/shell_shortcuts_test.sh | 2 +- test/support/shunit2 | 6 +++--- 8 files changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/git/aliases.sh b/lib/git/aliases.sh index 28b0e31..e9c8f59 100644 --- a/lib/git/aliases.sh +++ b/lib/git/aliases.sh @@ -71,7 +71,7 @@ __git_alias () { alias_str="$1"; cmd_prefix="$2"; cmd="$3"; if [ $# -gt 2 ]; then shift 3 2>/dev/null - cmd_args=$@ + cmd_args=("$@") fi alias $alias_str="$cmd_prefix $cmd${cmd_args:+ }${cmd_args[*]}" diff --git a/lib/git/branch_shortcuts.sh b/lib/git/branch_shortcuts.sh index 61012e2..7157339 100644 --- a/lib/git/branch_shortcuts.sh +++ b/lib/git/branch_shortcuts.sh @@ -22,7 +22,7 @@ function _scmb_git_branch_shortcuts { # Use ruby to inject numbers into ls output ruby -e "$( cat < 9 && i < 9 ? " " : " ") diff --git a/lib/git/repo_index.sh b/lib/git/repo_index.sh index 3d2e9c7..9fc2b44 100644 --- a/lib/git/repo_index.sh +++ b/lib/git/repo_index.sh @@ -217,10 +217,10 @@ _git_index_update_all_branches() { # Ignore branch if remote and merge is not configured if [[ -n "$remote" ]] && [[ -n "$merge" ]]; then - branches=(${branches[@]} "$branch") - remotes=(${remotes[@]} "$remote") + branches=("${branches[@]}" "$branch") + remotes=("${remotes[@]}" "$remote") # Get branch from merge ref (refs/heads/master => master) - merges=(${merges[@]} "$(basename $merge)") + merges=("${merges[@]}" "$(basename "$merge")") else echo "=== Skipping $branch: remote and merge refs are not configured." fi @@ -232,12 +232,12 @@ _git_index_update_all_branches() { local index=0 # Iterate over branches, and update those that can be fast-forwarded - for branch in ${branches[@]}; do + for branch in "${branches[@]}"; do branch_rev="$(git rev-parse $branch)" # Local branch can be fast-forwarded if revision is ancestor of remote revision, and not the same. # (see http://stackoverflow.com/a/2934062/304706) - if [[ "$branch_rev" != "$(git rev-parse ${remotes[$index]}/${merges[$index]})" ]] && \ - [[ "$(git merge-base $branch_rev ${remotes[$index]}/${merges[$index]})" = "$branch_rev" ]]; then + if [[ "$branch_rev" != "$(git rev-parse "${remotes[$index]}/${merges[$index]}")" ]] && \ + [[ "$(git merge-base "$branch_rev" "${remotes[$index]}/${merges[$index]}")" = "$branch_rev" ]]; then echo "=== Updating $branch branch in $base_path from ${remotes[$index]}/${merges[$index]}..." # Checkout branch if we aren't already on it. if [[ "$branch" != "$(parse_git_branch)" ]]; then git checkout $branch; fi @@ -272,7 +272,7 @@ function _git_index_batch_cmd() { local base_path for base_path in $(sed -e "s/--.*//" "$GIT_REPO_DIR/.git_index" | \grep . | sort); do builtin cd "$base_path" - $@ + "$@" done else echo "Please give a command to run for all repos. (It may be useful to write your command as a function or script.)" diff --git a/lib/git/shell_shortcuts.sh b/lib/git/shell_shortcuts.sh index 4c427d8..2cadc24 100644 --- a/lib/git/shell_shortcuts.sh +++ b/lib/git/shell_shortcuts.sh @@ -126,7 +126,7 @@ if [ "$shell_ls_aliases_enabled" = "true" ] && which ruby > /dev/null 2>&1; then # Parse path from args IFS=$'\n' - for arg in $@; do + for arg in "$@"; do if [ -d "$arg" ]; then local rel_path="${arg%/}"; fi done unset IFS @@ -142,7 +142,7 @@ if [ "$shell_ls_aliases_enabled" = "true" ] && which ruby > /dev/null 2>&1; then puts o.lines.map{|l|l.sub(re){|m|\"%s%-#{u}s %-#{g}s%#{s}s \"%[\$1,*\$3.split]}}" } - ll_output=$(echo "$ll_output" | \sed -$SED_REGEX_ARG "s/ $USER/ $(/bin/cat $HOME/.user_sym)/g" | rejustify_ls_columns) + ll_output=$(echo "$ll_output" | \sed -$SED_REGEX_ARG "s/ $USER/ $(/bin/cat "$HOME/.user_sym")/g" | rejustify_ls_columns) fi if [ "$(echo "$ll_output" | wc -l)" -gt "50" ]; then diff --git a/lib/git/status_shortcuts.sh b/lib/git/status_shortcuts.sh index d63bb0c..ee0f804 100644 --- a/lib/git/status_shortcuts.sh +++ b/lib/git/status_shortcuts.sh @@ -21,7 +21,7 @@ git_status_shortcuts() { zsh_compat # Ensure shwordsplit is on for zsh git_clear_vars # Run ruby script, store output - local cmd_output="$(/usr/bin/env ruby "$scmbDir/lib/git/status_shortcuts.rb" $@)" + local cmd_output="$(/usr/bin/env ruby "$scmbDir/lib/git/status_shortcuts.rb" "$@")" # Print debug information if $scmbDebug = "true" if [ "${scmbDebug:-}" = "true" ]; then printf "status_shortcuts.rb output => \n$cmd_output\n------------------------\n" @@ -102,8 +102,8 @@ git_show_affected_files(){ fail_if_not_git_repo || return 1 f=0 # File count # Show colored revision and commit message - echo -n "# "; git show --oneline --name-only $@ | head -n1; echo "# " - for file in $(git show --pretty="format:" --name-only $@ | \grep -v '^$'); do + echo -n "# "; git show --oneline --name-only "$@" | head -n1; echo "# " + for file in $(git show --pretty="format:" --name-only "$@" | \grep -v '^$'); do let f++ export $git_env_char$f=$file # Export numbered variable. echo -e "# \033[2;37m[\033[0m$f\033[2;37m]\033[0m $file" @@ -210,7 +210,7 @@ git_commit_prompt() { fi if [ -n "$commit_msg" ]; then - eval $@ # run any prequisite commands + eval "$@" # run any prequisite commands # Add $APPEND to commit message, if given. (Used to append things like [ci skip] for Travis CI) if [ -n "$APPEND" ]; then commit_msg="$commit_msg $APPEND"; fi echo $commit_msg | git commit -F - | tail -n +2 diff --git a/lib/git/tools.sh b/lib/git/tools.sh index 0c62e72..e5e2c5d 100644 --- a/lib/git/tools.sh +++ b/lib/git/tools.sh @@ -23,8 +23,8 @@ git_remove_history() { return fi # Remove all paths passed as arguments from the history of the repo - files=$@ - $_git_cmd filter-branch --index-filter "$_git_cmd rm -rf --cached --ignore-unmatch $files" HEAD + 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 rm -rf .git/refs/original/ && $_git_cmd reflog expire --all && $_git_cmd gc --aggressive --prune } diff --git a/test/lib/git/shell_shortcuts_test.sh b/test/lib/git/shell_shortcuts_test.sh index f699085..3842590 100755 --- a/test/lib/git/shell_shortcuts_test.sh +++ b/test/lib/git/shell_shortcuts_test.sh @@ -38,7 +38,7 @@ oneTimeSetUp() { alias rvm="test" # Ensure tests run if RVM isn't loaded but $HOME/.rvm is present # Test functions - function ln() { ln $@; } + function ln() { ln "$@"; } # Before aliasing, get original locations so we can compare them in the test unalias mv rm sed cat 2>/dev/null diff --git a/test/support/shunit2 b/test/support/shunit2 index 8ca4b4b..df37583 100755 --- a/test/support/shunit2 +++ b/test/support/shunit2 @@ -25,9 +25,9 @@ SHUNIT_ERROR=2 # enable strict mode by default SHUNIT_STRICT=${SHUNIT_STRICT:-${SHUNIT_TRUE}} -_shunit_warn() { echo "shunit2:WARN $@" >&2; } -_shunit_error() { echo "shunit2:ERROR $@" >&2; } -_shunit_fatal() { echo "shunit2:FATAL $@" >&2; exit ${SHUNIT_ERROR}; } +_shunit_warn() { echo "shunit2:WARN $*" >&2; } +_shunit_error() { echo "shunit2:ERROR $*" >&2; } +_shunit_fatal() { echo "shunit2:FATAL $*" >&2; exit ${SHUNIT_ERROR}; } # specific shell checks if [ -n "${ZSH_VERSION:-}" ]; then