{"record":{"id":"3680dc4077218543","repo":"can1357/oh-my-pi","slug":"gitcommanderror-git-args-proc-returncode-p","errorCode":null,"errorMessage":"GitCommandError([\"git\", *args], proc.returncode, proc.stdout, proc.stderr)","messagePattern":"GitCommandError\\(\\[\"git\", \\*args\\], proc\\.returncode, proc\\.stdout, proc\\.stderr\\)","errorType":"exception","errorClass":"GitCommandError","httpStatus":null,"severity":"error","filePath":"python/robomp/src/git_ops.py","lineNumber":699,"sourceCode":"    user: int | None = None,\n    group: int | None = None,\n    extra_groups: list[int] | tuple[int, ...] | None = None,\n    umask: int | None = None,\n) -> str:\n    \"\"\"Return the SHA of HEAD or raise GitCommandError.\"\"\"\n    args = [\"rev-parse\", \"HEAD\"]\n    proc = _run_git(\n        args,\n        cwd=repo_dir,\n        token=None,\n        safe_directory=safe_directory,\n        user=user,\n        group=group,\n        extra_groups=extra_groups,\n        umask=umask,\n    )\n    if proc.returncode != 0:\n        raise GitCommandError([\"git\", *args], proc.returncode, proc.stdout, proc.stderr)\n    return proc.stdout.strip()\n\n\ndef inspect_dirty_state(\n    repo_dir: Path,\n    *,\n    slot_uid: int | None = None,\n    safe_directory: Path | None = None,\n) -> DirtyState:\n    \"\"\"Probe the worktree at `repo_dir` for uncommitted/unpushed work.\n\n    Returns a {@link DirtyState}. Errors from the underlying git invocations\n    are swallowed — the caller treats \"we couldn't tell\" as clean so a broken\n    git binary can't pin the agent in a reminder loop forever.\n    \"\"\"\n    slot_kwargs = _slot_subprocess_kwargs(slot_uid)\n    uncommitted = 0\n    uncommitted_sample: list[str] = []","sourceCodeStart":681,"sourceCodeEnd":717,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/python/robomp/src/git_ops.py#L681-L717","documentation":"A helper building git argument lists (with `user`, `group`, `extra_groups`, `umask` execution options) runs `git *args` and raises `GitCommandError([\"git\", *args], returncode, stdout, stderr)` on non-zero exit. Like `_check`, it unifies git failures into one exception type carrying the command and redacted output; `rev_parse_head` uses it and its callers (`push`, `push_release`) rely on HEAD resolution succeeding.","triggerScenarios":"Calling `rev_parse_head(repo_dir, ...)` outside a git repository (fatal: not a git repository), in an empty repo with no commits (unknown revision HEAD), or with a `safe_directory` mismatch making git refuse the repo (dubious ownership).","commonSituations":"Running the tool against a freshly `git init`ed repo before the first commit, wrong repo_dir path, container bind-mount ownership causing git's safe.directory error.","solutions":["Confirm repo_dir is a real git worktree with at least one commit","Resolve 'dubious ownership' by adding the repo to git's safe.directory or passing the correct safe_directory option","Call rev_parse_head only after a commit exists (e.g. after commit succeeds, not before the first one)","Inspect the exception's stderr/returncode for git's specific fatal message"],"exampleFix":"// before\nhead = rev_parse_head(new_repo_dir)  # fails: no commits yet\n// after\nif (new_repo_dir / \".git\").exists():\n    rev_parse_head(new_repo_dir)  # only after initial commit\nhead = rev_parse_head(new_repo_dir)","handlingStrategy":"try-catch","validationCode":"from pathlib import Path\nimport subprocess\nd = Path(repo_dir)\nif not (d / \".git\").exists():\n    raise ValueError(f\"{repo_dir} is not a git repo\")\nr = subprocess.run([\"git\", \"-C\", str(d), \"rev-parse\", \"--verify\", \"HEAD\"], capture_output=True)\nif r.returncode != 0:\n    raise ValueError(\"repo has no commits yet (HEAD unborn)\")","typeGuard":"def has_head(repo_dir) -> bool:\n    import subprocess\n    return subprocess.run([\"git\", \"-C\", str(repo_dir), \"rev-parse\", \"--verify\", \"HEAD\"],\n                          capture_output=True).returncode == 0","tryCatchPattern":"try:\n    head = rev_parse_head(repo_dir)\nexcept GitCommandError as exc:\n    if \"dubious ownership\" in (exc.stderr or \"\"):\n        add_safe_directory(repo_dir); head = rev_parse_head(repo_dir)\n    else:\n        raise","preventionTips":["Only resolve HEAD after at least one commit exists","Configure git safe.directory for container/bind-mount repos","Verify repo_dir points at the worktree root, not a subdirectory (though git -C handles both)"],"tags":["git","subprocess","head-resolution"],"backgroundTag":"git-command-failed","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}