From e10201bda39a6a43bab53c35b2c541422fbbfd84 Mon Sep 17 00:00:00 2001 From: Kate Date: Thu, 20 Jan 2022 14:22:55 +0000 Subject: [PATCH] Check that the repositories given to "opam repository remove" actually exist --- master_changes.md | 1 + src/client/opamCommands.ml | 20 +++++++++++++++++++- tests/reftests/repository.test | 2 ++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/master_changes.md b/master_changes.md index 8b2c42cab32..e562dd85d87 100644 --- a/master_changes.md +++ b/master_changes.md @@ -66,6 +66,7 @@ users) ## Lint ## Repository + * Check that the repositories given to `opam repository remove` actually exist [#5014 @kit-ty-kate - fixes #5012] ## Lock diff --git a/src/client/opamCommands.ml b/src/client/opamCommands.ml index 9aaed5dc39a..c02ac5627b5 100644 --- a/src/client/opamCommands.ml +++ b/src/client/opamCommands.ml @@ -2418,13 +2418,26 @@ let repository cli = `Ok () | Some `remove, names -> let names = List.map OpamRepositoryName.of_string names in - let rm = List.filter (fun n -> not (List.mem n names)) in let full_wipe = List.mem `All scope in let global = global || full_wipe in + let unknown_repos = ref (if full_wipe then [] else names) in + let rm = + List.filter (fun n -> + let seen = + List.exists (fun n' -> OpamRepositoryName.equal n n') names + in + if seen && not full_wipe then + unknown_repos := + List.filter + (fun n' -> not (OpamRepositoryName.equal n n')) + !unknown_repos; + not seen) + in let gt = OpamRepositoryCommand.update_selection gt ~global ~switches:switches rm in + let unknown_repos = !unknown_repos in if full_wipe then OpamRepositoryState.with_ `Lock_write gt @@ fun rt -> check_for_repos rt names @@ -2437,6 +2450,11 @@ let repository cli = "Repositories removed from the selections of switch %s. \ Use '--all' to forget about them altogether.\n" (OpamSwitch.to_string (OpamStateConfig.get_switch ())); + List.iter (fun repo -> + OpamConsole.warning "Repository '%s' not found in the \ + current switch, ignoring" + (OpamRepositoryName.to_string repo)) + unknown_repos; `Ok () | Some `add, [name] -> let name = OpamRepositoryName.of_string name in diff --git a/tests/reftests/repository.test b/tests/reftests/repository.test index f817d2f10c1..320acf92f35 100644 --- a/tests/reftests/repository.test +++ b/tests/reftests/repository.test @@ -623,10 +623,12 @@ to-many-commits git+file://${BASEDIR}/TMCS repos(1/3) 3 oper file://${BASEDIR}/OPER3 ### opam repository remove does-not-exist Repositories removed from the selections of switch repos. Use '--all' to forget about them altogether. +[WARNING] Repository 'does-not-exist' not found in the current switch, ignoring ### opam repository remove does-not-exist --all [WARNING] No configured repositories by these names found: does-not-exist ### opam repository remove does-not-exist oper Repositories removed from the selections of switch repos. Use '--all' to forget about them altogether. +[WARNING] Repository 'does-not-exist' not found in the current switch, ignoring ### opam repository remove does-not-exist repo2 --all [WARNING] No configured repositories by these names found: does-not-exist ### opam repository --all