Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check that the repositories given to "opam repository remove" actually exist #5014

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ users)

## Repository
* Accurately tag `curl` download command when loaded from global config file [#6270 @rjbou]
* Check that the repositories given to `opam repository remove` actually exist [#5014 @kit-ty-kate - fixes #5012]

## Lock

Expand Down
42 changes: 33 additions & 9 deletions src/client/opamCommands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2345,13 +2345,18 @@ let repository cli =
OpamStd.List.insert_at rank new_repo
(List.filter (( <> ) new_repo ) repos)
in
let check_for_repos rt names err =
let check_for_repos' rt names err =
match
List.filter (fun n ->
not (OpamRepositoryName.Map.mem n rt.repositories))
names
with [] -> () | l ->
err (OpamStd.List.concat_map " " OpamRepositoryName.to_string l)
with [] -> true | l ->
err (OpamStd.List.concat_map " " OpamRepositoryName.to_string l);
List.compare_lengths l names <> 0
in
let check_for_repos rt names err =
let _has_known_repos : bool = check_for_repos' rt names err in
()
in
OpamGlobalState.with_ `Lock_none @@ fun gt ->
let all_switches = OpamFile.Config.installed_switches gt.config in
Expand Down Expand Up @@ -2421,7 +2426,10 @@ 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 rm =
kit-ty-kate marked this conversation as resolved.
Show resolved Hide resolved
List.filter (fun n ->
not (List.exists (OpamRepositoryName.equal n) names))
in
let full_wipe = List.mem `All scope in
let global = global || full_wipe in
let gt =
Expand All @@ -2435,11 +2443,27 @@ let repository cli =
"No configured repositories by these names found: %s");
OpamRepositoryState.drop @@
List.fold_left OpamRepositoryCommand.remove rt names
else if scope = [`Current_switch] then
OpamConsole.msg
"Repositories removed from the selections of switch %s. \
Use '--all' to forget about them altogether.\n"
(OpamSwitch.to_string (OpamStateConfig.get_switch ()));
else begin
let has_known_repos =
List.fold_left (fun has_known_repos switch ->
let rt =
OpamSwitchState.with_ `Lock_none ~switch gt @@ fun st ->
st.OpamStateTypes.switch_repos
in
check_for_repos' rt names
(OpamConsole.warning
"No configured repositories by these names found in \
the selection of switch '%s': %s"
(OpamSwitch.to_string switch))
|| has_known_repos)
false switches
in
if scope = [`Current_switch] && has_known_repos then
OpamConsole.msg
"Repositories removed from the selections of switch %s. \
Use '--all' to forget about them altogether.\n"
(OpamSwitch.to_string (OpamStateConfig.get_switch ()));
end;
`Ok ()
| Some `add, [name] ->
let name = OpamRepositoryName.of_string name in
Expand Down
41 changes: 41 additions & 0 deletions tests/reftests/repository.test
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@ GARBAGE
[ERROR] No package matching two-three found
# Return code 5 #
### : Repo config with no url repo
### cp OPAM/repo/repos-config repos-config.bak
### opam switch create nourl --empty
### opam repo remove --all repo versions
### <nourl/repo>
Expand Down Expand Up @@ -868,3 +869,43 @@ opam-version: "2.0"

# Packages matching: any
# No matches found
### mv repos-config.bak OPAM/repo/repos-config
### : Check the behaviour of opam repository remove when given unknown repositories
### <REM/repo>
opam-version: "2.0"
### mkdir REM/packages
### opam switch create rm-unknown --empty
### opam repository add oper ./REM --this-switch
[oper] Initialised
### opam repository add oper3 ./REM --this-switch
[oper3] Initialised
### opam repository add repo2 ./REM --dont-select
[repo2] Initialised
### opam repository add to-many-commits ./REM --this-switch
[to-many-commits] Initialised
### opam repository remove repo versions --all
### opam repository --all --short
oper
oper3
repo2
to-many-commits
### opam repository --short
to-many-commits
oper3
oper
### opam repository remove does-not-exist
[WARNING] No configured repositories by these names found in the selection of switch 'rm-unknown': does-not-exist
### 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
[WARNING] No configured repositories by these names found in the selection of switch 'rm-unknown': does-not-exist
Repositories removed from the selections of switch rm-unknown. Use '--all' to forget about them altogether.
### opam repository remove does-not-exist repo2 --all
[WARNING] No configured repositories by these names found: does-not-exist
### opam repository --all --short
oper
oper3
to-many-commits
### opam repository --short
to-many-commits
oper3