Skip to content

Commit

Permalink
fix: get rid of global $shell that can be unset by direnv
Browse files Browse the repository at this point in the history
  • Loading branch information
ghthor committed Feb 27, 2024
1 parent dd6ee87 commit 6d936bb
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 50 deletions.
7 changes: 3 additions & 4 deletions lib/git/aliases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/git/branch_shortcuts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions lib/git/fallback/status_shortcuts_shell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions lib/git/keybindings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/git/repo_index.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/git/shell_shortcuts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 11 additions & 10 deletions lib/git/status_shortcuts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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"
}
Expand All @@ -183,7 +185,6 @@ git_clear_vars() {
done
}


# Shortcuts for resolving merge conflicts.
_git_resolve_merge_conflict() {
if [ -n "$2" ]; then
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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]}"
Expand Down
9 changes: 1 addition & 8 deletions lib/git/tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -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:
Expand All @@ -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,
Expand All @@ -108,7 +103,6 @@ git_submodule_rm() {
$_git_cmd rm --cached "$1"
}


# Swaps git remotes
# i.e. swap origin <-> username
git_swap_remotes() {
Expand All @@ -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
Expand Down
55 changes: 45 additions & 10 deletions lib/scm_breeze.sh
Original file line number Diff line number Diff line change
@@ -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; }
Expand All @@ -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}"
Expand All @@ -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}"
Expand All @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions test/lib/git/status_shortcuts_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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\""
Expand Down Expand Up @@ -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\]"
Expand Down

0 comments on commit 6d936bb

Please sign in to comment.