diff --git a/crates/ide-vcs/src/lib.rs b/crates/ide-vcs/src/lib.rs index 91fe239ff..e194f69e6 100644 --- a/crates/ide-vcs/src/lib.rs +++ b/crates/ide-vcs/src/lib.rs @@ -529,9 +529,12 @@ pub enum CheckoutOutcome { LocalChanges { files: Vec, }, - /// These untracked files (repository-relative paths) would be overwritten. + /// These untracked files (repository-relative paths) would be overwritten by the + /// checkout; with `removed`, they would be removed by it (tracked here, absent + /// from the target). UntrackedFiles { files: Vec, + removed: bool, }, /// Local branch `local` has commits that remote branch `remote` does not. LocalBranchAhead { diff --git a/crates/ide-vcs/src/write.rs b/crates/ide-vcs/src/write.rs index 12cd0e6e2..b674375d1 100644 --- a/crates/ide-vcs/src/write.rs +++ b/crates/ide-vcs/src/write.rs @@ -771,9 +771,14 @@ fn overwritten_files(stderr: &str) -> Option> { listed_files(stderr, "local changes to the following files would be overwritten") } -/// Untracked files git refuses to overwrite, from its error message. -fn overwritten_untracked(stderr: &str) -> Option> { - listed_files(stderr, "untracked working tree files would be") +/// Untracked files git refuses to lose, from its error message: they would be *removed* +/// by the checkout (tracked here, absent from the target) or *overwritten* by it (present +/// in the target). Returns the files and whether they would be removed. +fn refused_untracked(stderr: &str) -> Option<(Vec, bool)> { + if let Some(files) = listed_files(stderr, "untracked working tree files would be removed") { + return Some((files, true)); + } + Some((listed_files(stderr, "untracked working tree files would be")?, false)) } /// The branch git refused to check out or update because another worktree has it, from @@ -840,8 +845,8 @@ pub(crate) fn checkout_with( if let Some((branch, path)) = branch_of_other_worktree(&stderr) { return Ok(CheckoutOutcome::CheckedOutInWorktree { branch, path: path.into() }); } - if let Some(files) = overwritten_untracked(&stderr) { - return Ok(CheckoutOutcome::UntrackedFiles { files }); + if let Some((files, removed)) = refused_untracked(&stderr) { + return Ok(CheckoutOutcome::UntrackedFiles { files, removed }); } let Some(files) = overwritten_files(&stderr) else { return Err(cli::failure(&argv, &out.stderr, &out.stdout, out.code)); diff --git a/crates/ide-vcs/tests/repo.rs b/crates/ide-vcs/tests/repo.rs index daa8346cd..b8a540ddc 100644 --- a/crates/ide-vcs/tests/repo.rs +++ b/crates/ide-vcs/tests/repo.rs @@ -2074,12 +2074,37 @@ fn checkout_reports_untracked_files_it_would_overwrite() { for mode in [CheckoutMode::Normal, CheckoutMode::Smart] { let opts = CheckoutOptions { mode, ..Default::default() }; let outcome = repo.checkout_with("other", false, &opts).unwrap(); - assert_eq!(outcome, CheckoutOutcome::UntrackedFiles { files: vec!["dir/new.txt".into()] }); + assert_eq!(outcome, CheckoutOutcome::UntrackedFiles { files: vec!["dir/new.txt".into()], removed: false }); assert_eq!(repo.branch_info().unwrap().head.as_deref(), Some("main")); assert_eq!(fx.read("dir/new.txt"), "mine\n"); } } +/// Untracked on disk but tracked on the current branch and gone from the target +/// (as after `git rm --cached`): git refuses to *remove* the file, and the outcome +/// says so (#3980). +#[test] +fn checkout_reports_untracked_files_it_would_remove() { + let fx = Fixture::new(); + fx.write("TEST.md", "hi\n"); + fx.commit_all("base"); + fx.git(&["checkout", "-q", "-b", "feature/x"]); + fx.git(&["rm", "-q", "TEST.md"]); + fx.commit_all("remove TEST.md on feature/x"); + fx.git(&["checkout", "-q", "main"]); + fx.git(&["rm", "--cached", "TEST.md"]); + fx.write("TEST.md", "local dirty version\n"); + let repo = fx.repo(); + for mode in [CheckoutMode::Normal, CheckoutMode::Smart] { + // The refusal leaves the repository untouched, so both modes see the same state. + let opts = CheckoutOptions { mode, ..Default::default() }; + let outcome = repo.checkout_with("feature/x", false, &opts).unwrap(); + assert_eq!(outcome, CheckoutOutcome::UntrackedFiles { files: vec!["TEST.md".into()], removed: true }); + assert_eq!(repo.branch_info().unwrap().head.as_deref(), Some("main")); + assert_eq!(fx.read("TEST.md"), "local dirty version\n"); + } +} + #[test] fn reword_keeps_local_changes_and_can_be_undone() { let fx = Fixture::new(); diff --git a/src/bindings.ts b/src/bindings.ts index cc6ccf3fc..78c9646fb 100644 --- a/src/bindings.ts +++ b/src/bindings.ts @@ -3293,8 +3293,12 @@ export type CheckoutOptions = { export type CheckoutOutcome = { kind: "done" } | /** These local changes (repository-relative paths) would be overwritten. */ { kind: "localChanges"; files: string[] } | -/** These untracked files (repository-relative paths) would be overwritten. */ -{ kind: "untrackedFiles"; files: string[] } | +/** + * These untracked files (repository-relative paths) would be overwritten by the + * checkout; with `removed`, they would be removed by it (tracked here, absent + * from the target). + */ +{ kind: "untrackedFiles"; files: string[]; removed: boolean } | /** Local branch `local` has commits that remote branch `remote` does not. */ { kind: "localBranchAhead"; local: string; remote: string } | /** diff --git a/src/features/vcs/__tests__/branchOps.test.ts b/src/features/vcs/__tests__/branchOps.test.ts index 9e36e16fa..6966b2a5a 100644 --- a/src/features/vcs/__tests__/branchOps.test.ts +++ b/src/features/vcs/__tests__/branchOps.test.ts @@ -219,6 +219,34 @@ it("does not open the worktree when the checkout offer is declined", async () => expect(mocks.executeAction).not.toHaveBeenCalled(); }); +it("names the files git would remove when untracked ones block the checkout", async () => { + mocks.checkout.mockResolvedValue(ok({ kind: "untrackedFiles", files: ["TEST.md"], removed: true })); + await checkoutBranch("feature/x"); + expect(mocks.checkout).toHaveBeenCalledTimes(1); + expect(mocks.notify).toHaveBeenCalledWith( + expect.objectContaining({ + level: "error", + title: "Untracked files prevent checkout", + body: expect.stringContaining( + "The following untracked working tree files would be removed by checkout:\nTEST.md", + ), + }), + ); +}); + +it("still says overwritten when untracked files would be overwritten", async () => { + mocks.checkout.mockResolvedValue(ok({ kind: "untrackedFiles", files: ["dir/new.txt"], removed: false })); + await checkoutBranch("feature/x"); + expect(mocks.notify).toHaveBeenCalledWith( + expect.objectContaining({ + level: "error", + body: expect.stringContaining( + "The following untracked working tree files would be overwritten by checkout:\ndir/new.txt", + ), + }), + ); +}); + it("keeps the branch when the worktree question is turned down", async () => { mocks.worktrees.mockResolvedValue(ok([worktree("/p/front-feature-wt", "feature/wt")])); mocks.confirm.mockResolvedValue(false); diff --git a/src/features/vcs/branchOps.ts b/src/features/vcs/branchOps.ts index 31b2097e9..bd589afdc 100644 --- a/src/features/vcs/branchOps.ts +++ b/src/features/vcs/branchOps.ts @@ -104,11 +104,17 @@ async function performCheckout( return true; case "untrackedFiles": { const files = outcome.files.join("\n"); + // git refuses untracked files in two ways: they would be overwritten by the + // checkout (present in the target), or removed by it (tracked here, absent + // from the target); each names its files the way git's own message does. + const body = outcome.removed + ? tx`The following untracked working tree files would be removed by checkout:\n${files}\nPlease move or remove them before you switch branches.` + : tx`The following untracked working tree files would be overwritten by checkout:\n${files}\nPlease move or remove them before you switch branches.`; notifications().notify({ group: "vcs", level: "error", title: "Untracked files prevent checkout", - body: tx`The following untracked working tree files would be overwritten by checkout:\n${files}\nPlease move or remove them before you switch branches.`, + body, }); return false; } diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 85355520b..2aa5dca19 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -7001,6 +7001,7 @@ "Choose Declaration": "选择声明", "Go to mapping: {target}": "跳转到映射:{target}", "Untracked files prevent checkout": "未跟踪的文件阻止了签出", + "The following untracked working tree files would be removed by checkout:\n{files}\nPlease move or remove them before you switch branches.": "签出将删除以下未跟踪的工作区文件:\n{files}\n请在切换分支前移动或删除它们。", "The following untracked working tree files would be overwritten by checkout:\n{files}\nPlease move or remove them before you switch branches.": "签出将覆盖以下未跟踪的工作区文件:\n{files}\n请在切换分支前移动或删除它们。", "Select Target Changelist": "选择目标更改列表", "Rebase undone": "已撤销变基",