{"record":{"id":"57c81ff2ec5123b1","repo":"affaan-m/ECC","slug":"git-worktree-list-porcelain-failed-stderr","errorCode":null,"errorMessage":"git worktree list --porcelain failed: {stderr}","messagePattern":"git worktree list --porcelain failed: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"ecc2/src/worktree/mod.rs","lineNumber":1581,"sourceCode":"        .arg(repo_root)\n        .args([\"rev-parse\", \"--abbrev-ref\", \"HEAD\"])\n        .output()\n        .context(\"Failed to get current branch\")?;\n\n    Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())\n}\n\nfn base_checkout_path(worktree: &WorktreeInfo) -> Result<PathBuf> {\n    let output = Command::new(\"git\")\n        .arg(\"-C\")\n        .arg(&worktree.path)\n        .args([\"worktree\", \"list\", \"--porcelain\"])\n        .output()\n        .context(\"Failed to resolve git worktree list\")?;\n\n    if !output.status.success() {\n        let stderr = String::from_utf8_lossy(&output.stderr);\n        anyhow::bail!(\"git worktree list --porcelain failed: {stderr}\");\n    }\n\n    let target_branch = format!(\"refs/heads/{}\", worktree.base_branch);\n    let mut current_path: Option<PathBuf> = None;\n    let mut current_branch: Option<String> = None;\n    let mut fallback: Option<PathBuf> = None;\n\n    for line in String::from_utf8_lossy(&output.stdout).lines() {\n        if line.is_empty() {\n            if let Some(path) = current_path.take() {\n                if fallback.is_none() && path != worktree.path {\n                    fallback = Some(path.clone());\n                }\n                if current_branch.as_deref() == Some(target_branch.as_str())\n                    && path != worktree.path\n                {\n                    return Ok(path);\n                }","sourceCodeStart":1563,"sourceCodeEnd":1599,"githubUrl":"https://github.com/affaan-m/ECC/blob/8321021c54d670126ce3b2969d5deb880b4b0c2a/ecc2/src/worktree/mod.rs#L1563-L1599","documentation":"Raised when `git worktree list --porcelain` exits non-zero while the library is resolving the checkout path for a worktree's base branch. Since this command is the canonical source of worktree metadata, failure means git could not enumerate worktrees — corrupt .git/worktrees metadata, not a repo, or a git version too old to support --porcelain for worktree list.","triggerScenarios":"Calling worktree resolution outside a git repository (or with a corrupted GIT_DIR), with git < 2.7 lacking 'worktree list --porcelain', or with a damaged .git/worktrees directory containing stale admin files.","commonSituations":"Running the tool in a subdirectory that is not a repo (wrong cwd); pruned worktree metadata left behind after manual directory deletion; ancient system git on a CI container.","solutions":["Confirm the process cwd (repo root) is inside a git repository: 'git rev-parse --is-inside-work-tree'","Run 'git worktree prune' to clean stale worktree admin metadata","Check 'git --version' — upgrade git to >= 2.7 where --porcelain is supported for worktree list","Read the stderr in the message for git's specific complaint (e.g. 'not a git repository')","If a worktree dir was deleted manually, 'git worktree repair' or recreate it"],"exampleFix":"// before: assumes valid repo\nlet path = base_checkout_path(&wt)?;\n// after: pre-check repo validity\nlet ok = Command::new(\"git\")\n    .args([\"rev-parse\", \"--is-inside-work-tree\"])\n    .current_dir(&wt.path)\n    .output()?;\nanyhow::ensure!(ok.status.success(), \"{} is not a git repository\", wt.path.display());\nlet path = base_checkout_path(&wt)?;","handlingStrategy":"try-catch","validationCode":"let is_repo = Command::new(\"git\")\n    .args([\"rev-parse\", \"--is-inside-work-tree\"])\n    .current_dir(&wt.path)\n    .output()\n    .map(|o| o.status.success())\n    .unwrap_or(false);\nanyhow::ensure!(is_repo, \"{} is not a git repository\", wt.path.display());","typeGuard":null,"tryCatchPattern":"match base_checkout_path(&wt) {\n    Ok(p) => p,\n    Err(e) if e.to_string().contains(\"not a git repository\") => {\n        eprintln!(\"worktree {} is outside a repo or has stale metadata\", wt.path.display());\n        // 'git worktree prune' or 'git worktree repair' before retry\n        Err(e)\n    }\n    Err(e) => Err(e),\n}","preventionTips":["Pin a modern git (>= 2.7) in CI images","Run 'git worktree prune' after manually deleting worktree directories","Verify cwd is inside the repo before invoking worktree commands","Use 'git worktree repair' when moving repo directories"],"tags":["git","worktree","command"],"backgroundTag":"git-command-failed","analyzedSha":"8321021c54d670126ce3b2969d5deb880b4b0c2a","analyzedAt":"2026-09-16T10:08:13.343Z","contentChangedAt":"2026-09-16T10:08:13.343Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}