From 9fcb6f3e20bd92936238e7aa18e58191ea214b09 Mon Sep 17 00:00:00 2001 From: David Hall Date: Wed, 14 Feb 2024 15:30:22 -0800 Subject: [PATCH 1/2] 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 From 72f0d5baccd39268761f02a2b80207e835428deb Mon Sep 17 00:00:00 2001 From: David Hall Date: Thu, 15 Feb 2024 13:09:59 -0800 Subject: [PATCH 2/2] don't try to log sha if we can't get it --- src/levanter/tracker/wandb.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/levanter/tracker/wandb.py b/src/levanter/tracker/wandb.py index 99d851ec7..98e462f8e 100644 --- a/src/levanter/tracker/wandb.py +++ b/src/levanter/tracker/wandb.py @@ -190,7 +190,9 @@ 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 - other_settings["git_commit"] = self._get_git_sha(code_dir) + sha = self._get_git_sha(code_dir) + if sha is not None: + other_settings["git_commit"] = sha return other_settings @@ -208,7 +210,9 @@ def _get_git_sha(self, code_dir) -> Optional[str]: import subprocess try: - out = subprocess.run(["git", "--git-dir", f"{code_dir}/.git", "rev-parse", "HEAD"], check=True) + out = subprocess.run( + ["git", "--git-dir", f"{code_dir}/.git", "rev-parse", "HEAD"], check=True, capture_output=True + ) git_sha = out.stdout.decode().strip() except subprocess.CalledProcessError: return None