From f8509381429ce67104107842285408775d6fc21b Mon Sep 17 00:00:00 2001
From: Jan David Mol <mol@astron.nl>
Date: Mon, 5 Jul 2021 14:02:59 +0200
Subject: [PATCH] L2SS-289: Fix lofar_logging for use on detached head, and to
 allow detection if its own git repo if run as a direct command.

---
 devices/common/lofar_git.py | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/devices/common/lofar_git.py b/devices/common/lofar_git.py
index 353748d98..f4f621728 100644
--- a/devices/common/lofar_git.py
+++ b/devices/common/lofar_git.py
@@ -2,7 +2,7 @@ import git # pip3 install gitpython
 import os
 from functools import lru_cache
 
-def get_repo(starting_directory: str = os.path.dirname(__file__)) -> git.Repo:
+def get_repo(starting_directory: str = os.path.dirname(os.path.abspath(__file__))) -> git.Repo:
     """ Try finding the repository by traversing up the tree.
 
         By default, the repository containing this module is returned.
@@ -50,14 +50,20 @@ def get_version(repo: git.Repo = None) -> str:
     if repo is None:
         repo = get_repo()
 
-    branch = repo.active_branch
     commit = repo.commit()
     tags = {tag.commit: tag for tag in repo.tags}
 
     if commit in tags:
+        # a tag = production ready
         commit_str = "{}".format(tags[commit])
         production_ready = True
+    elif repo.head.is_detached:
+        # no active branch
+        commit_str = "<detached HEAD> [{}]".format(commit)
+        production_ready = False
     else:
+        # HEAD of a branch
+        branch = repo.active_branch
         commit_str = "{} [{}]".format(branch, commit)
         production_ready = False
 
-- 
GitLab