## Generally have fun
# Don't do anything on non-topgit branch
-git rev-parse --verify "$(git symbolic-ref HEAD | sed 's/heads/top-bases/')" >/dev/null 2>&1 ||
- exit 0
+if head_=$(git symbolic-ref -q HEAD); then
+ case "$head_" in
+ refs/heads/*)
+ git rev-parse -q --verify "refs/top-bases${head_#refs/heads}" >/dev/null || exit 0;;
+ *)
+ exit 0;;
+ esac
-[ -s "$root_dir/.topdeps" ] ||
- die ".topdeps is missing"
-[ -s "$root_dir/.topmsg" ] ||
- die ".topmsg is missing"
+else
+ exit 0;
+fi
+
+check_topfile()
+{
+ local tree file ls_line type size
+ tree=$1
+ file=$2
+
+ ls_line="$(git ls-tree --long "$tree" "$file")" ||
+ die "Can't ls tree for $file"
+
+ [ -n "$ls_line" ] ||
+ die "$file is missing"
+
+ # check for type and size
+ set -- $ls_line
+ type=$2
+ size=$4
+
+ # check file is of type blob (file)
+ [ "x$type" = "xblob" ] ||
+ die "$file is not a file"
+
+ # check for positive size
+ [ "$size" -gt 0 ] ||
+ die "$file has empty size"
+}
+
+tree=$(git write-tree) ||
+ die "Can't write tree"
+
+check_topfile "$tree" ".topdeps"
+check_topfile "$tree" ".topmsg"
# TODO: Verify .topdeps for valid branch names and against cycles