chiark / gitweb /
factor out checks into function
[topgit.git] / tg.sh
diff --git a/tg.sh b/tg.sh
index 8c23d26b9a62ddcc1869bb70299862c32edd4403..f8c8de4ba859d76d254580ef88041b3bdce05f19 100644 (file)
--- a/tg.sh
+++ b/tg.sh
@@ -17,6 +17,20 @@ die()
        exit 1
 }
 
+# Make sure we are in the worktree, not under .git; die otherwise
+ensure_git_repo_or_die()
+{
+       local is_inside_repo is_inside_git_dir
+       is_inside_repo=1
+       is_inside_git_dir=$(git rev-parse --is-inside-git-dir 2>/dev/null) ||
+               is_inside_repo=0
+
+       case "$is_inside_repo/$is_inside_git_dir" in
+       0*) die "Cannot run outside of a Git repository.";;
+       1/true) die "Cannot run from inside \`.git\` hierarchy, please switch to work-tree.";;
+       esac
+}
+
 # setup_hook NAME
 setup_hook()
 {
@@ -249,6 +263,8 @@ do_help()
 [ -d "@cmddir@" ] ||
        die "No command directory: '@cmddir@'"
 
+ensure_git_repo_or_die
+
 ## Initial setup
 
 set -e