CoplayDev/unity-mcp · error · RuntimeError

git {args} failed

Error message

git {args} failed

What it means

Raised by run_git in mcp_source.py when a git subprocess (git -C <repo> <args...>) exits non-zero. The error message is the trimmed stderr, or the fallback 'git <args> failed' text when stderr was empty. mcp_source.py uses git to query and switch the Unity package source between branches/tags.

Source

Thrown at mcp_source.py:33

from __future__ import annotations

import argparse
import json
import pathlib
import subprocess
import sys

PKG_NAME = "com.coplaydev.unity-mcp"
BRIDGE_SUBPATH = "MCPForUnity"


def run_git(repo: pathlib.Path, *args: str) -> str:
    result = subprocess.run([
        "git", "-C", str(repo), *args
    ], capture_output=True, text=True)
    if result.returncode != 0:
        raise RuntimeError(result.stderr.strip()
                           or f"git {' '.join(args)} failed")
    return result.stdout.strip()


def normalize_origin_to_https(url: str) -> str:
    """Map common SSH origin forms to https for Unity's git URL scheme."""
    if url.startswith("git@github.com:"):
        owner_repo = url.split(":", 1)[1]
        if owner_repo.endswith(".git"):
            owner_repo = owner_repo[:-4]
        return f"https://github.com/{owner_repo}.git"
    # already https or file: etc.
    return url


def detect_repo_root(explicit: str | None) -> pathlib.Path:
    if explicit:
        return pathlib.Path(explicit).resolve()

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Run the same git command manually (git -C <repo> <args>) to read the real stderr and fix the underlying cause.
  2. For branch switches, confirm the branch exists (git branch -a) and the working tree is clean first.
  3. For remote fetch failures, verify network, credentials, and that the remote URL is reachable.

Example fix

# before
/mcp-source branch nonexistent-branch
# after (diagnose)
git -C <unity-project> fetch origin
git -C <unity-project> branch -a | grep <name>
# then retry /mcp-source branch <real-branch>
Defensive patterns

Strategy: try-catch

Validate before calling

def safe_run_git(repo, *args):
    result = subprocess.run(['git','-C',str(repo),*args], capture_output=True, text=True)
    if result.returncode != 0:
        return None, result.stderr.strip()
    return result.stdout.strip(), None

Type guard

def git_succeeds(repo, *args) -> bool:
    return subprocess.run(['git','-C',str(repo),*args], capture_output=True).returncode == 0

Try / catch

try:
    run_git(repo, 'checkout', branch)
except RuntimeError as e:
    print(f'git failed: {e}'); sys.exit(1)

Prevention

When it happens

Trigger: Subprocess git invocation returns returncode != 0: bad ref name, missing remote, dirty working tree blocking checkout, no network/auth for a private remote, or the repo path does not exist.

Common situations: Running /mcp-source branch with a branch name that doesn't exist; switching to a tag while on detached HEAD with conflicts; corporate proxy/SSH key not set; the local Unity project is not a git repo so -C fails.

Related errors


AI-assisted analysis of CoplayDev/unity-mcp@c21bf496bc (2026-08-13). Data as JSON: /api/errors/3f5f21974e749b00. Report an issue: GitHub.