From d0cde13966e69020d8e8815b8322d31d8ce1e744 Mon Sep 17 00:00:00 2001 From: Raja Boujbel Date: Thu, 20 Jan 2022 22:19:57 +0100 Subject: [PATCH] wip --- src/client/opamCommands.ml | 38 ++++++++++++++++++++-------- src/client/opamRepositoryCommand.ml | 30 ++++++++++++---------- src/client/opamRepositoryCommand.mli | 2 +- src/state/opamSwitchState.ml | 22 ++++++++++------ src/state/opamSwitchState.mli | 2 +- 5 files changed, 60 insertions(+), 34 deletions(-) diff --git a/src/client/opamCommands.ml b/src/client/opamCommands.ml index 4e71354a8f2..08fa1997dda 100644 --- a/src/client/opamCommands.ml +++ b/src/client/opamCommands.ml @@ -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 \ @@ -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 @@ -2215,11 +2223,19 @@ 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 @@ -2227,7 +2243,7 @@ let repository cli = 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 () @@ -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 ()) diff --git a/src/client/opamRepositoryCommand.ml b/src/client/opamRepositoryCommand.ml index cb48d2811dd..1f953819e3a 100644 --- a/src/client/opamRepositoryCommand.ml +++ b/src/client/opamRepositoryCommand.ml @@ -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 *) diff --git a/src/client/opamRepositoryCommand.mli b/src/client/opamRepositoryCommand.mli index 6b964e9881a..77658769f59 100644 --- a/src/client/opamRepositoryCommand.mli +++ b/src/client/opamRepositoryCommand.mli @@ -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: diff --git a/src/state/opamSwitchState.ml b/src/state/opamSwitchState.ml index 9ba8bf49642..c3771da28a8 100644 --- a/src/state/opamSwitchState.ml +++ b/src/state/opamSwitchState.ml @@ -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)) diff --git a/src/state/opamSwitchState.mli b/src/state/opamSwitchState.mli index 61b5835afb5..3469eb9c2cb 100644 --- a/src/state/opamSwitchState.mli +++ b/src/state/opamSwitchState.mli @@ -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 } *)