Skip to content

Commit

Permalink
Use an explicit cache to avoid using the global cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonidas-from-XIV committed Jun 15, 2022
1 parent a950480 commit 78000f4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
18 changes: 9 additions & 9 deletions cli/lock.ml
Original file line number Diff line number Diff line change
Expand Up @@ -224,36 +224,36 @@ let interpret_solver_error ~repositories solver = function

(** Turns each repository URL into a path to the repository's sources,
eventually fetching them from the remote. *)
let make_repository_locally_available global_state url =
let make_repository_locally_available url =
let open OpamProcess.Job.Op in
match OpamUrl.local_dir url with
| Some path when Opam.Url.is_local_filesystem url ->
Done (Ok (OpamFilename.Dir.to_string OpamFilename.Op.(path / "packages")))
| _ -> (
let tmp_dir = Fpath.(Bos.OS.Dir.default_tmp () / "opam-monorepo") in
(* the URL might contain all kind of invalid characters like slashes -> hash *)
let repo_dir =
url |> OpamUrl.to_string |> Digest.string |> Digest.to_hex
in
let dir =
Fpath.(Bos.OS.Dir.default_tmp () / "opam-monorepo" / "repos" / repo_dir)
in
let dir = Fpath.(tmp_dir / "repos" / repo_dir) in
let cache_dir = Fpath.(tmp_dir / "cache") in
let url =
match OpamUrl.backend_of_string url.transport with
| `http -> OpamUrl.Op.(url / "index.tar.gz")
| `rsync | #OpamUrl.version_control -> url
in
match Bos.OS.Dir.create dir with
match Result.List.map ~f:Bos.OS.Dir.create [ cache_dir; dir ] with
| Error (`Msg msg) -> Done (Rresult.R.error_msg msg)
| Ok _ -> (
Opam.pull_tree ~url ~hashes:[] ~dir global_state @@| function
Opam.pull_tree_with_cache ~cache_dir ~url ~hashes:[] ~dir @@| function
| Error (`Msg msg) -> Rresult.R.error_msg msg
| Ok () ->
let packages = Fpath.(dir / "packages" |> to_string) in
Ok packages))

let make_repositories_locally_available global_state repositories =
let make_repositories_locally_available repositories =
repositories
|> OpamProcess.Job.seq_map (make_repository_locally_available global_state)
|> OpamProcess.Job.seq_map make_repository_locally_available
|> OpamProcess.Job.run |> Result.List.all

let opam_env_from_global_state global_state =
Expand Down Expand Up @@ -289,7 +289,7 @@ let calculate_opam ~source_config ~build_only ~allow_jbuilder
Fmt.(list ~sep:(const char '\n') Opam.Pp.url)
repositories);
let* local_repo_dirs =
make_repositories_locally_available global_state repositories
make_repositories_locally_available repositories
in
let opam_env = extract_opam_env ~source_config global_state in
let solver = Opam_solve.explicit_repos_solver in
Expand Down
15 changes: 11 additions & 4 deletions lib/opam.ml
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,8 @@ let has_cross_compile_tag opam =
let tags = OpamFile.OPAM.tags opam in
List.mem ~set:tags "cross-compile"

let pull_tree ~url ~hashes ~dir global_state =
let pull_tree_with_cache' ~cache_dir ~url ~hashes ~dir =
let dir_str = Fpath.to_string dir in
let cache_dir =
OpamRepositoryPath.download_cache global_state.OpamStateTypes.root
in
let label = dir_str in
(* Opam requires a label for the pull, it's only used for logging *)
let opam_dir = OpamFilename.Dir.of_string dir_str in
Expand All @@ -70,6 +67,16 @@ let pull_tree ~url ~hashes ~dir global_state =
| Not_available (_, long_msg) ->
Error (`Msg (Printf.sprintf "Failed to pull %s: %s" label long_msg))

let pull_tree ~url ~hashes ~dir global_state =
let cache_dir =
OpamRepositoryPath.download_cache global_state.OpamStateTypes.root
in
pull_tree_with_cache' ~cache_dir ~url ~hashes ~dir

let pull_tree_with_cache ~cache_dir =
let cache_dir = cache_dir |> Fpath.to_string |> OpamFilename.Dir.of_string in
pull_tree_with_cache' ~cache_dir

module Url = struct
type t = Git of { repo : string; ref : string option } | Other of string

Expand Down
10 changes: 10 additions & 0 deletions lib/opam.mli
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ val pull_tree :
if sucessful and an error otherwise.
This benefits from opam's global cache.*)

val pull_tree_with_cache :
cache_dir:Fpath.t ->
url:OpamUrl.t ->
hashes:OpamHash.t list ->
dir:Fpath.t ->
(unit, [> `Msg of string ]) result OpamProcess.job
(** Pulls the sources from [url] to [dir] using opam's library. Returns the target directory path
if sucessful and an error otherwise.
Uses a dedicated path for caching. *)

val local_package_version :
OpamFile.OPAM.t ->
explicit_version:OpamTypes.version option ->
Expand Down

0 comments on commit 78000f4

Please sign in to comment.