Skip to content

Commit text

commit_text

Text renderers for GitCommitTool dual-format ToolResult.

These helpers transform the structured data dict produced by :class:axm_git.tools.commit.GitCommitTool into a compact, token-efficient text representation suitable for MCP consumers.

The header pattern follows the AXM convention (see axm_ast.tools.search_text)::

Text Only
git_commit | {status} | {succeeded}/{total} commits [· {extra}]

format_commit_line(result)

Format one successful commit record as {sha} [↻ ]{message}.

Source code in packages/axm-git/src/axm_git/tools/commit_text.py
Python
def format_commit_line(result: dict[str, object]) -> str:
    """Format one successful commit record as ``{sha} [↻ ]{message}``."""
    sha = _as_str(result.get("sha"))
    message = _as_str(result.get("message"))
    if result.get("retried"):
        return f"{sha}{message}"
    return f"{sha} {message}"

format_text_header(*, status, succeeded, total, retried_count=0, extra=None)

Build the header line for a git_commit text rendering.

When total is 0 and extra is given, emit the pure-error shape git_commit | error: {extra} (used for early failures with no commit list available).

Source code in packages/axm-git/src/axm_git/tools/commit_text.py
Python
def format_text_header(
    *,
    status: str,
    succeeded: int,
    total: int,
    retried_count: int = 0,
    extra: str | None = None,
) -> str:
    """Build the header line for a ``git_commit`` text rendering.

    When *total* is 0 and *extra* is given, emit the pure-error shape
    ``git_commit | error: {extra}`` (used for early failures with no
    commit list available).
    """
    if total == 0 and extra is not None:
        return f"git_commit | error: {extra}"
    sections = ["git_commit", status, f"{succeeded}/{total} commits"]
    header = " | ".join(sections)
    if retried_count > 0:
        header += f" · {retried_count} retried"
    if extra:
        header += f" · {extra}"
    return header

render_failure_text(*, error, data)

Render the failure-path text representation.

Branches:

  • data is None → plain git_commit | error: {error} line.
  • data has suggestions → M4 not-a-repo hint.
  • data has failed_commit → M7 pre-commit failure with optional auto-fixed list and indented hook output.
  • otherwise → M5/M6 validation or git-add failure.
Source code in packages/axm-git/src/axm_git/tools/commit_text.py
Python
def render_failure_text(
    *,
    error: str,
    data: dict[str, object] | None,
) -> str:
    """Render the failure-path text representation.

    Branches:

    - *data* is ``None``  → plain ``git_commit | error: {error}`` line.
    - *data* has ``suggestions``  → M4 not-a-repo hint.
    - *data* has ``failed_commit``  → M7 pre-commit failure with optional
      auto-fixed list and indented hook output.
    - otherwise  → M5/M6 validation or git-add failure.
    """
    if data is None:
        msg = error.lower() if error.startswith("No commits") else error
        return f"git_commit | error: {msg}"
    suggestions = _as_str_list(data.get("suggestions"))
    if suggestions:
        return _render_suggestions(error=error, suggestions=suggestions)
    failed = data.get("failed_commit")
    if isinstance(failed, dict):
        return _render_failed_commit(data=data, failed=failed)
    return _render_validation(error=error, data=data)

render_text(data)

Render the success-path data dict.

Header + one format_commit_line per entry in data['results'].

Source code in packages/axm-git/src/axm_git/tools/commit_text.py
Python
def render_text(data: dict[str, object]) -> str:
    """Render the success-path ``data`` dict.

    Header + one ``format_commit_line`` per entry in ``data['results']``.
    """
    results = _as_results(data.get("results"))
    total = _as_int(data.get("total"), len(results))
    succeeded = _as_int(data.get("succeeded"), len(results))
    header = format_text_header(
        status="ok",
        succeeded=succeeded,
        total=total,
        retried_count=_retried_count(results),
    )
    if not results:
        return header
    lines = [header, *(format_commit_line(r) for r in results)]
    return "\n".join(lines)