Skip to content

Push

push

Push hook action.

Pushes the current branch to origin with upstream tracking.

PushHook dataclass

Push the current branch to origin -u.

Reads branch from context (or detects it from HEAD). Skips gracefully when the working directory is not a git repository.

Source code in packages/axm-git/src/axm_git/hooks/push.py
@dataclass
class PushHook:
    """Push the current branch to ``origin -u``.

    Reads ``branch`` from *context* (or detects it from HEAD).
    Skips gracefully when the working directory is not a git repository.
    """

    def execute(self, context: dict[str, Any], **params: Any) -> HookResult:
        """Execute the hook action.

        Args:
            context: Session context dictionary.
            **params: Optional ``enabled`` (default ``True``).

        Returns:
            HookResult with ``pushed`` and ``branch`` in metadata.
        """
        if not params.get("enabled", True):
            return HookResult.ok(skipped=True, reason="git disabled")

        working_dir = _resolve_working_dir(params, context)

        git_root = find_git_root(working_dir)
        if git_root is None:
            return HookResult.ok(skipped=True, reason="not a git repo")

        branch = context.get("branch")
        if not branch:
            head = run_git(["rev-parse", "--abbrev-ref", "HEAD"], git_root)
            if head.returncode != 0:
                return HookResult.fail(f"failed to detect branch: {head.stderr}")
            branch = head.stdout.strip()

        result = run_git(["push", "-u", "origin", branch], git_root)
        if result.returncode != 0:
            stderr = result.stderr.strip()
            if "Everything up-to-date" in (result.stderr + result.stdout):
                return HookResult.ok(pushed=True, branch=branch)
            return HookResult.fail(f"git push failed: {stderr}")

        return HookResult.ok(pushed=True, branch=branch)
execute(context, **params)

Execute the hook action.

Parameters:

Name Type Description Default
context dict[str, Any]

Session context dictionary.

required
**params Any

Optional enabled (default True).

{}

Returns:

Type Description
HookResult

HookResult with pushed and branch in metadata.

Source code in packages/axm-git/src/axm_git/hooks/push.py
def execute(self, context: dict[str, Any], **params: Any) -> HookResult:
    """Execute the hook action.

    Args:
        context: Session context dictionary.
        **params: Optional ``enabled`` (default ``True``).

    Returns:
        HookResult with ``pushed`` and ``branch`` in metadata.
    """
    if not params.get("enabled", True):
        return HookResult.ok(skipped=True, reason="git disabled")

    working_dir = _resolve_working_dir(params, context)

    git_root = find_git_root(working_dir)
    if git_root is None:
        return HookResult.ok(skipped=True, reason="not a git repo")

    branch = context.get("branch")
    if not branch:
        head = run_git(["rev-parse", "--abbrev-ref", "HEAD"], git_root)
        if head.returncode != 0:
            return HookResult.fail(f"failed to detect branch: {head.stderr}")
        branch = head.stdout.strip()

    result = run_git(["push", "-u", "origin", branch], git_root)
    if result.returncode != 0:
        stderr = result.stderr.strip()
        if "Everything up-to-date" in (result.stderr + result.stdout):
            return HookResult.ok(pushed=True, branch=branch)
        return HookResult.fail(f"git push failed: {stderr}")

    return HookResult.ok(pushed=True, branch=branch)