chiark / gitweb /
Merge branch 'i/build' into refs/top-bases/i/basis-merge
[topgit.git] / tg.sh
diff --git a/tg.sh b/tg.sh
index 9082d88910569af146dde388332bedd0b555ffc1..838dc49185be1fa81b288830ca950725d469ae10 100644 (file)
--- a/tg.sh
+++ b/tg.sh
@@ -18,6 +18,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
+}
+
 # cat_file TOPIC:PATH [FROM]
 # cat the file PATH from branch TOPIC when FROM is empty.
 # FROM can be -i or -w, than the file will be from the index or worktree,
@@ -364,8 +378,8 @@ do_help()
                setup_pager
                @cmddir@/tg-$1 -h 2>&1 || :
                echo
-               if [ -r "@sharedir@/tg-$1.txt" ] ; then
-                       cat "@sharedir@/tg-$1.txt"
+               if [ -r "@docdir@/tg-$1.txt" ] ; then
+                       cat "@docdir@/tg-$1.txt"
                fi
        else
                echo "`basename $0`: no help for $1" 1>&2
@@ -374,6 +388,29 @@ do_help()
        fi
 }
 
+# Check whether we are supposed to output the help message
+should_do_help()
+{
+       # we are being sourced for utility functions, never run help
+       [ -z "$tg__include" ] || return 1
+
+       local prev
+       while [ -n "$1" ]; do
+               case "$1" in
+               help|--help|-h)
+                       shift
+                       echo "${1:-$prev}"
+                       return 0
+               esac
+               prev="$1"
+               shift
+       done
+
+       # run help when there was no previous topic, meaning that there where
+       # no arguments at all
+       test -z "$prev"
+}
+
 ## Pager stuff
 
 # isatty FD
@@ -427,6 +464,11 @@ get_temp()
 [ -d "@cmddir@" ] ||
        die "No command directory: '@cmddir@'"
 
+# check if we should run help and get the topic while we're at it
+help_topic="$(should_do_help "$@")" && { do_help "$help_topic"; exit 0; }
+
+ensure_git_repo_or_die
+
 ## Initial setup
 
 set -e
@@ -466,9 +508,6 @@ cmd="$1"
 shift
 
 case "$cmd" in
-help|--help|-h)
-       do_help "$1"
-       exit 0;;
 --hooks-path)
        # Internal command
        echo "@hooksdir@";;