Skip to content

Internal repos-config new syntax #6393

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ users)
* Speedup the detection of available system packages with pacman and brew [#6324 @kit-ty-kate]

## Format upgrade
* Add a note about to enforce no more upgrading last hard upgrade version (2.0.0~beta5), as far as possible. [#6416 @rjbou]
* Complete upgrade mechanism to permit on the fly upgrade and write upgrade from repo and switch level [#6416 @rjbou]

## Sandbox
* Respect the `DUNE_CACHE_ROOT` environment variable if it exists [#6326 @smorimoto]
Expand Down
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~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
Loading
Loading