diff --git a/Cargo.lock b/Cargo.lock index ddade593..1b247564 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1617,7 +1617,7 @@ dependencies = [ [[package]] name = "tach" -version = "0.16.4" +version = "0.16.5" dependencies = [ "cached", "criterion", diff --git a/Cargo.toml b/Cargo.toml index f22bfce8..dcf32b92 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tach" -version = "0.16.4" +version = "0.16.5" edition = "2021" [lib] diff --git a/docs/usage/commands.mdx b/docs/usage/commands.mdx index 3deaa8af..0529d090 100644 --- a/docs/usage/commands.mdx +++ b/docs/usage/commands.mdx @@ -255,7 +255,7 @@ If you use the [pre-commit framework](https://github.com/pre-commit/pre-commit), ```yaml repos: - repo: https://github.com/gauge-sh/tach-pre-commit - rev: v0.16.4 # change this to the latest tag! + rev: v0.16.5 # change this to the latest tag! hooks: - id: tach ``` diff --git a/pyproject.toml b/pyproject.toml index 267788df..da2341d2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "tach" -version = "0.16.4" +version = "0.16.5" authors = [ { name = "Caelean Barnes", email = "caeleanb@gmail.com" }, { name = "Evan Doyle", email = "evanmdoyle@gmail.com" }, diff --git a/python/tach/__init__.py b/python/tach/__init__.py index 9c8f804c..ace3e8d9 100644 --- a/python/tach/__init__.py +++ b/python/tach/__init__.py @@ -1,5 +1,5 @@ from __future__ import annotations -__version__: str = "0.16.4" +__version__: str = "0.16.5" __all__ = ["__version__"] diff --git a/python/tach/filesystem/git_ops.py b/python/tach/filesystem/git_ops.py index 4b44f675..de5ac8af 100644 --- a/python/tach/filesystem/git_ops.py +++ b/python/tach/filesystem/git_ops.py @@ -1,10 +1,15 @@ from __future__ import annotations +import os from dataclasses import dataclass from pathlib import Path +from typing import TYPE_CHECKING from tach.errors import TachError, TachSetupError +if TYPE_CHECKING: + from git import Repo + @dataclass class GitBranchInfo: @@ -13,6 +18,24 @@ class GitBranchInfo: commit: str +def is_github_actions(): + return os.environ.get("GITHUB_ACTIONS") == "true" + + +def _get_branch_name(repo: Repo) -> str: + try: + repo_name = repo.active_branch.name + except TypeError as e: + # GHA uses a detached HEAD / shallow clone in actions/checkout@v4, get the branch name from env + if is_github_actions(): + repo_name = os.environ.get("GITHUB_HEAD_REF") + if not repo_name: + raise e + else: + raise e + return repo_name + + def get_current_branch_info( project_root: Path, allow_dirty: bool = False ) -> GitBranchInfo: @@ -33,7 +56,7 @@ def get_current_branch_info( try: # TODO: support slashes or org names repo_name = repo.remotes.origin.url.split("/")[-1].replace(".git", "") - branch = repo.active_branch.name + branch = _get_branch_name(repo) commit = repo.head.commit.hexsha except Exception as e: raise TachError(f"Failed to determine current branch information!\nError: {e}")