Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rjbou committed Jan 20, 2022
1 parent 27f19fa commit d0cde13
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 34 deletions.
38 changes: 27 additions & 11 deletions src/client/opamCommands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2186,8 +2186,9 @@ let repository cli =
(OpamRepositoryState.drop @@ OpamRepositoryCommand.remove rt name;
OpamConsole.error_and_exit `Sync_error
"Initial repository fetch failed"));
OpamGlobalState.drop @@ OpamRepositoryCommand.update_selection gt ~global
~switches (update_repos name);
OpamGlobalState.drop @@ fst @@
OpamRepositoryCommand.update_selection gt ~global ~switches
(update_repos name);
if scope = [`Current_switch] then
OpamConsole.note
"Repository %s has been added to the selections of switch %s \
Expand All @@ -2201,10 +2202,17 @@ 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 repos =
let names, unknown = List.partition (fun n -> List.mem n repos) names in
if unknown <> [] then
OpamConsole.warning "Repository %s not found, ignoring"
(OpamStd.Format.pretty_list
(List.map OpamRepositoryName.to_string unknown));
List.filter (fun n -> not (List.mem n names)) repos
in
let full_wipe = List.mem `All scope in
let global = global || full_wipe in
let gt =
let gt, updated_switches =
OpamRepositoryCommand.update_selection gt
~global ~switches:switches rm
in
Expand All @@ -2215,19 +2223,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
(let unchanged_switches =
let usw = List.map fst updated_switches in
List.filter (fun s -> not (List.mem s usw)) switches in
if unchanged_switches <> [] then
OpamConsole.warning "Unchanged switches %s"
(OpamStd.Format.pretty_list
(List.map OpamSwitch.to_string unchanged_switches));
if scope = [`Current_switch] && updated_switches <> [] 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 ())));
`Ok ()
| Some `add, [name] ->
let name = OpamRepositoryName.of_string name in
OpamRepositoryState.with_ `Lock_none gt (fun rt ->
check_for_repos rt [name]
(OpamConsole.error_and_exit `Not_found
"No configured repository '%s' found, you must specify an URL"));
OpamGlobalState.drop @@
OpamGlobalState.drop @@ fst @@
OpamRepositoryCommand.update_selection gt ~global ~switches
(update_repos name);
`Ok ()
Expand Down Expand Up @@ -2264,7 +2280,7 @@ let repository cli =
List.filter (fun r -> not (OpamRepositoryName.Map.mem r repos)) names
in
if not_found = [] then
(OpamGlobalState.drop @@
(OpamGlobalState.drop @@ fst @@
OpamRepositoryCommand.update_selection gt ~global ~switches
(fun _ -> names);
`Ok ())
Expand Down
30 changes: 17 additions & 13 deletions src/client/opamRepositoryCommand.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,23 @@ let update_global_selection gt update_fun =
gt

let update_selection gt ~global ~switches update_fun =
List.iter (OpamSwitchState.update_repositories gt update_fun) switches;
if global then
(* ensure all unselected switches aren't modified by changing the default *)
(List.iter (fun sw ->
if not (List.mem sw switches) then
OpamSwitchState.update_repositories gt (fun r -> r) sw)
(OpamFile.Config.installed_switches gt.config);
let (), gt =
OpamGlobalState.with_write_lock gt @@ fun gt ->
(), update_global_selection gt update_fun
in
gt)
else gt
let updated =
List.filter_map
(OpamSwitchState.update_repositories gt update_fun)
switches
in
(if global then
(* ensure all unselected switches aren't modified by changing the default *)
(List.iter (fun sw ->
if not (List.mem sw switches) then
ignore @@ OpamSwitchState.update_repositories gt (fun r -> r) sw)
(OpamFile.Config.installed_switches gt.config);
let (), gt =
OpamGlobalState.with_write_lock gt @@ fun gt ->
(), update_global_selection gt update_fun
in
gt)
else gt), updated

let update_repos_config rt repositories =
(* Remove cached opam files for changed or removed repos *)
Expand Down
2 changes: 1 addition & 1 deletion src/client/opamRepositoryCommand.mli
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ val update_global_selection:
val update_selection:
'a global_state -> global:bool -> switches:switch list ->
(repository_name list -> repository_name list) ->
'a global_state
'a global_state * (switch * OpamFile.Switch_config.t) list

(** Change the registered address of a repo *)
val set_url:
Expand Down
22 changes: 14 additions & 8 deletions src/state/opamSwitchState.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1219,15 +1219,21 @@ let update_repositories gt update_fun switch =
OpamFilename.with_flock `Lock_write (OpamPath.Switch.lock gt.root switch)
@@ fun _ ->
let conf = load_switch_config ~lock_kind:`Lock_write gt switch in
let repos =
let repos0 =
match conf.OpamFile.Switch_config.repos with
| None -> OpamGlobalState.repos_list gt
| Some repos -> repos
in
let conf =
{ conf with
OpamFile.Switch_config.repos = Some (update_fun repos) }
in
OpamFile.Switch_config.write
(OpamPath.Switch.switch_config gt.root switch)
conf
let repos = update_fun repos0 in
if
List.sort OpamRepositoryName.compare repos
= List.sort OpamRepositoryName.compare repos0 then
None else
(let conf =
{ conf with
OpamFile.Switch_config.repos = Some repos }
in
OpamFile.Switch_config.write
(OpamPath.Switch.switch_config gt.root switch)
conf;
Some (switch, conf))
2 changes: 1 addition & 1 deletion src/state/opamSwitchState.mli
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ val update_pin: package -> OpamFile.OPAM.t -> 'a switch_state -> 'a switch_state
repositories in any case, even if unchanged from the defaults. *)
val update_repositories:
'a global_state -> (repository_name list -> repository_name list) ->
switch -> unit
switch -> (switch * OpamFile.Switch_config.t) option

(** {2 User interaction and reporting } *)

Expand Down

0 comments on commit d0cde13

Please sign in to comment.