{"record":{"id":"238a2b2589de9b3d","repo":"can1357/oh-my-pi","slug":"gitcommanderror-git-branch-m-failed-nonzero-exi","errorCode":null,"errorMessage":"GitCommandError: git branch -m failed (nonzero exit)","messagePattern":"GitCommandError: git branch -m failed \\(nonzero exit\\)","errorType":"exception","errorClass":"GitCommandError","httpStatus":null,"severity":"error","filePath":"python/robomp/src/sandbox.py","lineNumber":208,"sourceCode":"    new_branch = f\"farm/{parts[1]}/{new_slug}\"\n    if new_branch == workspace.branch:\n        return new_branch\n    if pr_number is not None:\n        log.warning(\n            \"rename_workspace_branch skipped: PR #%d already tracks %r; refusing to rename to %r\",\n            pr_number,\n            workspace.branch,\n            new_branch,\n        )\n        return workspace.branch\n    proc = _safe_run(\n        [\"git\", \"branch\", \"-m\", workspace.branch, new_branch],\n        cwd=workspace.repo_dir,\n        env=_git_env_for_repo(workspace.repo_dir),\n        **_slot_subprocess_kwargs(slot_uid),\n    )\n    if proc.returncode != 0:\n        raise GitCommandError(\n            [\"git\", \"branch\", \"-m\", workspace.branch, new_branch],\n            proc.returncode,\n            proc.stdout,\n            proc.stderr,\n        )\n    _share_git_metadata_with_slots(workspace.repo_dir, slot_uid)\n    workspace.branch = new_branch\n    return new_branch\n\n\n# ---------- GitTransport (transport abstraction over clone/fetch/push) ----------\n\n\nclass GitTransport(Protocol):\n    \"\"\"Pluggable remote-facing git operations.\n\n    Two implementations ship in-tree:\n    - `LocalGitTransport`: in-process git with PAT injected per invocation.","sourceCodeStart":190,"sourceCodeEnd":226,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/python/robomp/src/sandbox.py#L190-L226","documentation":"rename_workspace_branch shells out to `git branch -m <old> <new>` inside the worktree and raises GitCommandError when git exits nonzero. The most common cause is the target branch name already existing locally — git refuses to rename over an existing branch. Other causes include a corrupt or locked repository and missing git binary permissions in slot-restricted environments.","triggerScenarios":"Renaming to a slug whose resulting branch `farm/<hex>/<slug>` already exists in the pool (e.g. a previous workspace or retry used the same slug); git lock contention from a concurrent worktree operation in the shared clone pool; repository corruption after a crashed git process.","commonSituations":"Re-running an issue after a partial failure left the target branch behind; the agent or operator picking a slug that collides with an earlier attempt; slot-uid sandboxing denying the subprocess access to the shared refs.","solutions":["Check stderr in the exception for 'branch already exists'; if so, verify the existing branch is stale and delete it (`git branch -D farm/<hex>/<slug>`) or pick a different slug.","Call git branch --list `farm/<hex>/*` first and choose a slug that does not collide.","Retry once — transient lock contention (index.lock) usually clears; the event queue will retry the task.","Inspect `git fsck` / stale `.git/worktrees` state if failures persist across all slugs."],"exampleFix":"# before\ntry:\n    rename_workspace_branch(ws, slug)\nexcept GitCommandError:\n    pass  # swallowed, state now inconsistent\n# after\ntry:\n    rename_workspace_branch(ws, slug)\nexcept GitCommandError as e:\n    if b\"already exists\" in (e.stderr or b\"\"):\n        slug = f\"{slug}-{int(time.time())}\"  # pick a fresh slug\n        rename_workspace_branch(ws, slug)\n    else:\n        raise","handlingStrategy":"try-catch","validationCode":"probe = subprocess.run([\"git\", \"branch\", \"--list\", new_branch], cwd=ws.repo_dir, capture_output=True)\nbranch_taken = bool(probe.stdout.strip())","typeGuard":null,"tryCatchPattern":"try:\n    rename_workspace_branch(ws, slug)\nexcept GitCommandError as e:\n    if b\"already exists\" in (e.stderr or b\"\"):\n        slug = f\"{slug}-{secrets.token_hex(2)}\"  # disambiguate and retry\n        rename_workspace_branch(ws, slug)\n    else:\n        raise","preventionTips":["List existing `farm/<hex>/*` branches and pick a non-colliding slug first.","Always inspect e.stderr — it contains the specific git refusal.","Retry transient failures via the event queue rather than swallowing the exception."],"tags":["git","branch-rename","subprocess"],"backgroundTag":"git-command-failed","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}