{"record":{"id":"1307dabd7050eb10","repo":"zed-industries/zed","slug":"git-diff-tree-failed-stderr","errorCode":null,"errorMessage":"git diff-tree failed: {stderr}","messagePattern":"git diff-tree failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/git/src/repository.rs","lineNumber":1909,"sourceCode":"            .boxed()\n    }\n\n    fn status(&self, path_prefixes: &[RepoPath]) -> Task<Result<GitStatus>> {\n        let git = self.git_binary_in_worktree();\n        let args = git_status_args(path_prefixes);\n        log::debug!(\"Checking for git status in {path_prefixes:?}\");\n        self.executor.spawn(async move {\n            let git = git?;\n            let output = git.build_command(&args).output().await?;\n            if output.status.success() {\n                let stdout = String::from_utf8_lossy(&output.stdout);\n                stdout.parse()\n            } else {\n                let stderr = String::from_utf8_lossy(&output.stderr);\n                anyhow::bail!(\"git status failed: {stderr}\");\n            }\n        })\n    }\n\n    fn check_access(&self) -> BoxFuture<'_, Result<()>> {\n        let git = self.git_binary_in_worktree();\n        self.executor\n            .spawn(async move {\n                git?.run(&[\"rev-parse\"]).await?;\n                Ok(())\n            })\n            .boxed()\n    }\n\n    fn diff_tree(&self, request: DiffTreeType) -> BoxFuture<'_, Result<TreeDiff>> {\n        let git = self.git_binary_in_worktree();\n        let working_directory = self.working_directory.clone();\n        let merge_base_ref = match &request {\n            DiffTreeType::MergeBaseWithWorktree { base } => Some(base.clone()),\n            DiffTreeType::MergeBase { .. } | DiffTreeType::Since { .. } => None,\n        };","sourceCodeStart":1891,"sourceCodeEnd":1927,"githubUrl":"https://github.com/zed-industries/zed/blob/5a9b9558db01a6b906cec2fb70a797affdc58cdd/crates/git/src/repository.rs#L1891-L1927","documentation":"While computing a tree diff for a comparison, the code runs `git diff-tree` between a base ref and HEAD; a non-zero exit surfaces git's stderr here. As with all such wrappers, the real cause is in the appended stderr text.","triggerScenarios":"`git diff-tree ... HEAD` fails when HEAD is unborn (a fresh repo or new branch with zero commits yields `fatal: bad revision HEAD`), when the base/merge-base ref does not resolve, when the object database is corrupt (`fatal: bad object`), or on repository permission problems.","commonSituations":"Opening comparisons on a brand-new repository with no initial commit; comparing against a ref that was deleted or pruned; interrupted clones leaving a broken object store; worktrees created from an empty branch.","solutions":["Check the appended stderr for the failing revision name.","Treat unborn HEAD as an empty diff and skip the diff-tree call until the first commit exists.","Validate the base ref resolves: `git rev-parse --verify <base_ref>`.","If stderr says `bad object`, run `git fsck`; re-clone if the object store is damaged."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// skip diffs while HEAD is unborn (repo with no commits)\nif git.run(&[\"rev-parse\", \"--verify\", \"--quiet\", \"HEAD\"]).await.is_err() {\n    return Ok(TreeDiff::default());\n}","typeGuard":null,"tryCatchPattern":"match repo.diff_for_paths(..).await {\n    Err(e) if e.to_string().contains(\"diff-tree failed\") && e.to_string().contains(\"bad revision HEAD\") => {\n        Ok(TreeDiff::default()) // unborn branch: no diff yet\n    }\n    other => other?,\n}","preventionTips":["Guard comparisons with a HEAD-exists check before requesting diffs against HEAD.","Validate the compare/base ref resolves before starting the diff.","Re-clone interrupted clones instead of diffing a partially written object store."],"tags":["git","diff-tree","subprocess","stderr"],"backgroundTag":"git-command-failed","analyzedSha":"5a9b9558db01a6b906cec2fb70a797affdc58cdd","analyzedAt":"2026-08-20T19:29:52.058Z","contentChangedAt":"2026-08-20T19:29:52.058Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}