From 0cee2564e94c80f6dca5e0da58df7f6c028b9d3c Mon Sep 17 00:00:00 2001
From: accowa <acc@noc.ac.uk>
Date: Mon, 10 Jan 2022 16:05:57 +0000
Subject: [PATCH] Branch dev_2022_sette_updates. Add support for running SETTE
 on detached HEADS. This occurs when older commits are checked out for
 testing. A detached HEAD is not associated with a branch but these changes
 will, at least, recover an appropriate date for the commit being tested
 rather than referring to the current origin.

---
 sette/all_functions.sh |  6 +++++-
 sette/sette.sh         | 15 +++++++++++++++
 sette/sette_eval.sh    | 24 ++++++++++++++++++++++--
 sette/sette_rpt.sh     | 24 ++++++++++++++++++++++--
 4 files changed, 64 insertions(+), 5 deletions(-)

diff --git a/sette/all_functions.sh b/sette/all_functions.sh
index e8bc906c..326b38d1 100755
--- a/sette/all_functions.sh
+++ b/sette/all_functions.sh
@@ -171,7 +171,11 @@ clean_config() {
 
 # define validation dir
 set_valid_dir () {
-    REVISION_NB=`git -C ${MAIN_DIR} rev-list --abbrev-commit origin | head -1l`
+    if [ ${DETACHED_HEAD} == "no" ] ; then
+      REVISION_NB=`git -C ${MAIN_DIR} rev-list --abbrev-commit origin | head -1l`
+    else
+      REVISION_NB=${DETACHED_CMIT}
+    fi
     REV_DATE0="`git -C ${MAIN_DIR} log -1 | grep Date | sed -e 's/.*Date: *//' -e's/ +.*$//'`"
     REV_DATE=`${DATE_CONV}"${REV_DATE0}" +"%y%j"`
     REVISION_NB=${REV_DATE}_${REVISION_NB}
diff --git a/sette/sette.sh b/sette/sette.sh
index 8724abd3..69aee970 100755
--- a/sette/sette.sh
+++ b/sette/sette.sh
@@ -36,10 +36,25 @@ export USER_INPUT='yes'        # Default: yes => request user input on decisions
                                #                 3. regarding creation of directories
 #
 # Check that git branch is usable
+export DETACHED_HEAD="no"
 git branch --show-current >& /dev/null
 if [[ $? == 0 ]] ; then
   # subdirectory below NEMO_VALIDATION_DIR defaults to branchname
   export SETTE_SUB_VAL="$(git branch --show-current)"
+  if [ -z $SETTE_SUB_VAL ] ; then
+   # Probabably on a detached HEAD (possibly testing an old commit).
+   # Verify this and try to recover original commit
+   MORE_INFO="$(git branch -a | head -1l | sed -e's/.*(//' -e 's/)//' )"
+   if [[ "${MORE_INFO}" == *"detached"* ]] ; then
+     export DETACHED_HEAD="yes"
+     export DETACHED_CMIT=$( echo \\${MORE_INFO} | awk '{print $NF}' )
+     # There is no robust way to recover a branch name in a detached state
+     # so just use the commit with a prefix
+     export SETTE_SUB_VAL="detached_"${DETACHED_CMIT}
+   else
+     export SETTE_SUB_VAL="Unknown"
+   fi
+  fi
   export SETTE_THIS_BRANCH=${SETTE_SUB_VAL}
 else
   # subdirectory below NEMO_VALIDATION_DIR defaults to "MAIN"
diff --git a/sette/sette_eval.sh b/sette/sette_eval.sh
index 93d70715..cfe9fe00 100755
--- a/sette/sette_eval.sh
+++ b/sette/sette_eval.sh
@@ -255,11 +255,31 @@ function runcmpres(){
 #
 if [ ${quiet} -eq 0 ] ; then echo "" ; fi
 localchanges=`git status --short -uno | wc -l`
-revision=`git rev-list --abbrev-commit origin | head -1l`
+# Check that git branch is usable and use it to detect detached HEADs
+git branch --show-current >& /dev/null
+if [[ $? == 0 ]] ; then
+  branchname="$(git branch --show-current)"
+  if [ -z $branchname ] ; then
+   # Probabably on a detached HEAD (possibly testing an old commit).
+   # Verify this and try to recover original commit
+   MORE_INFO="$(git branch -a | head -1l | sed -e's/.*(//' -e 's/)//' )"
+   if [[ "${MORE_INFO}" == *"detached"* ]] ; then
+     revision=$( echo \\${MORE_INFO} | awk '{print $NF}' )
+     # There is no robust way to recover a branch name in a detached state
+     # so just use the commit with a prefix
+     branchname="detached_"${revision}
+   else
+     branchname="Unknown"
+   fi
+  else
+   revision=`git rev-list --abbrev-commit origin | head -1l`
+  fi
+else
+  branchname="Unknown"
+fi
 rev_date0=`git log -1 | grep Date | sed -e 's/.*Date: *//' -e's/ +.*$//'`
 rev_date=`${DATE_CONV}"${rev_date0}" +"%y%j"`
 revision=${rev_date}_${revision}
-branchname=`git branch --show-current`
 if [[ $localchanges > 0 ]] ; then
  if [ ${quiet} -eq 0 ] ; then  echo "Current code is : $branchname @ $revision  ( with local changes )" ; fi
  lastchange=${revision}+
diff --git a/sette/sette_rpt.sh b/sette/sette_rpt.sh
index 16998096..dfc9db7a 100755
--- a/sette/sette_rpt.sh
+++ b/sette/sette_rpt.sh
@@ -567,11 +567,31 @@ function identictest(){
 #
 echo ""
 localchanges=`git status --short -uno | wc -l`
-revision=`git rev-list --abbrev-commit origin | head -1l`
+# Check that git branch is usable and use it to detect detached HEADs
+git branch --show-current >& /dev/null
+if [[ $? == 0 ]] ; then
+  branchname="$(git branch --show-current)"
+  if [ -z $branchname ] ; then
+   # Probabably on a detached HEAD (possibly testing an old commit).
+   # Verify this and try to recover original commit
+   MORE_INFO="$(git branch -a | head -1l | sed -e's/.*(//' -e 's/)//' )"
+   if [[ "${MORE_INFO}" == *"detached"* ]] ; then
+     revision=$( echo \\${MORE_INFO} | awk '{print $NF}' )
+     # There is no robust way to recover a branch name in a detached state
+     # so just use the commit with a prefix
+     branchname="detached_"${revision}
+   else
+     branchname="Unknown"
+   fi
+  else
+   revision=`git rev-list --abbrev-commit origin | head -1l`
+  fi
+else
+  branchname="Unknown"
+fi
 rev_date0=`git log -1 | grep Date | sed -e 's/.*Date: *//' -e's/ +.*$//'`
 rev_date=`${DATE_CONV}"${rev_date0}" +"%y%j"`
 revision=${rev_date}_${revision}
-branchname=`git branch --show-current`
 if [[ $localchanges > 0 ]] ; then
  echo "Current code is : $branchname @ $revision  ( with local changes )"
  lastchange=${revision}+
-- 
GitLab