|
| 1 | +(*********************************************************************************) |
| 2 | +(* central - Manage history between sub-repos and their monorepo *) |
| 3 | +(* SPDX-FileCopyrightText: 2024-2026 Mathieu Barbin <mathieu.barbin@gmail.com> *) |
| 4 | +(* SPDX-License-Identifier: MIT *) |
| 5 | +(*********************************************************************************) |
| 6 | + |
| 7 | +open! Central |
| 8 | + |
| 9 | +(* @mdexp |
| 10 | +
|
| 11 | +# Subrepo |
| 12 | +
|
| 13 | +`Central.Subrepo.t` identifies one of the sub-repos vendored under `repo/` |
| 14 | +in the enclosing monorepo. It isn't a fixed, hand-maintained enum: it's just |
| 15 | +a validated string (the directory name under `repo/`), and the set of known |
| 16 | +sub-repos is discovered dynamically by walking the filesystem. |
| 17 | +
|
| 18 | +## Discovery |
| 19 | +
|
| 20 | +`all` walks `repo/`'s direct children and keeps the ones that contain a |
| 21 | +`.gitrepo` file, sorted by name: *) |
| 22 | + |
| 23 | +let%expect_test "Subrepo.all" = |
| 24 | + let vcs = Volgo_git_unix.create () in |
| 25 | + let widget = Subrepo.v "widget" in |
| 26 | + let gadget = Subrepo.v "gadget" in |
| 27 | + let fake_central = Central_test_helpers.create ~vcs ~subrepos:[ widget; gadget ] in |
| 28 | + let { Central_test_helpers.Fake_central.central_root; _ } = fake_central in |
| 29 | + List.iter (Subrepo.all ~repo_root:central_root) ~f:(fun t -> |
| 30 | + print_endline (Subrepo.to_string t)); |
| 31 | + [%expect |
| 32 | + {| |
| 33 | + gadget |
| 34 | + widget |
| 35 | + |}] |
| 36 | +;; |
| 37 | + |
| 38 | +(* @mdexp |
| 39 | +
|
| 40 | +`find_on_disk` looks a sub-repo name up against `all` - unlike `of_string`, |
| 41 | +it validates that the name actually names a vendored sub-repo, not merely |
| 42 | +that it has the right shape: *) |
| 43 | + |
| 44 | +let%expect_test "Subrepo.find_on_disk" = |
| 45 | + let vcs = Volgo_git_unix.create () in |
| 46 | + let widget = Subrepo.v "widget" in |
| 47 | + let fake_central = Central_test_helpers.create ~vcs ~subrepos:[ widget ] in |
| 48 | + let { Central_test_helpers.Fake_central.central_root; _ } = fake_central in |
| 49 | + let print_find name = |
| 50 | + print_dyn |
| 51 | + (Subrepo.find_on_disk ~repo_root:central_root ~name |> Dyn.option Subrepo.to_dyn) |
| 52 | + in |
| 53 | + print_find "widget"; |
| 54 | + [%expect {| Some "widget" |}]; |
| 55 | + print_find "does-not-exist"; |
| 56 | + [%expect {| None |}] |
| 57 | +;; |
| 58 | + |
| 59 | +(* @mdexp |
| 60 | +
|
| 61 | +## Manipulating paths |
| 62 | +
|
| 63 | +Unlike `all` and `find_on_disk` above, everything in this section is pure |
| 64 | +path manipulation: none of it reads the filesystem, or confirms that |
| 65 | +anything it is given actually exists on disk. |
| 66 | +
|
| 67 | +`root` and `gitrepo_file_path` locate a sub-repo's own directory, and its |
| 68 | +`.gitrepo` file, as paths in the enclosing monorepo: *) |
| 69 | + |
| 70 | +let%expect_test "Subrepo.root, Subrepo.gitrepo_file_path" = |
| 71 | + let widget = Subrepo.v "widget" in |
| 72 | + print_endline (Vcs.Path_in_repo.to_string (Subrepo.root widget)); |
| 73 | + [%expect {| repo/widget |}]; |
| 74 | + print_endline (Vcs.Path_in_repo.to_string (Subrepo.gitrepo_file_path widget)); |
| 75 | + [%expect {| repo/widget/.gitrepo |}] |
| 76 | +;; |
| 77 | + |
| 78 | +(* @mdexp |
| 79 | +
|
| 80 | +`central_path` and `subrepo_path` convert a path back and forth between the |
| 81 | +two frames of reference a path can be expressed in: relative to the |
| 82 | +sub-repo's own root (what the sub-repo's standalone checkout sees), or |
| 83 | +relative to the enclosing monorepo (prefixed with `root`, what the monorepo |
| 84 | +checkout sees): *) |
| 85 | + |
| 86 | +let print_subrepo_path t ~central_path = |
| 87 | + print_dyn (Subrepo.subrepo_path t ~central_path |> Dyn.option Vcs.Path_in_repo.to_dyn) |
| 88 | +;; |
| 89 | + |
| 90 | +let%expect_test "Subrepo.central_path, Subrepo.subrepo_path" = |
| 91 | + let widget = Subrepo.v "widget" in |
| 92 | + let subrepo_path = Vcs.Path_in_repo.v "src/dune" in |
| 93 | + let central_path = Subrepo.central_path widget ~subrepo_path in |
| 94 | + print_endline (Vcs.Path_in_repo.to_string central_path); |
| 95 | + (* @mdexp.snapshot { lang: "text" } *) |
| 96 | + [%expect {| repo/widget/src/dune |}]; |
| 97 | + print_subrepo_path widget ~central_path; |
| 98 | + [%expect {| Some "src/dune" |}] |
| 99 | +;; |
| 100 | + |
| 101 | +(* @mdexp |
| 102 | +
|
| 103 | +`subrepo_path` returns `None` for a path that doesn't belong to the |
| 104 | +sub-repo at all - and, since a path in the sub-repo's own repo is never |
| 105 | +empty, for the sub-repo's root itself: *) |
| 106 | + |
| 107 | +let%expect_test "Subrepo.subrepo_path, not under root" = |
| 108 | + let widget = Subrepo.v "widget" in |
| 109 | + print_subrepo_path widget ~central_path:(Vcs.Path_in_repo.v "README.md"); |
| 110 | + [%expect {| None |}]; |
| 111 | + print_subrepo_path widget ~central_path:(Vcs.Path_in_repo.v "repo/widget"); |
| 112 | + [%expect {| None |}] |
| 113 | +;; |
0 commit comments