Skip to content

Commit

Permalink
Resolve '_opam' links to system switches
Browse files Browse the repository at this point in the history
  • Loading branch information
AltGr committed Mar 23, 2018
1 parent 2141185 commit 54aeb0c
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 19 deletions.
2 changes: 1 addition & 1 deletion admin-scripts/couverture.ml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ let () =
OpamStateConfig.init ();
let t =
OpamState.load_state ~save_cache:false "couverture"
OpamStateConfig.(!r.current_switch)
(OpamStateConfig.get_switch_opt ())
in
let avail = Lazy.force t.OpamState.Types.available_packages in
let wanted = match Array.to_list Sys.argv with
Expand Down
8 changes: 3 additions & 5 deletions src/client/opamClient.ml
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,9 @@ let update
log "UPDATE %a" (slog @@ String.concat ", ") names;
let rt = OpamRepositoryState.load `Lock_none gt in
let st, repos_only =
if OpamStateConfig.(!r.current_switch) = None then
OpamSwitchState.load_virtual gt rt, true
else
OpamSwitchState.load `Lock_none gt rt (OpamStateConfig.get_switch ()),
repos_only
match OpamStateConfig.get_switch_opt () with
| None -> OpamSwitchState.load_virtual gt rt, true
| Some sw -> OpamSwitchState.load `Lock_none gt rt sw, repos_only
in
let repo_names =
let all_repos = OpamRepositoryName.Map.keys rt.repositories in
Expand Down
10 changes: 5 additions & 5 deletions src/client/opamCommands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ let list ?(force_search=false) () =
owns_file disjunction search format packages =
apply_global_options global_options;
let no_switch =
no_switch || OpamStateConfig.(!r.current_switch) = None
no_switch || OpamStateConfig.get_switch_opt () = None
in
let format =
let force_all_versions =
Expand Down Expand Up @@ -758,7 +758,7 @@ let config =
match command, params with
| Some `env, [] ->
OpamGlobalState.with_ `Lock_none @@ fun gt ->
(match OpamStateConfig.(!r.current_switch) with
(match OpamStateConfig.get_switch_opt () with
| None -> `Ok ()
| Some sw ->
`Ok (OpamConfigCommand.env gt sw
Expand Down Expand Up @@ -1047,7 +1047,7 @@ let env =
match revert with
| false ->
OpamGlobalState.with_ `Lock_none @@ fun gt ->
(match OpamStateConfig.(!r.current_switch) with
(match OpamStateConfig.get_switch_opt () with
| None -> ()
| Some sw ->
OpamConfigCommand.env gt sw
Expand Down Expand Up @@ -1599,7 +1599,7 @@ let repository =
else if List.mem sw acc then acc
else acc @ [sw]
| `Current_switch ->
match OpamStateConfig.(!r.current_switch) with
match OpamStateConfig.get_switch_opt () with
| None ->
OpamConsole.warning "No switch is currently set, maybe you meant \
'--set-default' ?";
Expand Down Expand Up @@ -2753,7 +2753,7 @@ let clean =
in
let switches =
if all_switches then OpamGlobalState.switches gt
else if switch then match OpamStateConfig.(!r.current_switch) with
else if switch then match OpamStateConfig.get_switch_opt () with
| Some s -> [s]
| None -> []
else []
Expand Down
2 changes: 1 addition & 1 deletion src/client/opamConfigCommand.ml
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ let variable gt v =
| Some c ->
OpamConsole.msg "%s\n" (OpamVariable.string_of_variable_contents c)
| None ->
match OpamStateConfig.(!r.current_switch) with
match OpamStateConfig.get_switch_opt () with
| None ->
OpamConsole.error_and_exit `Not_found
"Variable %s not found"
Expand Down
2 changes: 1 addition & 1 deletion src/client/opamListCommand.ml
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ let display st format packages =

let get_switch_state gt =
let rt = OpamRepositoryState.load `Lock_none gt in
match OpamStateConfig.(!r.current_switch) with
match OpamStateConfig.get_switch_opt () with
| None -> OpamSwitchState.load_virtual gt rt
| Some sw -> OpamSwitchState.load `Lock_none gt rt sw

Expand Down
4 changes: 2 additions & 2 deletions src/client/opamMain.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ let check_and_run_external_commands () =
["PATH", PlusEq, OpamFilename.Dir.to_string plugins_bin, None]
in
OpamStateConfig.init ~root_dir ();
match OpamStateConfig.(!r.current_switch) with
match OpamStateConfig.get_switch_opt () with
| None -> env_array (OpamEnv.get_pure ~updates ())
| Some sw ->
env_array
Expand All @@ -60,7 +60,7 @@ let check_and_run_external_commands () =
| None when not has_init -> ()
| None ->
(* Look for a corresponding package *)
match OpamStateConfig.(!r.current_switch) with
match OpamStateConfig.get_switch_opt () with
| None -> ()
| Some sw ->
OpamGlobalState.with_ `Lock_none @@ fun gt ->
Expand Down
4 changes: 2 additions & 2 deletions src/client/opamSwitchCommand.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ let list gt ~print_short =
List.map (OpamConsole.colorise `blue)
["#"; "switch"; "compiler"; "description" ] ::
List.map (fun (switch, (packages, descr)) ->
let current = Some switch = OpamStateConfig.(!r.current_switch) in
let current = Some switch = OpamStateConfig.get_switch_opt () in
List.map
(if current then OpamConsole.colorise `bold else fun s -> s)
[ if current then if OpamConsole.utf8 ()
Expand All @@ -86,7 +86,7 @@ let list gt ~print_short =
OpamStd.Format.print_table stdout ~sep:" "
(OpamStd.Format.align_table table);

match OpamStateConfig.(!r.current_switch), OpamStateConfig.(!r.switch_from)
match OpamStateConfig.get_switch_opt (), OpamStateConfig.(!r.switch_from)
with
| None, _ when OpamFile.Config.installed_switches gt.config <> [] ->
OpamConsole.note
Expand Down
3 changes: 3 additions & 0 deletions src/core/opamFilename.ml
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ let is_exec file =
let starts_with dirname filename =
OpamStd.String.starts_with ~prefix:(Dir.to_string dirname) (to_string filename)

let dir_starts_with pfx dir =
OpamStd.String.starts_with ~prefix:(Dir.to_string pfx) (Dir.to_string dir)

let remove_prefix prefix filename =
let prefix =
let str = Dir.to_string prefix in
Expand Down
3 changes: 3 additions & 0 deletions src/core/opamFilename.mli
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ val starts_with: Dir.t -> t -> bool
(** Check whether a filename ends with a given suffix *)
val ends_with: string -> t -> bool

(** [dir starts_with pfx dir] Check whether [dir] starts with [pfx] *)
val dir_starts_with: Dir.t -> Dir.t -> bool

(** Check whether a dirname ends with a given suffix *)
val dir_ends_with: string -> Dir.t -> bool

Expand Down
2 changes: 1 addition & 1 deletion src/format/opamSwitch.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ let of_dirname d =

let get_root root s =
if is_external s
then OpamFilename.Op.(OpamFilename.Dir.of_string s / external_dirname)
then OpamFilename.Dir.of_string (Filename.concat s external_dirname)
else OpamFilename.Op.(root / s)
16 changes: 15 additions & 1 deletion src/state/opamStateConfig.ml
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,22 @@ let load_defaults root_dir =
update ?current_switch ();
Some conf

let get_switch () =
let get_switch_opt () =
match !r.current_switch with
| Some s ->
if OpamSwitch.is_external s then
let root = !r.root_dir in
let switch_root = OpamSwitch.get_root root s in
if OpamFilename.dir_starts_with root switch_root then
(* actually symlinked to a global switch *)
Some (OpamSwitch.of_string
(OpamFilename.remove_prefix_dir root switch_root))
else Some s
else Some s
| None -> None

let get_switch () =
match get_switch_opt () with
| Some s -> s
| None ->
OpamConsole.error_and_exit `Configuration_error
Expand Down
3 changes: 3 additions & 0 deletions src/state/opamStateConfig.mli
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ val load_defaults: OpamFilename.Dir.t -> OpamFile.Config.t option
(** Returns the current switch, failing with an error message is none is set. *)
val get_switch: unit -> switch

(** Returns the current switch, if any is set. *)
val get_switch_opt: unit -> switch option

(** The function used to locate an external switch from parents of the current
directory. Takes the opam root as parameter, and rejects any external switch
configured with a different root *)
Expand Down

0 comments on commit 54aeb0c

Please sign in to comment.