{"record":{"id":"ced0fe7b200b3cc6","repo":"nikivdev/code","slug":"failed-to-resolve-head-commit","errorCode":null,"errorMessage":"failed to resolve HEAD commit","messagePattern":"failed to resolve HEAD commit","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/git_guard.rs","lineNumber":144,"sourceCode":"}\n\nfn land_head_to_branch(repo_root: &Path, requested_target: &str) -> Result<()> {\n    ensure_clean_working_tree_for_land(repo_root)?;\n\n    let current = git_capture_in(repo_root, &[\"rev-parse\", \"--abbrev-ref\", \"HEAD\"])\n        .unwrap_or_else(|| \"HEAD\".to_string());\n    if current.trim() == \"HEAD\" {\n        bail!(\"HEAD is detached. Run `f git-repair` first.\");\n    }\n    let current = current.trim().to_string();\n    let target = resolve_land_target_branch(repo_root, requested_target)?;\n    if current == target {\n        println!(\"Already on {}\", target);\n        return Ok(());\n    }\n\n    let head_sha = git_capture_in(repo_root, &[\"rev-parse\", \"HEAD\"])\n        .ok_or_else(|| anyhow::anyhow!(\"failed to resolve HEAD commit\"))?;\n\n    git_run_in(repo_root, &[\"checkout\", &target])?;\n    match git_run_in(repo_root, &[\"cherry-pick\", head_sha.trim()]) {\n        Ok(_) => {\n            println!(\n                \"✓ Landed commit {} from {} onto {}\",\n                short_sha(head_sha.trim()),\n                current,\n                target\n            );\n            Ok(())\n        }\n        Err(err) => {\n            let conflicts = git_unmerged_files(repo_root);\n            let _ = git_run_in(repo_root, &[\"cherry-pick\", \"--abort\"]);\n            let _ = git_run_in(repo_root, &[\"checkout\", &current]);\n\n            eprintln!(","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/nikivdev/code/blob/a747e741ae92c09071d0ae946ab48488adcff1ce/src/git_guard.rs#L126-L162","documentation":"land_head_to_branch resolves the current HEAD commit via `git rev-parse HEAD` before cherry-picking it onto the target branch. If the git command produces no output, the function throws this error instead of proceeding with an empty SHA.","triggerScenarios":"Running `run_git_repair` land flow in a repository with no commits (unborn HEAD, freshly `git init`), a corrupted HEAD ref, or running outside a valid work tree so git_capture_in returns None.","commonSituations":"Repo with zero commits, detached/broken .git/HEAD pointing to a missing ref, wrong working directory passed as repo_root.","solutions":["Make an initial commit first (git commit --allow-empty) so HEAD resolves","Verify repo_root points at a real git work tree (git -C <root> status)","Check .git/HEAD and branch refs are not corrupted; run git fsck","Ensure you're not in a bare repo when a commit is expected"],"exampleFix":"// before\nlet head_sha = git_capture_in(repo_root, &[\"rev-parse\", \"HEAD\"])\n    .ok_or_else(|| anyhow::anyhow!(\"failed to resolve HEAD commit\"))?;\n// after\nif git_capture_in(repo_root, &[\"rev-parse\", \"--verify\", \"HEAD\"]).is_none() {\n    anyhow::bail!(\"no commits on HEAD; create a commit before landing\");\n}\nlet head_sha = git_capture_in(repo_root, &[\"rev-parse\", \"HEAD\"])\n    .ok_or_else(|| anyhow::anyhow!(\"failed to resolve HEAD commit\"))?;","handlingStrategy":"validation","validationCode":"fn has_commits(root: &Path) -> bool {\n    std::process::Command::new(\"git\").args([\"-C\"]).arg(root)\n        .args([\"rev-parse\", \"--verify\", \"HEAD\"])\n        .output().map(|o| o.status.success()).unwrap_or(false)\n}","typeGuard":null,"tryCatchPattern":"match land_head_to_branch(root, target) {\n    Err(e) if e.to_string().contains(\"resolve HEAD\") => {\n        eprintln!(\"repo has no commits; commit first\");\n    }\n    other => other?,\n}","preventionTips":["Only run repair/land flows in repos with at least one commit","Validate repo_root with `git rev-parse --is-inside-work-tree` first","Check `git fsck` health after crashes"],"tags":["git","repo-state"],"backgroundTag":"unresolved-git-head","analyzedSha":"a747e741ae92c09071d0ae946ab48488adcff1ce","analyzedAt":"2026-09-01T22:43:55.719Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}