diff --git a/lib/git/aliases.sh b/lib/git/aliases.sh index 2d6a6b87..09485402 100644 --- a/lib/git/aliases.sh +++ b/lib/git/aliases.sh @@ -75,7 +75,7 @@ __git_alias () { fi alias $alias_str="$cmd_prefix $cmd${cmd_args:+ }${cmd_args[*]}" - if [ "$shell" = "bash" ]; then + if breeze_shell_is "bash"; then __define_git_completion "$alias_str" "$cmd" complete -o default -o nospace -F _git_"$alias_str"_shortcut "$alias_str" fi @@ -177,10 +177,9 @@ if [ "$git_setup_aliases" = "yes" ]; then _alias "$git_pull_request_alias" 'gh pr' fi - - +# TODO(ghthor): apply these same fixes for NixOS # Tab completion -if [ $shell = "bash" ]; then +if breeze_shell_is "bash"; then # Fix to preload Arch bash completion for git [[ -s "/usr/share/git/completion/git-completion.bash" ]] && source "/usr/share/git/completion/git-completion.bash" # new path in Ubuntu 13.04 diff --git a/lib/git/branch_shortcuts.sh b/lib/git/branch_shortcuts.sh index 0a195f2a..467b2038 100644 --- a/lib/git/branch_shortcuts.sh +++ b/lib/git/branch_shortcuts.sh @@ -47,7 +47,7 @@ __git_alias "$git_branch_delete_alias" "_scmb_git_branch_shortcuts" "-d" __git_alias "$git_branch_delete_force_alias" "_scmb_git_branch_shortcuts" "-D" # Define completions for git branch shortcuts -if [ "$shell" = "bash" ]; then +if breeze_shell_is "bash"; then for alias_str in $git_branch_alias $git_branch_all_alias $git_branch_move_alias $git_branch_delete_alias; do __define_git_completion $alias_str branch complete -o default -o nospace -F _git_"$alias_str"_shortcut $alias_str diff --git a/lib/git/fallback/status_shortcuts_shell.sh b/lib/git/fallback/status_shortcuts_shell.sh index 0a103870..22cdda66 100755 --- a/lib/git/fallback/status_shortcuts_shell.sh +++ b/lib/git/fallback/status_shortcuts_shell.sh @@ -53,10 +53,14 @@ git_status_shortcuts() { echo -e "$c_dark#$c_rst On branch: $c_branch$branch$c_rst $c_dark| [$c_rst*$c_dark]$c_rst => \$$git_env_char*\n$c_dark#$c_rst" for line in $git_status; do - if [[ $shell == *bash ]]; then - x=${line:0:1}; y=${line:1:1}; file=${line:3} + if breeze_shell_is "bash"; then + x=${line:0:1} + y=${line:1:1} + file=${line:3} else - x=$line[1]; y=$line[2]; file=$line[4,-1] + x=$line[1] + y=$line[2] + file=$line[4,-1] fi # Index modification states diff --git a/lib/git/keybindings.sh b/lib/git/keybindings.sh index 4bbca42b..28865915 100644 --- a/lib/git/keybindings.sh +++ b/lib/git/keybindings.sh @@ -10,9 +10,9 @@ # See [here](http://qntm.org/bash#sec1) for info about why I wanted a prompt. # Cross-shell key bindings -_bind(){ +_bind() { if [ -n "$1" ]; then - if [[ $shell == "zsh" ]]; then + if breeze_shell_is "zsh"; then bindkey -s "$1" "$2" else # bash bind "\"$1\": $2" @@ -24,11 +24,11 @@ _bind(){ if [[ "$git_keyboard_shortcuts_enabled" = "true" ]]; then case "$-" in *i*) - if [ -n "$ZSH_VERSION" ]; then - RETURN_CHAR="^M" - else - RETURN_CHAR="\n" - fi + if [ -n "$ZSH_VERSION" ]; then + RETURN_CHAR="^M" + else + RETURN_CHAR="\n" + fi # Uses emacs style keybindings, so vi mode is not supported for now if ! set -o | grep -q '^vi .*on$'; then diff --git a/lib/git/repo_index.sh b/lib/git/repo_index.sh index fa60797a..51622cfb 100644 --- a/lib/git/repo_index.sh +++ b/lib/git/repo_index.sh @@ -79,8 +79,8 @@ function git_index() { echo # If $1 starts with '/', change to top-level directory within $GIT_REPO_DIR - elif ([ $shell = "bash" ] && [ "${1:0:1}" = "/" ]) || \ - ([ $shell = "zsh" ] && [ "${1[1]}" = "/" ]); then + elif (breeze_shell_is "bash" && [ "${1:0:1}" = "/" ]) || \ + (breeze_shell_is "zsh" && [ "${1[1]}" = "/" ]); then if [ -d "$GIT_REPO_DIR$1" ]; then builtin cd "$GIT_REPO_DIR$1" fi @@ -278,7 +278,7 @@ function _git_index_batch_cmd() { } -if [ $shell = 'bash' ]; then +if breeze_shell_is "bash"; then # Bash tab completion function for git_index() function _git_index_tab_completion() { _check_git_index diff --git a/lib/git/shell_shortcuts.sh b/lib/git/shell_shortcuts.sh index de1693dc..3b8f2289 100644 --- a/lib/git/shell_shortcuts.sh +++ b/lib/git/shell_shortcuts.sh @@ -127,7 +127,7 @@ if [ "$shell_ls_aliases_enabled" = "true" ] && builtin command -v ruby >/dev/nul ll_output="$(CLICOLOR_FORCE=1 "${ll_command[@]}" -lG "$@")" fi - if [[ $shell == "zsh" ]]; then + if breeze_shell_is "zsh"; then # Ensure sh_word_split is on [[ -o shwordsplit ]] && SHWORDSPLIT_ON=true setopt shwordsplit @@ -227,7 +227,7 @@ EOF done # Turn off shwordsplit unless it was on previously - if [[ $shell == "zsh" && -z $SHWORDSPLIT_ON ]]; then unsetopt shwordsplit; fi + if breeze_shell_is "zsh" && [[ -z $SHWORDSPLIT_ON ]]; then unsetopt shwordsplit; fi } # Setup aliases diff --git a/lib/git/status_shortcuts.sh b/lib/git/status_shortcuts.sh index a8616ce6..9f40e53a 100644 --- a/lib/git/status_shortcuts.sh +++ b/lib/git/status_shortcuts.sh @@ -8,7 +8,6 @@ # Numbered file shortcuts for git commands # ------------------------------------------------------------------------------ - # Processes 'git status --porcelain', and exports numbered # env variables that contain the path of each affected file. # Output is also more concise than standard 'git status'. @@ -17,6 +16,11 @@ # 1 || staged, 2 || unmerged, 3 || unstaged, 4 || untracked # -------------------------------------------------------------------- git_status_shortcuts() { + if [ "${scmbDebug:-}" = "true" ]; then + set -x + trap "set +x;" RETURN + fi + fail_if_not_git_repo || return 1 zsh_compat # Ensure shwordsplit is on for zsh git_clear_vars @@ -50,8 +54,6 @@ git_status_shortcuts() { zsh_reset # Reset zsh environment to default } - - # 'git add' & 'git rm' wrapper # This shortcut means 'stage the change to the file' # i.e. It will add new and changed files, and remove deleted files. @@ -155,8 +157,8 @@ scmb_expand_args() { _print_path() { local pathname pathname=$(eval printf '%s' "\"\${$2}\"") - if [ "$1" = 1 ]; then # print relative - pathname=${pathname#$PWD/} # Remove $PWD from beginning of the path + if [ "$1" = 1 ]; then # print relative + pathname=${pathname#$PWD/} # Remove $PWD from beginning of the path fi printf '%s' "$pathname" } @@ -183,7 +185,6 @@ git_clear_vars() { done } - # Shortcuts for resolving merge conflicts. _git_resolve_merge_conflict() { if [ -n "$2" ]; then @@ -216,7 +217,7 @@ git_commit_prompt() ( saved_commit_msg="$(cat /tmp/.git_commit_message~)" echo -e "\033[0;36mLeave blank to use saved commit message: \033[0m$saved_commit_msg" fi - if [[ $shell == "zsh" ]]; then + if breeze_shell_is "zsh"; then vared -h -p "Commit Message: " commit_msg else read -r -e -p "Commit Message: " commit_msg @@ -242,7 +243,7 @@ git_commit_prompt() ( escaped_msg=$(echo "$commit_msg" | sed -e 's/"/\\"/g' -e "s/!/\"'!'\"/g") # Add command to bash history, so that if a git pre-commit hook fails, # you can just press "up" and "return" to retry the commit. - if [[ $shell == "zsh" ]]; then + if breeze_shell_is "zsh"; then # zsh's print needs double escaping print -s "git commit -m \"${escaped_msg//\\/\\\\}\"" else @@ -252,14 +253,14 @@ git_commit_prompt() ( fi # Also save the commit message to a temp file in case git commit fails - echo "$commit_msg" > "/tmp/.git_commit_message~" + echo "$commit_msg" >"/tmp/.git_commit_message~" eval $@ # run any prequisite commands echo "$commit_msg" | git commit -F - | tail -n +2 # Fetch the pipe status (for both bash and zsh): GIT_PIPE_STATUS=("${PIPESTATUS[@]}${pipestatus[@]}") - if [[ $shell == "zsh" ]]; then + if breeze_shell_is "zsh"; then git_exit_status="${GIT_PIPE_STATUS[2]}" # zsh array indexes start at 1 else git_exit_status="${GIT_PIPE_STATUS[1]}" diff --git a/lib/git/tools.sh b/lib/git/tools.sh index c158a772..313c6276 100644 --- a/lib/git/tools.sh +++ b/lib/git/tools.sh @@ -10,7 +10,6 @@ # at https://github.com/ndbroadbent/scm_breeze # ----------------------------------------------------------------- - # Remove files/folders from git history # ------------------------------------------------------------------- # To use it, cd to your repository's root and then run the function @@ -30,7 +29,6 @@ git_remove_history() { rm -rf .git/refs/original/ && $_git_cmd reflog expire --all && $_git_cmd gc --aggressive --prune } - # Set default remote and merge for a git branch (pull and push) # Usage: git_set_default_remote(branch = master, remote = origin) git_set_default_remote() { @@ -68,8 +66,6 @@ git_exclude_basename() { exec_scmb_expand_args __git_exclude_basename "$@" } - - # Use git bisect to find where text was removed from a file. # # Example: @@ -91,7 +87,6 @@ git_bisect_grep() { $_git_cmd bisect run grep -qRE "$2" $search_path } - # Removes a git submodule # (from http://stackoverflow.com/a/7646931/304706) # Deletes the sections from .gitmodules, .git/config, @@ -108,7 +103,6 @@ git_submodule_rm() { $_git_cmd rm --cached "$1" } - # Swaps git remotes # i.e. swap origin <-> username git_swap_remotes() { @@ -122,11 +116,10 @@ git_swap_remotes() { echo "Swapped $1 <-> $2" } # (use git fetch tab completion) -if [ "$shell" = "bash" ]; then +if breeze_shell_is "bash"; then complete -o default -o nospace -F _git_fetch git_swap_remotes fi - # Delete a git branch from local, cached remote and remote server git_branch_delete_all() { if [ -z "$1" ]; then diff --git a/lib/scm_breeze.sh b/lib/scm_breeze.sh index 2744b007..f5a06ed0 100644 --- a/lib/scm_breeze.sh +++ b/lib/scm_breeze.sh @@ -1,13 +1,48 @@ # Detect shell -if [ -n "${ZSH_VERSION:-}" ]; then shell="zsh"; else shell="bash"; fi +breeze_detect_shell() { + if [ -n "${ZSH_VERSION:-}" ]; then + echo "zsh" + else + echo "bash" + fi +} + +breeze_shell_is() { + [ "$(breeze_detect_shell)" = "$1" ] && return 0 + return 1 +} # Detect whether zsh 'shwordsplit' option is on by default. -if [ $shell = "zsh" ]; then zsh_shwordsplit=$( (setopt | grep -q shwordsplit) && echo "true" ); fi +if breeze_shell_is "zsh"; then + zsh_shwordsplit=$( (setopt | grep -q shwordsplit) && echo "true") +fi + # Switch on/off shwordsplit for functions that require it. -zsh_compat(){ if [ $shell = "zsh" ] && [ -z $zsh_shwordsplit ]; then setopt shwordsplit; fi; } -zsh_reset(){ if [ $shell = "zsh" ] && [ -z $zsh_shwordsplit ]; then unsetopt shwordsplit; fi; } +zsh_compat() { + if breeze_shell_is "zsh" && [ -z $zsh_shwordsplit ]; then + setopt shwordsplit + fi +} +zsh_reset() { + if breeze_shell_is "zsh" && [ -z $zsh_shwordsplit ]; then + unsetopt shwordsplit + fi +} + # Enable/disable nullglob for zsh or bash -enable_nullglob() { if [ $shell = "zsh" ]; then setopt NULL_GLOB; else shopt -s nullglob; fi; } -disable_nullglob() { if [ $shell = "zsh" ]; then unsetopt NULL_GLOB; else shopt -u nullglob; fi; } +enable_nullglob() { + if breeze_shell_is "zsh"; then + setopt NULL_GLOB + else + shopt -s nullglob + fi +} +disable_nullglob() { + if breeze_shell_is "zsh"; then + unsetopt NULL_GLOB + else + shopt -u nullglob + fi +} # Alias wrapper that ignores errors if alias is not defined. _safe_alias(){ alias "$@" 2> /dev/null; } @@ -31,7 +66,7 @@ function token_quote { # Keep this code for use when minimum versions of {ba,z}sh can be increased. # See https://github.com/scmbreeze/scm_breeze/issues/260 # - # if [[ $shell = bash ]]; then + # if breeze_detect_shell "bash"; then # # ${parameter@operator} where parameter is ${@} and operator is 'Q' # # https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html # eval "${@@Q}" @@ -50,7 +85,7 @@ function _safe_eval() { # Keep this code for use when minimum versions of {ba,z}sh can be increased. # See https://github.com/scmbreeze/scm_breeze/issues/260 # - # if [[ $shell = bash ]]; then + # if breeze_detect_shell "bash"; then # # ${parameter@operator} where parameter is ${@} and operator is 'Q' # # https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html # eval "${@@Q}" @@ -60,8 +95,8 @@ function _safe_eval() { # fi } -find_binary(){ - if [ $shell = "zsh" ]; then +find_binary() { + if breeze_shell_is "bash"; then builtin type -p "$1" | sed "s/$1 is //" | head -1 else builtin type -P "$1" diff --git a/test/lib/git/status_shortcuts_test.sh b/test/lib/git/status_shortcuts_test.sh index d24ed231..5ba6a7b5 100755 --- a/test/lib/git/status_shortcuts_test.sh +++ b/test/lib/git/status_shortcuts_test.sh @@ -335,7 +335,7 @@ test_git_commit_prompt() { assertIncludes "$git_show_output" "$commit_msg" # Test that history was appended correctly. - if [[ $shell == "zsh" ]]; then + if breeze_shell_is "zsh"; then test_history="$(history)" # TODO(ghthor): zsh isn't working here # assertIncludes "$test_history" "git commit -m \"$dbl_escaped_msg\"" @@ -371,7 +371,7 @@ test_git_commit_prompt_with_append() { assertIncludes "$git_show_output" "$commit_msg \[ci skip\]" # Test that history was appended correctly. - if [[ $shell == "zsh" ]]; then + if breeze_shell_is "zsh"; then test_history="$(history)" # TODO(ghthor): zsh isn't working here # assertIncludes "$test_history" "$commit_msg \[ci skip\]"