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

Internal repos-config new syntax #6393

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
15 changes: 11 additions & 4 deletions src/client/opamClient.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1846,10 +1846,17 @@ let init
OpamStd.Sys.exit_because `Aborted);
try
(* Create the content of ~/.opam/config *)
let repos = match repo with
| Some r -> [r.repo_name, (r.repo_url, r.repo_trust)]
| None -> OpamFile.InitConfig.repositories init_config
in
let repos =
let open OpamFile.Repos_config in
match repo with
| Some r ->
[r.repo_name,
{repoc_url = r.repo_url; repoc_trust = r.repo_trust}]
| None ->
List.map (fun (n,(u,t)) ->
n, {repoc_url = u; repoc_trust = t})
( OpamFile.InitConfig.repositories init_config)
in
let config =
update_with_init_config
OpamFile.Config.(with_opam_root_version root_version empty)
Expand Down
132 changes: 127 additions & 5 deletions src/format/opamFile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1386,7 +1386,7 @@ module ConfigSyntax = struct
let internal = "config"
let format_version = OpamVersion.of_string "2.1"
let file_format_version = OpamVersion.of_string "2.0"
let root_version = OpamVersion.of_string "2.2"
let root_version = OpamVersion.of_string "2.4"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let root_version = OpamVersion.of_string "2.4"
let root_version = OpamVersion.of_string "2.4~alpha1"


let default_old_root_version = OpamVersion.of_string "2.1~~previous"

Expand Down Expand Up @@ -1957,13 +1957,17 @@ module InitConfig = struct
include SyntaxFile(InitConfigSyntax)
end

module Repos_configSyntax = struct
module Repos_config_LegacySyntax = struct

let internal = "repos-config"
let format_version = OpamVersion.of_string "2.0"
let file_format_version = OpamVersion.of_string "2.0"

type t = (url * trust_anchors option) OpamRepositoryName.Map.t
type repo = {
repoc_url: url;
repoc_trust: trust_anchors option;
}
type t = repo OpamRepositoryName.Map.t

let empty = OpamRepositoryName.Map.empty

Expand All @@ -1973,8 +1977,10 @@ module Repos_configSyntax = struct
((Pp.V.map_list ~depth:1 @@
InitConfigSyntax.pp_repository_def -|
Pp.pp
(fun ~pos:_ (name, url, ta) -> (name, (url, ta)))
(fun (name, (url, ta)) -> (name, url, ta))) -|
(fun ~pos:_ (name, repoc_url, repoc_trust) ->
(name, {repoc_url; repoc_trust}))
(fun (name, {repoc_url; repoc_trust}) ->
(name, repoc_url, repoc_trust))) -|
Pp.of_pair "repository-url-list"
OpamRepositoryName.Map.(of_list, bindings));
]
Expand All @@ -1991,6 +1997,122 @@ module Repos_configSyntax = struct
let pp = pp_cond ()

end

module Repos_config_Legacy = struct
include Repos_config_LegacySyntax
include SyntaxFile(Repos_config_LegacySyntax)
module BestEffort = MakeBestEffort(Repos_config_LegacySyntax)
end

module Repos_configSyntax = struct

let internal = "repos-config"
let format_version = OpamVersion.of_string "2.1"
let file_format_version = OpamVersion.of_string "2.0"

type repo = {
repoc_url: url;
repoc_trust: trust_anchors option;
}
type t = repo OpamRepositoryName.Map.t

let empty = OpamRepositoryName.Map.empty

let pp_repo =
let empty = { repoc_url = OpamUrl.empty; repoc_trust = None } in
Pp.I.fields ~name:"repos-config-repo" ~empty [
"url", Pp.ppacc
(fun repoc_url t -> { t with repoc_url })
(fun { repoc_url; _ } -> repoc_url)
Pp.V.url;
"fingerprint", Pp.ppacc_opt
(fun fingerprints t ->
match t.repoc_trust with
| Some x -> { t with repoc_trust = Some { x with fingerprints }}
| None -> { t with repoc_trust = Some { fingerprints; quorum = 1}})
(fun t ->
match t.repoc_trust with
| Some {fingerprints; _} -> Some fingerprints
| None -> None)
(Pp.V.map_list ~depth:1 Pp.V.string);
"quorum",
Pp.ppacc_opt
(fun quorum t ->
match t.repoc_trust with
| Some x -> { t with repoc_trust = Some { x with quorum }}
| None -> { t with repoc_trust = Some { quorum; fingerprints = []}})
(fun t ->
match t.repoc_trust with
| Some {quorum; _} -> Some quorum
| None -> None)
Pp.V.int;
]

let sections =
Pp.map_list
(Pp.I.section "repo"
-| Pp.pp ~name:"repo section"
(fun ~pos (name_opt, content) ->
let url = "repo", (Some pos, "missing URL") in
let name_repo = "repo", (Some pos, "missing repository name") in
let repo, errs = Pp.parse ~pos pp_repo content in
let nr, errs =
match name_opt with
| Some name ->
Some (OpamRepositoryName.of_string name, repo), errs
| None ->
None, name_repo::errs
in
if OpamUrl.equal OpamUrl.empty repo.repoc_url then
None, url::errs
else nr, errs
)
(function
| Some (name, repo), _ ->
Some (OpamRepositoryName.to_string name),
Pp.print pp_repo (repo, [])
| None, _ -> None, []))
-| Pp.pp ~name:"repositories"
(fun ~pos:_ repos ->
let repos, errs = List.split repos in
let repos = List.filter_map Fun.id repos in
let map = OpamRepositoryName.Map.of_list repos in
let errs = List.flatten errs in
map, errs)
(fun (map, _errs) ->
OpamRepositoryName.Map.bindings map
|> List.map (fun x -> Some x, []))
Comment on lines +2076 to +2084
Copy link
Member

@kit-ty-kate kit-ty-kate Feb 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two functions can be done more efficiently in one fold each


let pp_cond ?f ?condition () =
let name = internal in
let format_version = file_format_version in
Pp.I.map_file @@
Pp.I.check_opam_version ~optional:true ?f ~format_version () -|
Pp.I.opam_version ~format_version ~undefined:true () -|
Pp.I.partition (fun i -> match i.pelem with
| Section ({ section_kind={pelem="repo";_}; section_name=Some _; _ }) ->
false
| _ -> true)
-| Pp.map_pair
(* we need to keep the fields parser in order to display
unknown field errors *)
(let condition =
(* we need to propagate the BestEffort condition value *)
OpamStd.Option.map (fun cond -> fun () -> cond empty) condition
in
Pp.I.fields ~name ~empty:() []
-| Pp.I.show_errors ~name ?condition ()
-| Pp.pp (fun ~pos:_ _ -> ()) (fun () -> ())
)
(sections
-| Pp.I.show_errors ~name ?condition ())
-| Pp.pp (fun ~pos:_ (_, map) -> map)
(fun t -> (), t)

let pp = pp_cond ()

end

module Repos_config = struct
include Repos_configSyntax
include SyntaxFile(Repos_configSyntax)
Expand Down
16 changes: 15 additions & 1 deletion src/format/opamFile.mli
Original file line number Diff line number Diff line change
Expand Up @@ -1028,8 +1028,22 @@ module Repo_config_legacy : sig
include IO_FILE with type t := t
end

module Repos_config_Legacy: sig
type repo = {
repoc_url: url;
repoc_trust: trust_anchors option;
}
type t = repo OpamRepositoryName.Map.t
include IO_FILE with type t := t
module BestEffort: BestEffortRead with type t := t
end

module Repos_config: sig
type t = (url * trust_anchors option) OpamRepositoryName.Map.t
type repo = {
repoc_url: url;
repoc_trust: trust_anchors option;
}
type t = repo OpamRepositoryName.Map.t
include IO_FILE with type t := t
module BestEffort: BestEffortRead with type t := t
end
Expand Down
39 changes: 36 additions & 3 deletions src/state/opamFormatUpgrade.ml
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ let opam_file_from_1_2_to_2_0 ?filename opam =

(* Global state changes that need to be propagated *)
let gtc_none = { gtc_repo = false; gtc_switch = false }
let _gtc_repo = { gtc_repo = true; gtc_switch = false }
let gtc_repo = { gtc_repo = true; gtc_switch = false }
let _gtc_switch = { gtc_repo = false; gtc_switch = true }
let _gtc_both = { gtc_repo = true; gtc_switch = true }

Expand Down Expand Up @@ -770,7 +770,10 @@ let from_1_3_dev7_to_2_0_alpha ~on_the_fly:_ root conf =
in
OpamFile.Repos_config.write (OpamPath.repos_config root)
(OpamRepositoryName.Map.of_list
(List.map (fun (_, r, u) -> r, (u,None)) prio_repositories));
(List.map (fun (_, repo_name, repoc_url) ->
repo_name,
OpamFile.Repos_config.{repoc_url; repoc_trust = None})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably best to use the proper naming syntax instead of the local open one to avoid confusion in the future between values and fields:

Suggested change
OpamFile.Repos_config.{repoc_url; repoc_trust = None})
{OpamFile.Repos_config.repoc_url; repoc_trust = None})

prio_repositories));
let prio_repositories =
List.stable_sort (fun (prio1, _, _) (prio2, _, _) -> prio2 - prio1)
prio_repositories
Expand Down Expand Up @@ -1147,6 +1150,32 @@ let v2_2 = OpamVersion.of_string "2.2"

let from_2_2_beta_to_2_2 ~on_the_fly:_ _ conf = conf, gtc_none

let v2_3 = OpamVersion.of_string "2.3"

let from_2_2_to_2_3 ~on_the_fly:_ _ conf = conf, gtc_none

let v2_4 = OpamVersion.of_string "2.4"

let from_2_3_to_2_4 ~on_the_fly root conf =
(if on_the_fly then
OpamConsole.error_and_exit `Internal_error
"This 2.4 upgrade should be a hard upgrade");
let f = OpamPath.repos_config root in
OpamStd.Option.iter (fun old_repoconfig ->
let repos =
OpamRepositoryName.Map.map (fun old_repo ->
let OpamFile.Repos_config_Legacy.{repoc_url; repoc_trust} =
old_repo
in
OpamFile.Repos_config.{repoc_url; repoc_trust})
Comment on lines +1167 to +1170
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let OpamFile.Repos_config_Legacy.{repoc_url; repoc_trust} =
old_repo
in
OpamFile.Repos_config.{repoc_url; repoc_trust})
let {OpamFile.Repos_config_Legacy.repoc_url; repoc_trust} =
old_repo
in
{OpamFile.Repos_config.repoc_url; repoc_trust})

