{"record":{"id":"18a758b5bd6ce253","repo":"nikivdev/code","slug":"target-path-exists-but-is-not-a-git-checkout","errorCode":null,"errorMessage":"target path exists but is not a git checkout: {}","messagePattern":"target path exists but is not a git checkout: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/repos.rs","lineNumber":527,"sourceCode":"    };\n\n    configure_upstream(&target_dir, &upstream_url, fetch_depth)?;\n    if shallow {\n        spawn_background_history_fetch(&target_dir, !upstream_is_origin)?;\n    }\n\n    init_jj_repo(&target_dir)?;\n    Ok(CloneRepoResult {\n        path: target_dir,\n        already_cloned: false,\n    })\n}\n\nfn preflight_clone_target(target_dir: &Path) -> Result<bool> {\n    match clone_target_state(target_dir)? {\n        CloneTargetState::Missing | CloneTargetState::EmptyDir => Ok(false),\n        CloneTargetState::GitCheckout => Ok(true),\n        CloneTargetState::OccupiedNonRepo => bail!(\n            \"target path exists but is not a git checkout: {}\",\n            target_dir.display()\n        ),\n    }\n}\n\n#[derive(Debug, Clone, Copy, PartialEq, Eq)]\nenum CloneTargetState {\n    Missing,\n    EmptyDir,\n    GitCheckout,\n    OccupiedNonRepo,\n}\n\nfn clone_target_state(path: &Path) -> Result<CloneTargetState> {\n    if !path.exists() {\n        return Ok(CloneTargetState::Missing);\n    }","sourceCodeStart":509,"sourceCodeEnd":545,"githubUrl":"https://github.com/nikivdev/code/blob/a747e741ae92c09071d0ae946ab48488adcff1ce/src/repos.rs#L509-L545","documentation":"preflight_clone_target checks the clone destination state before cloning. If the target path already exists and contains files but is neither empty nor a git checkout (CloneTargetState::OccupiedNonRepo), the clone is refused with this message naming the path. It prevents clobbering unrelated directory contents.","triggerScenarios":"Calling clone_repo (via preflight_clone_target) with a target_dir that exists, is non-empty, and has no `.git` directory.","commonSituations":"Cloning into a directory that holds stray files or an old non-git project; typos in the target path reusing an existing folder; leftover build/download directories.","solutions":["Choose a new, empty target directory.","Move/delete the existing contents at that path (after verifying they are not needed).","If the directory should be a repo, re-clone or `git init`/repair it first so it becomes a valid checkout."],"exampleFix":"// before\nclone(repo, \"./existing-project\")  // contains random files\n// after\nclone(repo, \"./new-target\")  // missing or empty dir\n// or: $ mv existing-project existing-project.bak && clone(repo, \"./existing-project\")","handlingStrategy":"validation","validationCode":"fn clone_target_ok(dir: &Path) -> bool {\n    match dir.try_exists() {\n        Ok(false) => true,\n        Ok(true) => std::fs::read_dir(dir).map_or(false, |mut d| d.next().is_none())\n            || dir.join(\".git\").exists(),\n        Err(_) => false,\n    }\n}","typeGuard":null,"tryCatchPattern":"match clone_repo(url, target) {\n    Err(e) if e.to_string().contains(\"target path exists but is not a git checkout\") => {\n        // prompt user to move the directory or pick a new target\n        let alt = target.with_extension(\"clone\");\n        clone_repo(url, &alt)?;\n    }\n    r => r?,\n}","preventionTips":["Before cloning, check the target is missing, empty, or a git checkout.","Avoid reusing folder names that may hold stray files.","Keep workspaces organized so clone targets are dedicated directories.","Clean leftover temp/build directories before re-cloning into the same path."],"tags":["filesystem","clone","path"],"backgroundTag":"directory-not-empty","analyzedSha":"a747e741ae92c09071d0ae946ab48488adcff1ce","analyzedAt":"2026-09-01T22:43:55.719Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}