{"record":{"id":"7357b323b26afff1","repo":"affaan-m/ECC","slug":"cannot-reset-hunks-for-untracked-files","errorCode":null,"errorMessage":"cannot reset hunks for untracked files","messagePattern":"cannot reset hunks for untracked files","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"ecc2/src/worktree/mod.rs","lineNumber":432,"sourceCode":"pub fn unstage_hunk(worktree: &WorktreeInfo, hunk: &GitPatchHunk) -> Result<()> {\n    if hunk.section != GitPatchSectionKind::Staged {\n        anyhow::bail!(\"selected hunk is not staged\");\n    }\n    git_apply_patch(\n        &worktree.path,\n        &[\"-R\", \"--cached\"],\n        &hunk.patch,\n        \"unstage selected hunk\",\n    )\n}\n\npub fn reset_hunk(\n    worktree: &WorktreeInfo,\n    entry: &GitStatusEntry,\n    hunk: &GitPatchHunk,\n) -> Result<()> {\n    if entry.untracked {\n        anyhow::bail!(\"cannot reset hunks for untracked files\");\n    }\n\n    match hunk.section {\n        GitPatchSectionKind::Unstaged => {\n            git_apply_patch(&worktree.path, &[\"-R\"], &hunk.patch, \"reset selected hunk\")\n        }\n        GitPatchSectionKind::Staged => {\n            if entry.unstaged {\n                anyhow::bail!(\n                    \"cannot reset a staged hunk while the file also has unstaged changes; unstage it first\"\n                );\n            }\n            git_apply_patch(\n                &worktree.path,\n                &[\"-R\", \"--index\"],\n                &hunk.patch,\n                \"reset selected staged hunk\",\n            )","sourceCodeStart":414,"sourceCodeEnd":450,"githubUrl":"https://github.com/affaan-m/ECC/blob/8321021c54d670126ce3b2969d5deb880b4b0c2a/ecc2/src/worktree/mod.rs#L414-L450","documentation":"reset_hunk discards a hunk's changes by reverse-applying its patch, but untracked files have no diff hunks that git can reverse against an index — they are entirely new content. The library rejects the operation up front for any entry flagged untracked rather than letting git fail confusingly.","triggerScenarios":"Calling reset_hunk(worktree, entry, hunk) where entry.untracked == true, i.e. the file was created after HEAD and appears in the untracked section of the status/patch view.","commonSituations":"Showing 'discard hunk' actions for untracked files in a diff UI; hunk list built from untracked content but fed into the tracked-file reset path.","solutions":["Check entry.untracked before offering/calling reset_hunk; discard untracked files by deleting them instead.","Use file-level reset_path semantics appropriately, or remove the file from disk for full discard of untracked content.","Filter untracked entries out of hunk-level action menus in the UI."],"exampleFix":"// before\nreset_hunk(&worktree, &entry, &hunk)?;\n\n// after\nif entry.untracked {\n    std::fs::remove_file(worktree.path.join(&entry.path))?;\n} else {\n    reset_hunk(&worktree, &entry, &hunk)?;\n}","handlingStrategy":"validation","validationCode":"if entry.untracked {\n    // discard untracked content by deleting instead\n    std::fs::remove_file(worktree.path.join(&entry.path))?;\n    return Ok(());\n}","typeGuard":"fn is_hunk_resettable(entry: &GitStatusEntry) -> bool { !entry.untracked }","tryCatchPattern":"match reset_hunk(&worktree, &entry, &hunk) {\n    Ok(()) => refresh_patch_view(),\n    Err(e) if e.to_string().contains(\"untracked\") => {\n        std::fs::remove_file(worktree.path.join(&entry.path))?;\n    }\n    Err(e) => eprintln!(\"reset hunk failed: {e:#}\"),\n}","preventionTips":["Never offer hunk-level actions for untracked files; they have no index baseline.","Handle untracked discard via file deletion at the file level.","Filter the patch view so untracked entries appear as whole-file rows only."],"tags":["git","untracked-files","unsupported-operation"],"backgroundTag":"unsupported-operation","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"}