From aaa571c2bfd6922bca4def71ea091294950ffa75 Mon Sep 17 00:00:00 2001 From: clearml <> Date: Fri, 14 Feb 2025 11:09:56 +0200 Subject: [PATCH] Fix detecting git branch in detached HEAD state --- .../backend_interface/task/repo/detectors.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/clearml/backend_interface/task/repo/detectors.py b/clearml/backend_interface/task/repo/detectors.py index f54e0343..c5576f23 100644 --- a/clearml/backend_interface/task/repo/detectors.py +++ b/clearml/backend_interface/task/repo/detectors.py @@ -251,6 +251,23 @@ class GitDetector(Detector): """ info = super(GitDetector, self).get_info( path=path, include_diff=include_diff, diff_from_remote=diff_from_remote) + + # check if for some reason we ended with the wrong branch name as HEAD (will happen when HEAD is detached) + if info and info.branch == "HEAD": + # noinspection PyBroadException + try: + # try to reparse the detached HEAD + commands = self._get_commands() + name = "branch" + command = commands.branch_fallback[:] + command[-1] = "origin" + info.branch = self._get_command_output( + path, name, command, commands=commands, strip=bool(name != 'diff')) or info.branch + info = self._post_process_info(info) + except Exception: + # not sure what happened, just skip it. + pass + # we could not locate the git because of some configuration issue, we need to try a more complex command if info and info.url == "origin": # noinspection PyBroadException