From 384f2a0668d6be616d416c512fc08704aeef7c59 Mon Sep 17 00:00:00 2001 From: Nathan Broadbent Date: Wed, 19 Sep 2012 21:34:03 +1200 Subject: [PATCH] Created fail_if_not_git_repo function and prepended it to all git functions --- lib/git/branch_shortcuts.sh | 1 + lib/git/helpers.sh | 8 ++++++++ lib/git/status_shortcuts.sh | 10 +++++----- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/git/branch_shortcuts.sh b/lib/git/branch_shortcuts.sh index d8120ed..e244227 100644 --- a/lib/git/branch_shortcuts.sh +++ b/lib/git/branch_shortcuts.sh @@ -12,6 +12,7 @@ # Adds numbered shortcuts to output of ls -l, just like 'git status' unalias $git_branch_alias > /dev/null 2>&1; unset -f $git_branch_alias > /dev/null 2>&1 function _scmb_git_branch_shortcuts { + fail_if_not_git_repo || return 1 # Fall back to normal git branch, if any unknown args given if [[ -n "$@" ]] && [[ "$@" != "-a" ]]; then $_git_cmd branch "$@" diff --git a/lib/git/helpers.sh b/lib/git/helpers.sh index cf83285..e73bd6d 100644 --- a/lib/git/helpers.sh +++ b/lib/git/helpers.sh @@ -5,4 +5,12 @@ function find_in_cwd_or_parent() { directory="$directory/.." done return 1 +} + +function fail_if_not_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 + return 0 } \ No newline at end of file diff --git a/lib/git/status_shortcuts.sh b/lib/git/status_shortcuts.sh index 7a03f2b..448b6ad 100644 --- a/lib/git/status_shortcuts.sh +++ b/lib/git/status_shortcuts.sh @@ -17,11 +17,7 @@ # 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 + fail_if_not_git_repo || return 1 zsh_compat # Ensure shwordsplit is on for zsh git_clear_vars # Run ruby script, store output @@ -64,6 +60,7 @@ git_status_shortcuts() { # - 'auto git rm' behaviour can be turned off # ------------------------------------------------------------------------------- git_add_shortcuts() { + fail_if_not_git_repo || return 1 if [ -z "$1" ]; then echo "Usage: ga => git add " echo " ga 1 => git add \$e1" @@ -102,6 +99,7 @@ git_silent_add_shortcuts() { # Prints a list of all files affected by a given SHA1, # and exports numbered environment variables for each file. 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 "# " @@ -205,6 +203,7 @@ git_commit_prompt() { # Prompt for commit message, then commit all modified and untracked files. git_commit_all() { + fail_if_not_git_repo || return 1 changes=$(git status --porcelain | wc -l) if [ "$changes" -gt 0 ]; then echo -e "\e[0;33mCommitting all files (\e[0;31m$changes\e[0;33m)\e[0m" @@ -216,6 +215,7 @@ git_commit_all() { # Add paths or expanded args if any given, then commit all staged changes. git_add_and_commit() { + fail_if_not_git_repo || return 1 git_silent_add_shortcuts "$@" changes=$(git diff --cached --numstat | wc -l) if [ "$changes" -gt 0 ]; then