Fix detached `HEAD` by checking out the branch
(local or remote, if one exists) that points to the same commit.
--- /dev/null
+#! /usr/bin/env bash
+
+git symbolic-ref -q HEAD >/dev/null && exit
+
+pwd
+
+declare -a branches
+branches=(`git branch --points-at=HEAD | tail -n +2`)
+if [ "${#branches[*]}" -eq 0 ]; then
+ branches=(`git branch --points-at=HEAD -r | sed 's!^ *origin/!!'`)
+fi
+
+if [ "${#branches[*]}" -eq 0 ]; then
+ echo "Cannot find a branch" >&2
+elif [ "${#branches[*]}" -eq 1 ]; then
+ git checkout "${branches[0]}"
+else
+ echo "Too many branches" >&2
+fi
--- /dev/null
+#! /bin/sh
+
+prog_dir="`dirname \"$0\"`" &&
+exec "$prog_dir"/../run-recursive "$prog_dir"/fix-detached-head
--- /dev/null
+#! /bin/sh
+
+cd "`dirname \"$0\"`" &&
+prog_dir="`pwd`" &&
+export prog_dir
+
+exec "$prog_dir"/../run-repos "$prog_dir"/fix-detached-head-recursive