Skip to content

Commit d9ff00c

Browse files
authored
fix: replace global shell variable with JIT shell detection (#336)
This fixes conflicts where when direnv/nix develop activates it would unset this global `shell` variable.
2 parents 683c21b + cf79fd0 commit d9ff00c

File tree

10 files changed

+91
-48
lines changed

10 files changed

+91
-48
lines changed

lib/git/aliases.sh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ __git_alias () {
7676

7777
alias $alias_str="$cmd_prefix $cmd${cmd_args:+ }${cmd_args[*]}"
7878
if [ "$GIT_SKIP_SHELL_COMPLETION" != "yes" ]; then
79-
if [ "$shell" = "bash" ]; then
79+
if breeze_shell_is "bash"; then
8080
__define_git_completion "$alias_str" "$cmd"
8181
complete -o default -o nospace -F _git_"$alias_str"_shortcut "$alias_str"
8282
fi
@@ -179,11 +179,10 @@ if [ "$GIT_SETUP_ALIASES" = "yes" ]; then
179179
_alias "$git_pull_request_alias" 'gh pr'
180180
fi
181181

182-
183-
182+
# TODO(ghthor): apply these same fixes for NixOS
184183
# Tab completion
185184
if [ "$GIT_SKIP_SHELL_COMPLETION" != "yes" ]; then
186-
if [ $shell = "bash" ]; then
185+
if breeze_shell_is "bash"; then
187186
# Fix to preload Arch bash completion for git
188187
[[ -s "/usr/share/git/completion/git-completion.bash" ]] && source "/usr/share/git/completion/git-completion.bash"
189188
# new path in Ubuntu 13.04

lib/git/branch_shortcuts.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ function _scmb_git_branch_shortcuts {
2121
fi
2222

2323
# Use ruby to inject numbers into git branch output
24-
ruby -e "$( cat <<EOF
24+
ruby -e "$(
25+
cat <<EOF
2526
output = %x($_git_cmd branch --color=always $(token_quote "$@"))
2627
line_count = output.lines.to_a.size
2728
output.lines.each_with_index do |line, i|
2829
spaces = (line_count > 9 && i < 9 ? " " : " ")
2930
puts line.sub(/^([ *]{2})/, "\\\1\033[2;37m[\033[0m#{i+1}\033[2;37m]\033[0m" << spaces)
3031
end
3132
EOF
32-
)"
33+
)"
3334

3435
# Set numbered file shortcut in variable
3536
local e=1 IFS=$'\n'
@@ -48,7 +49,7 @@ __git_alias "$git_branch_delete_force_alias" "_scmb_git_branch_shortcuts" "-D"
4849

4950
# Define completions for git branch shortcuts
5051
if [ "$GIT_SKIP_SHELL_COMPLETION" != "yes" ]; then
51-
if [ "$shell" = "bash" ]; then
52+
if breeze_shell_is "bash"; then
5253
for alias_str in $git_branch_alias $git_branch_all_alias $git_branch_move_alias $git_branch_delete_alias; do
5354
__define_git_completion $alias_str branch
5455
complete -o default -o nospace -F _git_"$alias_str"_shortcut $alias_str

lib/git/fallback/status_shortcuts_shell.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,14 @@ git_status_shortcuts() {
5353
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"
5454

5555
for line in $git_status; do
56-
if [[ $shell == *bash ]]; then
57-
x=${line:0:1}; y=${line:1:1}; file=${line:3}
56+
if breeze_shell_is "bash"; then
57+
x=${line:0:1}
58+
y=${line:1:1}
59+
file=${line:3}
5860
else
59-
x=$line[1]; y=$line[2]; file=$line[4,-1]
61+
x=$line[1]
62+
y=$line[2]
63+
file=$line[4,-1]
6064
fi
6165

6266
# Index modification states

lib/git/keybindings.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
# See [here](http://qntm.org/bash#sec1) for info about why I wanted a prompt.
1111

1212
# Cross-shell key bindings
13-
_bind(){
13+
_bind() {
1414
if [ -n "$1" ]; then
15-
if [[ $shell == "zsh" ]]; then
15+
if breeze_shell_is "zsh"; then
1616
bindkey -s "$1" "$2"
1717
else # bash
1818
bind "\"$1\": $2"
@@ -24,11 +24,11 @@ _bind(){
2424
if [[ "$git_keyboard_shortcuts_enabled" = "true" ]]; then
2525
case "$-" in
2626
*i*)
27-
if [ -n "$ZSH_VERSION" ]; then
28-
RETURN_CHAR="^M"
29-
else
30-
RETURN_CHAR="\n"
31-
fi
27+
if [ -n "$ZSH_VERSION" ]; then
28+
RETURN_CHAR="^M"
29+
else
30+
RETURN_CHAR="\n"
31+
fi
3232

3333
# Uses emacs style keybindings, so vi mode is not supported for now
3434
if ! set -o | grep -q '^vi .*on$'; then

lib/git/repo_index.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ function git_index() {
7979
echo
8080

8181
# If $1 starts with '/', change to top-level directory within $GIT_REPO_DIR
82-
elif ([ $shell = "bash" ] && [ "${1:0:1}" = "/" ]) || \
83-
([ $shell = "zsh" ] && [ "${1[1]}" = "/" ]); then
82+
elif (breeze_shell_is "bash" && [ "${1:0:1}" = "/" ]) || \
83+
(breeze_shell_is "zsh" && [ "${1[1]}" = "/" ]); then
8484
if [ -d "$GIT_REPO_DIR$1" ]; then
8585
builtin cd "$GIT_REPO_DIR$1"
8686
fi
@@ -278,7 +278,7 @@ function _git_index_batch_cmd() {
278278
}
279279

280280

281-
if [ $shell = 'bash' ]; then
281+
if breeze_shell_is "bash"; then
282282
# Bash tab completion function for git_index()
283283
function _git_index_tab_completion() {
284284
_check_git_index

lib/git/shell_shortcuts.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ if [ "$shell_ls_aliases_enabled" = "true" ] && builtin command -v ruby >/dev/nul
127127
ll_output="$(CLICOLOR_FORCE=1 "${ll_command[@]}" -lG "$@")"
128128
fi
129129

130-
if [[ $shell == "zsh" ]]; then
130+
if breeze_shell_is "zsh"; then
131131
# Ensure sh_word_split is on
132132
[[ -o shwordsplit ]] && SHWORDSPLIT_ON=true
133133
setopt shwordsplit
@@ -227,7 +227,7 @@ EOF
227227
done
228228

229229
# Turn off shwordsplit unless it was on previously
230-
if [[ $shell == "zsh" && -z $SHWORDSPLIT_ON ]]; then unsetopt shwordsplit; fi
230+
if breeze_shell_is "zsh" && [[ -z $SHWORDSPLIT_ON ]]; then unsetopt shwordsplit; fi
231231
}
232232

233233
# Setup aliases

lib/git/status_shortcuts.sh

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
# 1 || staged, 2 || unmerged, 3 || unstaged, 4 || untracked
1818
# --------------------------------------------------------------------
1919
git_status_shortcuts() {
20+
if [ "${scmbDebug:-}" = "true" ]; then
21+
set -x
22+
trap "set +x;" RETURN
23+
fi
24+
2025
fail_if_not_git_repo || return 1
2126
zsh_compat # Ensure shwordsplit is on for zsh
2227
git_clear_vars
@@ -155,8 +160,8 @@ scmb_expand_args() {
155160
_print_path() {
156161
local pathname
157162
pathname=$(eval printf '%s' "\"\${$2}\"")
158-
if [ "$1" = 1 ]; then # print relative
159-
pathname=${pathname#$PWD/} # Remove $PWD from beginning of the path
163+
if [ "$1" = 1 ]; then # print relative
164+
pathname=${pathname#$PWD/} # Remove $PWD from beginning of the path
160165
fi
161166
printf '%s' "$pathname"
162167
}
@@ -216,7 +221,7 @@ git_commit_prompt() (
216221
saved_commit_msg="$(cat /tmp/.git_commit_message~)"
217222
echo -e "\033[0;36mLeave blank to use saved commit message: \033[0m$saved_commit_msg"
218223
fi
219-
if [[ $shell == "zsh" ]]; then
224+
if breeze_shell_is "zsh"; then
220225
vared -h -p "Commit Message: " commit_msg
221226
else
222227
read -r -e -p "Commit Message: " commit_msg
@@ -242,7 +247,7 @@ git_commit_prompt() (
242247
escaped_msg=$(echo "$commit_msg" | sed -e 's/"/\\"/g' -e "s/!/\"'!'\"/g")
243248
# Add command to bash history, so that if a git pre-commit hook fails,
244249
# you can just press "up" and "return" to retry the commit.
245-
if [[ $shell == "zsh" ]]; then
250+
if breeze_shell_is "zsh"; then
246251
# zsh's print needs double escaping
247252
print -s "git commit -m \"${escaped_msg//\\/\\\\}\""
248253
else
@@ -252,14 +257,14 @@ git_commit_prompt() (
252257
fi
253258

254259
# Also save the commit message to a temp file in case git commit fails
255-
echo "$commit_msg" > "/tmp/.git_commit_message~"
260+
echo "$commit_msg" >"/tmp/.git_commit_message~"
256261
eval $@ # run any prequisite commands
257262

258263
echo "$commit_msg" | git commit -F - | tail -n +2
259264

260265
# Fetch the pipe status (for both bash and zsh):
261266
GIT_PIPE_STATUS=("${PIPESTATUS[@]}${pipestatus[@]}")
262-
if [[ $shell == "zsh" ]]; then
267+
if breeze_shell_is "zsh"; then
263268
git_exit_status="${GIT_PIPE_STATUS[2]}" # zsh array indexes start at 1
264269
else
265270
git_exit_status="${GIT_PIPE_STATUS[1]}"

lib/git/tools.sh

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,21 @@
1919
git_remove_history() {
2020
# Make sure we're at the root of a git repo
2121
if [ ! -d .git ]; then
22-
echo "Error: must run this script from the root of a git repository"
23-
return
22+
echo "Error: must run this script from the root of a git repository"
23+
return
2424
fi
2525
# Remove all paths passed as arguments from the history of the repo
2626
local files
2727
files=("$@")
2828
$_git_cmd filter-branch --index-filter "$_git_cmd rm -rf --cached --ignore-unmatch ${files[*]}" HEAD
2929
# Remove the temporary history git-filter-branch otherwise leaves behind for a long time
30-
rm -rf .git/refs/original/ && $_git_cmd reflog expire --all && $_git_cmd gc --aggressive --prune
30+
rm -rf .git/refs/original/ && $_git_cmd reflog expire --all && $_git_cmd gc --aggressive --prune
3131
}
3232

33-
3433
# Set default remote and merge for a git branch (pull and push)
3534
# Usage: git_set_default_remote(branch = master, remote = origin)
3635
git_set_default_remote() {
37-
curr_branch=$($_git_cmd branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
36+
curr_branch=$($_git_cmd branch 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
3837
if [ -n "$1" ]; then branch="$1"; else branch="$curr_branch"; fi
3938
if [ -n "$2" ]; then remote="$2"; else remote="origin"; fi
4039
echo "branch.$branch.remote: $remote"
@@ -47,7 +46,7 @@ git_set_default_remote() {
4746
# Usage: git_ignore [rule] [ignore_file=.gitignore]
4847
__git_ignore() {
4948
if [ -n "$2" ]; then local f="$2"; else local f=".gitignore"; fi
50-
if [ -n "$1" ] && ! ([ -e $f ] && grep -q "$1" $f); then echo "$1" >> $f; fi
49+
if [ -n "$1" ] && ! ([ -e $f ] && grep -q "$1" $f); then echo "$1" >>$f; fi
5150
}
5251
# Always expand args
5352
git_ignore() {
@@ -81,7 +80,7 @@ git_exclude_basename() {
8180
#
8281
git_bisect_grep() {
8382
if [ -z "$2" ]; then
84-
echo "Usage: $0 <good_revision> <string>";
83+
echo "Usage: $0 <good_revision> <string>"
8584
return
8685
fi
8786
if [ -n "$3" ]; then search_path="$3"; else search_path="."; fi
@@ -123,7 +122,7 @@ git_swap_remotes() {
123122
}
124123
# (use git fetch tab completion)
125124
if [ "$GIT_SKIP_SHELL_COMPLETION" != "yes" ]; then
126-
if [ "$shell" = "bash" ]; then
125+
if breeze_shell_is "bash"; then
127126
complete -o default -o nospace -F _git_fetch git_swap_remotes
128127
fi
129128
fi

lib/scm_breeze.sh

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,48 @@
11
# Detect shell
2-
if [ -n "${ZSH_VERSION:-}" ]; then shell="zsh"; else shell="bash"; fi
2+
breeze_detect_shell() {
3+
if [ -n "${ZSH_VERSION:-}" ]; then
4+
echo "zsh"
5+
else
6+
echo "bash"
7+
fi
8+
}
9+
10+
breeze_shell_is() {
11+
[ "$(breeze_detect_shell)" = "$1" ] && return 0
12+
return 1
13+
}
314
# Detect whether zsh 'shwordsplit' option is on by default.
4-
if [ $shell = "zsh" ]; then zsh_shwordsplit=$( (setopt | grep -q shwordsplit) && echo "true" ); fi
15+
if breeze_shell_is "zsh"; then
16+
zsh_shwordsplit=$( (setopt | grep -q shwordsplit) && echo "true")
17+
fi
18+
519
# Switch on/off shwordsplit for functions that require it.
6-
zsh_compat(){ if [ $shell = "zsh" ] && [ -z $zsh_shwordsplit ]; then setopt shwordsplit; fi; }
7-
zsh_reset(){ if [ $shell = "zsh" ] && [ -z $zsh_shwordsplit ]; then unsetopt shwordsplit; fi; }
20+
zsh_compat() {
21+
if breeze_shell_is "zsh" && [ -z $zsh_shwordsplit ]; then
22+
setopt shwordsplit
23+
fi
24+
}
25+
zsh_reset() {
26+
if breeze_shell_is "zsh" && [ -z $zsh_shwordsplit ]; then
27+
unsetopt shwordsplit
28+
fi
29+
}
30+
831
# Enable/disable nullglob for zsh or bash
9-
enable_nullglob() { if [ $shell = "zsh" ]; then setopt NULL_GLOB; else shopt -s nullglob; fi; }
10-
disable_nullglob() { if [ $shell = "zsh" ]; then unsetopt NULL_GLOB; else shopt -u nullglob; fi; }
32+
enable_nullglob() {
33+
if breeze_shell_is "zsh"; then
34+
setopt NULL_GLOB
35+
else
36+
shopt -s nullglob
37+
fi
38+
}
39+
disable_nullglob() {
40+
if breeze_shell_is "zsh"; then
41+
unsetopt NULL_GLOB
42+
else
43+
shopt -u nullglob
44+
fi
45+
}
1146

1247
# Alias wrapper that ignores errors if alias is not defined.
1348
_safe_alias(){ alias "$@" 2> /dev/null; }
@@ -31,7 +66,7 @@ function token_quote {
3166
# Keep this code for use when minimum versions of {ba,z}sh can be increased.
3267
# See https://github.com/scmbreeze/scm_breeze/issues/260
3368
#
34-
# if [[ $shell = bash ]]; then
69+
# if breeze_detect_shell "bash"; then
3570
# # ${parameter@operator} where parameter is ${@} and operator is 'Q'
3671
# # https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
3772
# eval "${@@Q}"
@@ -50,7 +85,7 @@ function _safe_eval() {
5085
# Keep this code for use when minimum versions of {ba,z}sh can be increased.
5186
# See https://github.com/scmbreeze/scm_breeze/issues/260
5287
#
53-
# if [[ $shell = bash ]]; then
88+
# if breeze_detect_shell "bash"; then
5489
# # ${parameter@operator} where parameter is ${@} and operator is 'Q'
5590
# # https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
5691
# eval "${@@Q}"
@@ -60,8 +95,8 @@ function _safe_eval() {
6095
# fi
6196
}
6297

63-
find_binary(){
64-
if [ $shell = "zsh" ]; then
98+
find_binary() {
99+
if breeze_shell_is "bash"; then
65100
builtin type -p "$1" | sed "s/$1 is //" | head -1
66101
else
67102
builtin type -P "$1"

test/lib/git/status_shortcuts_test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ test_git_commit_prompt() {
335335
assertIncludes "$git_show_output" "$commit_msg"
336336

337337
# Test that history was appended correctly.
338-
if [[ $shell == "zsh" ]]; then
338+
if breeze_shell_is "zsh"; then
339339
test_history="$(history)"
340340
# TODO(ghthor): zsh isn't working here
341341
# assertIncludes "$test_history" "git commit -m \"$dbl_escaped_msg\""
@@ -371,7 +371,7 @@ test_git_commit_prompt_with_append() {
371371
assertIncludes "$git_show_output" "$commit_msg \[ci skip\]"
372372

373373
# Test that history was appended correctly.
374-
if [[ $shell == "zsh" ]]; then
374+
if breeze_shell_is "zsh"; then
375375
test_history="$(history)"
376376
# TODO(ghthor): zsh isn't working here
377377
# assertIncludes "$test_history" "$commit_msg \[ci skip\]"

0 commit comments

Comments
 (0)