From 9fcb6f3e20bd92936238e7aa18e58191ea214b09 Mon Sep 17 00:00:00 2001 From: David Hall Date: Wed, 14 Feb 2024 15:30:22 -0800 Subject: [PATCH] attempt to work around weird dubious ownership thing --- src/levanter/tracker/wandb.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/levanter/tracker/wandb.py b/src/levanter/tracker/wandb.py index d217ab000..99d851ec7 100644 --- a/src/levanter/tracker/wandb.py +++ b/src/levanter/tracker/wandb.py @@ -190,10 +190,29 @@ def _git_settings(self): other_settings["code_dir"] = code_dir other_settings["git_root"] = code_dir # for some reason, wandb isn't populating the git commit, so we do it here - try: - repo = Repo(code_dir) - other_settings["git_commit"] = repo.head.commit.hexsha - except (NoSuchPathError, InvalidGitRepositoryError): - logger.warning(f"Could not find git repo at {code_dir}") - pass + other_settings["git_commit"] = self._get_git_sha(code_dir) + return other_settings + + def _get_git_sha(self, code_dir) -> Optional[str]: + try: + repo = Repo(code_dir) + git_sha = repo.head.commit.hexsha + except (NoSuchPathError, InvalidGitRepositoryError): + logger.warning(f"Could not find git repo at {code_dir}") + return None + except ValueError as e: + if "SHA is empty" in str(e): + # we have another workaround, which is to use the git command line + # git --git-dir={code_dir}/.git rev-parse HEAD + import subprocess + + try: + out = subprocess.run(["git", "--git-dir", f"{code_dir}/.git", "rev-parse", "HEAD"], check=True) + git_sha = out.stdout.decode().strip() + except subprocess.CalledProcessError: + return None + else: + raise e + + return git_sha