From 102da8bea01f787f40eadd1817d0ae93d0371c86 Mon Sep 17 00:00:00 2001 From: Sorin Ionescu Date: Sun, 30 Sep 2012 15:30:18 -0400 Subject: [PATCH] Make sure that the current directory is a Git repository --- modules/git/functions/_git-hub-browse | 4 ++++ modules/git/functions/_git-info | 4 ++++ modules/git/functions/git-branch-current | 6 ++++++ modules/git/functions/git-commit-lost | 5 +++++ modules/git/functions/git-dir | 1 + modules/git/functions/git-hub-browse | 5 +++++ modules/git/functions/git-root | 1 + modules/git/functions/git-stash-clear-interactive | 5 +++++ modules/git/functions/git-stash-dropped | 5 +++++ modules/git/functions/git-stash-recover | 5 +++++ modules/git/functions/git-submodule-move | 13 ++++++++----- modules/git/functions/git-submodule-remove | 11 ++++++----- 12 files changed, 55 insertions(+), 10 deletions(-) diff --git a/modules/git/functions/_git-hub-browse b/modules/git/functions/_git-hub-browse index e3c41d8..0085c18 100644 --- a/modules/git/functions/_git-hub-browse +++ b/modules/git/functions/_git-hub-browse @@ -8,6 +8,10 @@ # Sorin Ionescu # +if ! is-true "$(git rev-parse --is-inside-work-tree 2> /dev/null)"; then + return 1 +fi + local state expl remotes remote branches_or_tags branches tags files ret=1 _arguments -C -s -S \ diff --git a/modules/git/functions/_git-info b/modules/git/functions/_git-info index 0357c6d..c1467e8 100644 --- a/modules/git/functions/_git-info +++ b/modules/git/functions/_git-info @@ -8,6 +8,10 @@ # Sorin Ionescu # +if ! is-true "$(git rev-parse --is-inside-work-tree 2> /dev/null)"; then + return 1 +fi + _arguments "1:toggle:(( on\:'enable in-prompt information for the current repository' off\:'disable in-prompt information for the current repository' diff --git a/modules/git/functions/git-branch-current b/modules/git/functions/git-branch-current index 3078ad1..c731958 100644 --- a/modules/git/functions/git-branch-current +++ b/modules/git/functions/git-branch-current @@ -5,7 +5,13 @@ # Sorin Ionescu # +if ! git rev-parse 2> /dev/null; then + print "$0: not a repository: $PWD" >&2 + return 1 +fi + local ref="$(git symbolic-ref HEAD 2> /dev/null)" + if [[ -n "$ref" ]]; then print "${ref#refs/heads/}" return 0 diff --git a/modules/git/functions/git-commit-lost b/modules/git/functions/git-commit-lost index ed9b076..0984ab9 100644 --- a/modules/git/functions/git-commit-lost +++ b/modules/git/functions/git-commit-lost @@ -5,6 +5,11 @@ # Sorin Ionescu # +if ! is-true "$(git rev-parse --is-inside-work-tree 2> /dev/null)"; then + print "$0: not a repository work tree: $PWD" >&2 + return 1 +fi + git fsck 2> /dev/null \ | grep "^dangling commit" \ | awk '{print $3}' \ diff --git a/modules/git/functions/git-dir b/modules/git/functions/git-dir index e096752..79b36d7 100644 --- a/modules/git/functions/git-dir +++ b/modules/git/functions/git-dir @@ -11,6 +11,7 @@ if [[ -n "$git_dir" ]]; then print "$git_dir" return 0 else + print "$0: not a repository: $PWD" >&2 return 1 fi diff --git a/modules/git/functions/git-hub-browse b/modules/git/functions/git-hub-browse index ee4f9f6..80fddca 100644 --- a/modules/git/functions/git-hub-browse +++ b/modules/git/functions/git-hub-browse @@ -5,6 +5,11 @@ # Sorin Ionescu # +if ! is-true "$(git rev-parse --is-inside-work-tree 2> /dev/null)"; then + print "$0: not a repository work tree: $PWD" >&2 + return 1 +fi + local remotes remote references reference file url remote="${1:-origin}" diff --git a/modules/git/functions/git-root b/modules/git/functions/git-root index c5aa48f..6ba52ef 100644 --- a/modules/git/functions/git-root +++ b/modules/git/functions/git-root @@ -11,6 +11,7 @@ if [[ -n "$root" ]]; then print "$root" return 0 else + print "$0: not a repository work tree: $PWD" >&2 return 1 fi diff --git a/modules/git/functions/git-stash-clear-interactive b/modules/git/functions/git-stash-clear-interactive index 82d055c..effd038 100644 --- a/modules/git/functions/git-stash-clear-interactive +++ b/modules/git/functions/git-stash-clear-interactive @@ -5,6 +5,11 @@ # Sorin Ionescu # +if ! is-true "$(git rev-parse --is-inside-work-tree 2> /dev/null)"; then + print "$0: not a repository work tree: $PWD" >&2 + return 1 +fi + local stashed if [[ -f "$(git-dir)/refs/stash" ]]; then diff --git a/modules/git/functions/git-stash-dropped b/modules/git/functions/git-stash-dropped index ce3a52b..9867ad0 100644 --- a/modules/git/functions/git-stash-dropped +++ b/modules/git/functions/git-stash-dropped @@ -5,6 +5,11 @@ # Sorin Ionescu # +if ! is-true "$(git rev-parse --is-inside-work-tree 2> /dev/null)"; then + print "$0: not a repository work tree: $PWD" >&2 + return 1 +fi + git fsck --unreachable 2> /dev/null \ | grep 'commit' \ | awk '{print $3}' \ diff --git a/modules/git/functions/git-stash-recover b/modules/git/functions/git-stash-recover index dd50a1b..0709440 100644 --- a/modules/git/functions/git-stash-recover +++ b/modules/git/functions/git-stash-recover @@ -5,6 +5,11 @@ # Sorin Ionescu # +if ! is-true "$(git rev-parse --is-inside-work-tree 2> /dev/null)"; then + print "$0: not a repository work tree: $PWD" >&2 + return 1 +fi + local commit for commit in "$@"; do diff --git a/modules/git/functions/git-submodule-move b/modules/git/functions/git-submodule-move index 4c94ed2..c3db972 100644 --- a/modules/git/functions/git-submodule-move +++ b/modules/git/functions/git-submodule-move @@ -5,15 +5,18 @@ # Sorin Ionescu # +if ! is-true "$(git rev-parse --is-inside-work-tree 2> /dev/null)"; then + print "$0: not a repository work tree: $PWD" >&2 + return 1 +elif [[ "$PWD" != "$(git-root)" ]]; then + print "$0: must be run from the root of the work tree" >&2 + return 1 +fi + local src="$1" local dst="$2" local url -if [[ "$PWD" != "$(git-root)" ]]; then - print "$0: must be run from the root of the working tree" >&2 - return 1 -fi - url="$(git config --file "$(git-root)/.gitmodules" --get "submodule.${src}.url")" if [[ -z "$url" ]]; then diff --git a/modules/git/functions/git-submodule-remove b/modules/git/functions/git-submodule-remove index 26fca23..3f72fb6 100644 --- a/modules/git/functions/git-submodule-remove +++ b/modules/git/functions/git-submodule-remove @@ -5,12 +5,13 @@ # Sorin Ionescu # -if [[ "$PWD" != "$(git-root)" ]]; then - print "$0: must be run from the root of the working tree" >&2 +if ! is-true "$(git rev-parse --is-inside-work-tree 2> /dev/null)"; then + print "$0: not a repository work tree: $PWD" >&2 return 1 -fi - -if ! git config --file .gitmodules --get "submodule.${1}.path" &>/dev/null; then +elif [[ "$PWD" != "$(git-root)" ]]; then + print "$0: must be run from the root of the work tree" >&2 + return 1 +elif ! git config --file .gitmodules --get "submodule.${1}.path" &>/dev/null; then print "$0: submodule not found: $1" >&2 return 1 fi