Skip to content

Instantly share code, notes, and snippets.

@limitedeternity
Last active March 13, 2023 19:08
Show Gist options
  • Save limitedeternity/e278b7970c3793be98c82b23b973828d to your computer and use it in GitHub Desktop.
Save limitedeternity/e278b7970c3793be98c82b23b973828d to your computer and use it in GitHub Desktop.
Adds a git alias for running commands in all repositories of a monorepository
function New-Monorepo
{
[CmdletBinding()]
Param(
[Parameter(Mandatory, Position=0)]
[string] $Path
)
Process
{
pushd (Resolve-Path $Path)
Get-ChildItem -Force |
? { $_.PsIsContainer -eq $true -and $_.Name -ne ".git" } |
`
Get-ChildItem -Recurse -Force |
? { $_.PsIsContainer -eq $true -and $_.Name -eq ".git" } |
% { git config --local --add allRepos.repo $_.parent.FullName }
git config --local alias.monorepo "for-each-repo --config=allRepos.repo"
popd
}
}
function Save-BranchState
{
[CmdletBinding()]
Param(
[Parameter(Mandatory, Position=0)]
[string] $Monorepo,
[Parameter(Mandatory, Position=1)]
[string] $OutFile
)
Process
{
pushd (Resolve-Path $Monorepo)
$Repos = git monorepo rev-parse --show-toplevel | % { Resolve-Path -Relative $_ }
$Branches = git monorepo rev-parse --abbrev-ref HEAD
[System.Linq.Enumerable]::Zip([string[]]$Repos, [string[]]$Branches, [Func[string, string, object]] {
param($a, $b)
[PsCustomObject]@{
Repo = $a
Branch = $b
}
}) |
ConvertTo-Csv -NoTypeInformation |
Out-File $OutFile -Encoding ascii -Force
popd
}
}
function Import-BranchState
{
[CmdletBinding()]
Param(
[Parameter(Mandatory, Position=0)]
[string] $Monorepo,
[Parameter(Mandatory, Position=1)]
[string] $InFile
)
Process
{
pushd (Resolve-Path $Monorepo)
Get-Content -Encoding ascii (Resolve-Path $InFile) |
ConvertFrom-Csv |
% {
git -C $_.Repo fetch
git -C $_.Repo checkout $_.Branch
}
popd
}
}
@limitedeternity
Copy link
Author

limitedeternity commented Feb 20, 2023

Monorepository, in this particular context, is a repository with scripts for cloning actual project sources and dependencies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment