Skip to content

Commit

Permalink
Merge pull request #461 from gauge-sh/support-gha-checkout
Browse files Browse the repository at this point in the history
support github actions
  • Loading branch information
caelean authored Dec 9, 2024
2 parents 26dab51 + c04d2a1 commit e2e7fcc
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tach"
version = "0.16.4"
version = "0.16.5"
edition = "2021"

[lib]
Expand Down
2 changes: 1 addition & 1 deletion docs/usage/commands.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "tach"
version = "0.16.4"
version = "0.16.5"
authors = [
{ name = "Caelean Barnes", email = "[email protected]" },
{ name = "Evan Doyle", email = "[email protected]" },
Expand Down
2 changes: 1 addition & 1 deletion python/tach/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations

__version__: str = "0.16.4"
__version__: str = "0.16.5"

__all__ = ["__version__"]
25 changes: 24 additions & 1 deletion python/tach/filesystem/git_ops.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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:
Expand All @@ -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}")
Expand Down

0 comments on commit e2e7fcc

Please sign in to comment.