From 0f2ee1cb405df27ca3d71d6794e16e617187e97d Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Mon, 3 Aug 2020 18:04:52 +0300 Subject: [PATCH] Feat: Fix detached `HEAD` Fix detached `HEAD` by checking out the branch (local or remote, if one exists) that points to the same commit. --- detached-head/fix-detached-head | 19 +++++++++++++++++++ detached-head/fix-detached-head-recursive | 4 ++++ detached-head/fix-detached-head-repos | 7 +++++++ 3 files changed, 30 insertions(+) create mode 100755 detached-head/fix-detached-head create mode 100755 detached-head/fix-detached-head-recursive create mode 100755 detached-head/fix-detached-head-repos diff --git a/detached-head/fix-detached-head b/detached-head/fix-detached-head new file mode 100755 index 0000000..7be050a --- /dev/null +++ b/detached-head/fix-detached-head @@ -0,0 +1,19 @@ +#! /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 diff --git a/detached-head/fix-detached-head-recursive b/detached-head/fix-detached-head-recursive new file mode 100755 index 0000000..dbc501d --- /dev/null +++ b/detached-head/fix-detached-head-recursive @@ -0,0 +1,4 @@ +#! /bin/sh + +prog_dir="`dirname \"$0\"`" && +exec "$prog_dir"/../run-recursive "$prog_dir"/fix-detached-head diff --git a/detached-head/fix-detached-head-repos b/detached-head/fix-detached-head-repos new file mode 100755 index 0000000..f76c85d --- /dev/null +++ b/detached-head/fix-detached-head-repos @@ -0,0 +1,7 @@ +#! /bin/sh + +cd "`dirname \"$0\"`" && +prog_dir="`pwd`" && +export prog_dir + +exec "$prog_dir"/../run-repos "$prog_dir"/fix-detached-head-recursive -- 2.39.5