old_repoconfig
in
OpamFile.Repos_config.write f repos)
(OpamFile.Repos_config_Legacy.BestEffort.read_opt
(OpamFile.make
(OpamFile.filename f)));
Comment on lines +1175 to +1176
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(OpamFile.make
(OpamFile.filename f)));
(OpamFile.make (OpamFile.filename f)));

conf, gtc_repo

(* To add an upgrade layer
* If it is a light upgrade, returns as second element if the repo or switch
need an light upgrade with `gtc_*` values.
Expand All @@ -1156,7 +1185,7 @@ let from_2_2_beta_to_2_2 ~on_the_fly:_ _ conf = conf, gtc_none

let latest_version = OpamFile.Config.root_version

let latest_hard_upgrade = (* to *) v2_0_beta5
let latest_hard_upgrade = (* to *) v2_4

(* intermediate roots that need a hard upgrade when upgrading from them *)
let v2_1_intermediate_roots = [
Expand Down Expand Up @@ -1218,9 +1247,11 @@ let as_necessary ?reinit requested_lock global_lock root config =
let is_2_1_intermediate_root =
List.exists (OpamVersion.equal root_version) v2_1_intermediate_roots
in
(* As last hard upgrade is > 2.1~rc, we no more need that selection.
let latest_hard_upgrade =
if is_2_1_intermediate_root then v2_1_rc else latest_hard_upgrade
in
*)
(if is_2_1_intermediate_root then [
v2_1_alpha, from_2_0_to_2_1_alpha;
v2_1_alpha2, from_2_1_alpha_to_2_1_alpha2;
Expand All @@ -1244,6 +1275,8 @@ let as_necessary ?reinit requested_lock global_lock root config =
v2_2_alpha, from_2_1_to_2_2_alpha;
v2_2_beta, from_2_2_alpha_to_2_2_beta;
v2_2, from_2_2_beta_to_2_2;
v2_3, from_2_2_to_2_3;
v2_4, from_2_3_to_2_4;
]
|> List.filter (fun (v,_) ->
OpamVersion.compare root_version v < 0)
Expand Down
10 changes: 6 additions & 4 deletions src/state/opamRepositoryState.ml
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ let load lock_kind gt =
load with best-effort (read-only)"
(OpamVersion.to_string (OpamFile.Config.opam_root_version gt.config))
(OpamVersion.to_string (OpamFile.Config.root_version));
let mk_repo name (url, ta) = {
let mk_repo name OpamFile.Repos_config.{repoc_url; repoc_trust} = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let mk_repo name OpamFile.Repos_config.{repoc_url; repoc_trust} = {
let mk_repo name {OpamFile.Repos_config.repoc_url; repoc_trust} = {

repo_name = name;
repo_url = url;
repo_trust = ta;
repo_url = repoc_url;
repo_trust = repoc_trust;
} in
let repositories = OpamRepositoryName.Map.mapi mk_repo repos_map in
let repos_tmp_root = lazy (OpamFilename.mk_tmp_dir ()) in
Expand Down Expand Up @@ -280,7 +280,9 @@ let write_config rt =
OpamFile.Repos_config.write (OpamPath.repos_config rt.repos_global.root)
(OpamRepositoryName.Map.filter_map (fun _ r ->
if r.repo_url = OpamUrl.empty then None
else Some (r.repo_url, r.repo_trust))
else
Some OpamFile.Repos_config.{ repoc_url = r.repo_url;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Some OpamFile.Repos_config.{ repoc_url = r.repo_url;
Some { OpamFile.Repos_config.repoc_url = r.repo_url;

repoc_trust = r.repo_trust})
rt.repositories)

let check_last_update () =
Expand Down
21 changes: 0 additions & 21 deletions tests/reftests/dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1931,27 +1931,6 @@
%{targets}
(run ./run.exe %{exe:../../src/client/opamMain.exe.exe} %{dep:upgrade-format.test} %{read-lines:testing-env}))))

(rule
(alias reftest-upgrade-two-point-o)
(enabled_if (and (or (<> %{env:TESTALL=1} 0) (= %{env:TESTN0REP0=0} 1))))
(action
(diff upgrade-two-point-o.test upgrade-two-point-o.out)))

(alias
(name reftest)
(enabled_if (and (or (<> %{env:TESTALL=1} 0) (= %{env:TESTN0REP0=0} 1))))
(deps (alias reftest-upgrade-two-point-o)))

(rule
(targets upgrade-two-point-o.out)
(deps root-N0REP0)
(enabled_if (and (or (<> %{env:TESTALL=1} 0) (= %{env:TESTN0REP0=0} 1))))
(package opam)
(action
(with-stdout-to
%{targets}
(run ./run.exe %{exe:../../src/client/opamMain.exe.exe} %{dep:upgrade-two-point-o.test} %{read-lines:testing-env}))))

(rule
(alias reftest-upgrade)
(enabled_if (and (or (<> %{env:TESTALL=1} 0) (= %{env:TESTN0REP0=0} 1))))
Expand Down
Loading
Loading