How to split a monorepo accross multiple teams so that each has access to only a slice of the repo?

There are lots of advantages to a monorepo. We also read that large companies like Google and Facebook use this tech to keep all source code in a single repo.

But how do you manage to limit access of a certain team to projects they are working on when using a monorepo?

For example, we have a microservice infrastructure:

  • Service A
  • Service B
  • Api Gateway

Service A is developed using Team A, and service B is developed by Team B, while Api Gateway is the common repo in this project.

How can we limit access of each team to its own service only?

Currently we keep every part in its own repo, and this way we can control access for each team. But I was asked to migrate this to monorepo, and I am not sure how to protect source code and split the project with such an approach.