From 197dcfc74f4e34bb1aca3ed4ca236cf03803aa68 Mon Sep 17 00:00:00 2001 From: Nathan Broadbent Date: Wed, 19 Sep 2012 21:30:07 +1200 Subject: [PATCH] Fail git status if .git not in cwd or parent directory --- lib/git/helpers.sh | 8 ++++++++ lib/git/status_shortcuts.sh | 9 +++++++-- scm_breeze.sh | 1 + 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 lib/git/helpers.sh diff --git a/lib/git/helpers.sh b/lib/git/helpers.sh new file mode 100644 index 0000000..cf83285 --- /dev/null +++ b/lib/git/helpers.sh @@ -0,0 +1,8 @@ +function find_in_cwd_or_parent() { + local slashes=${PWD//[^\/]/}; local directory=$PWD; + for (( n=${#slashes}; n>0; --n )); do + test -e "$directory/$1" && echo "$directory/$1" && return 0 + directory="$directory/.." + done + return 1 +} \ No newline at end of file diff --git a/lib/git/status_shortcuts.sh b/lib/git/status_shortcuts.sh index 63dbda4..7a03f2b 100644 --- a/lib/git/status_shortcuts.sh +++ b/lib/git/status_shortcuts.sh @@ -17,6 +17,11 @@ # 1 || staged, 2 || unmerged, 3 || unstaged, 4 || untracked # -------------------------------------------------------------------- git_status_shortcuts() { + # Fail if not a git repo + if ! find_in_cwd_or_parent ".git" > /dev/null; then + echo -e "\e[31mNot a git repository (or any of the parent directories)\e[0m" + return 1 + fi zsh_compat # Ensure shwordsplit is on for zsh git_clear_vars # Run ruby script, store output @@ -120,7 +125,7 @@ scmb_expand_args() { if [ "$first" -eq 1 ]; then first=0; else printf '\t'; fi eval printf '%s' "\"\$$git_env_char$arg\"" elif [[ "$arg" =~ ^[0-9]+-[0-9]+$ ]]; then # Expand ranges into $e{*} variables - + for i in $(eval echo {${arg/-/..}}); do if [ "$first" -eq 1 ]; then first=0; else printf '\t'; fi eval printf '%s' "\"\$$git_env_char$i\"" @@ -148,7 +153,7 @@ git_clear_vars() { # Shortcuts for resolving merge conflicts. -_git_resolve_merge_conflict() { +_git_resolve_merge_conflict() { if [ -n "$2" ]; then # Expand args and process resulting set of files. IFS=$'\t' diff --git a/scm_breeze.sh b/scm_breeze.sh index dbf9438..96bd355 100644 --- a/scm_breeze.sh +++ b/scm_breeze.sh @@ -17,6 +17,7 @@ export scmbDir="$(dirname ${BASH_SOURCE:-$0})" if [[ -s "$HOME/.git.scmbrc" ]]; then # Load git config . "$HOME/.git.scmbrc" + . "$scmbDir/lib/git/helpers.sh" . "$scmbDir/lib/git/aliases.sh" . "$scmbDir/lib/git/keybindings.sh" . "$scmbDir/lib/git/status_shortcuts.sh"