Skip to content

Commit

Permalink
Check that the repositories given to "opam repository remove" actuall…
Browse files Browse the repository at this point in the history
…y exist
  • Loading branch information
kit-ty-kate authored and rjbou committed Nov 29, 2024
1 parent 8ac2d7e commit 6222549
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
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
37 changes: 29 additions & 8 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 @@ -2435,11 +2440,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
3 changes: 2 additions & 1 deletion tests/reftests/repository.test
Original file line number Diff line number Diff line change
Expand Up @@ -894,10 +894,11 @@ to-many-commits
oper3
oper
### opam repository remove does-not-exist
Repositories removed from the selections of switch rm-unknown. Use '--all' to forget about them altogether.
[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
Expand Down

0 comments on commit 6222549

Please sign in to comment.