Created fail_if_not_git_repo function and prepended it to all git functions

This commit is contained in:
Nathan Broadbent
2012-09-19 21:34:03 +12:00
parent 197dcfc74f
commit 384f2a0668
3 changed files with 14 additions and 5 deletions

View File

@@ -12,6 +12,7 @@
# Adds numbered shortcuts to output of ls -l, just like 'git status' # 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 unalias $git_branch_alias > /dev/null 2>&1; unset -f $git_branch_alias > /dev/null 2>&1
function _scmb_git_branch_shortcuts { function _scmb_git_branch_shortcuts {
fail_if_not_git_repo || return 1
# Fall back to normal git branch, if any unknown args given # Fall back to normal git branch, if any unknown args given
if [[ -n "$@" ]] && [[ "$@" != "-a" ]]; then if [[ -n "$@" ]] && [[ "$@" != "-a" ]]; then
$_git_cmd branch "$@" $_git_cmd branch "$@"

View File

@@ -5,4 +5,12 @@ function find_in_cwd_or_parent() {
directory="$directory/.." directory="$directory/.."
done done
return 1 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
} }

View File

@@ -17,11 +17,7 @@
# 1 || staged, 2 || unmerged, 3 || unstaged, 4 || untracked # 1 || staged, 2 || unmerged, 3 || unstaged, 4 || untracked
# -------------------------------------------------------------------- # --------------------------------------------------------------------
git_status_shortcuts() { git_status_shortcuts() {
# Fail if not a git repo fail_if_not_git_repo || return 1
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 zsh_compat # Ensure shwordsplit is on for zsh
git_clear_vars git_clear_vars
# Run ruby script, store output # Run ruby script, store output
@@ -64,6 +60,7 @@ git_status_shortcuts() {
# - 'auto git rm' behaviour can be turned off # - 'auto git rm' behaviour can be turned off
# ------------------------------------------------------------------------------- # -------------------------------------------------------------------------------
git_add_shortcuts() { git_add_shortcuts() {
fail_if_not_git_repo || return 1
if [ -z "$1" ]; then if [ -z "$1" ]; then
echo "Usage: ga <file> => git add <file>" echo "Usage: ga <file> => git add <file>"
echo " ga 1 => git add \$e1" 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, # Prints a list of all files affected by a given SHA1,
# and exports numbered environment variables for each file. # and exports numbered environment variables for each file.
git_show_affected_files(){ git_show_affected_files(){
fail_if_not_git_repo || return 1
f=0 # File count f=0 # File count
# Show colored revision and commit message # Show colored revision and commit message
echo -n "# "; git show --oneline --name-only $@ | head -n1; echo "# " 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. # Prompt for commit message, then commit all modified and untracked files.
git_commit_all() { git_commit_all() {
fail_if_not_git_repo || return 1
changes=$(git status --porcelain | wc -l) changes=$(git status --porcelain | wc -l)
if [ "$changes" -gt 0 ]; then if [ "$changes" -gt 0 ]; then
echo -e "\e[0;33mCommitting all files (\e[0;31m$changes\e[0;33m)\e[0m" 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. # Add paths or expanded args if any given, then commit all staged changes.
git_add_and_commit() { git_add_and_commit() {
fail_if_not_git_repo || return 1
git_silent_add_shortcuts "$@" git_silent_add_shortcuts "$@"
changes=$(git diff --cached --numstat | wc -l) changes=$(git diff --cached --numstat | wc -l)
if [ "$changes" -gt 0 ]; then if [ "$changes" -gt 0 ]; then