|
| 1 | +#!/usr/bin/env pwsh |
| 2 | +#Requires -Version 7.0 |
| 3 | + |
| 4 | +<# |
| 5 | +.SYNOPSIS |
| 6 | + Clone or update the MSX central workspace (docs + memory) in a git-isolated location under $HOME. |
| 7 | +
|
| 8 | +.DESCRIPTION |
| 9 | + The single starting point for every agent. It ensures the central |
| 10 | + documentation and memory repositories exist locally under one dedicated |
| 11 | + workspace, so an agent reads the same evergreen docs and the same prior |
| 12 | + memory regardless of which repository it is working in. |
| 13 | +
|
| 14 | + The workspace is deliberately kept separate from the repositories an agent |
| 15 | + works in: |
| 16 | +
|
| 17 | + - Each clone gets repository-local git config only. Nothing here modifies the |
| 18 | + global git config or the working repository's config; git still reads global |
| 19 | + and system config as usual, but this script writes only repository-local config. |
| 20 | + - Documentation (MSXOrg/docs) is context and is changed through pull requests |
| 21 | + only; this script never pushes its main branch. |
| 22 | + - Memory (MSXOrg/memory) is append-only context; notes are committed and |
| 23 | + pushed to main directly, without a pull request. |
| 24 | +
|
| 25 | + The script is idempotent: it clones what is missing and attempts to |
| 26 | + fast-forward what is already present, leaving a repository unchanged (with a |
| 27 | + warning) when it cannot fast-forward. |
| 28 | +
|
| 29 | +.EXAMPLE |
| 30 | + ./Initialize-MsxWorkspace.ps1 |
| 31 | + Clones missing repositories and attempts to fast-forward existing ones under ~/.msx. |
| 32 | +
|
| 33 | +.EXAMPLE |
| 34 | + ./Initialize-MsxWorkspace.ps1 -Root /work/.msx -Verbose |
| 35 | + Uses a custom workspace root and logs each step. |
| 36 | +
|
| 37 | +.OUTPUTS |
| 38 | + [pscustomobject] with Repository, Path, and Changes for each workspace repository. |
| 39 | +#> |
| 40 | +[CmdletBinding(SupportsShouldProcess)] |
| 41 | +param( |
| 42 | + # The workspace root under which 'docs' and 'memory' are placed. |
| 43 | + [Parameter()] |
| 44 | + [ValidateNotNullOrEmpty()] |
| 45 | + [string] $Root = (Join-Path $HOME '.msx'), |
| 46 | + |
| 47 | + # The git author name written to each clone's local config. |
| 48 | + [Parameter()] |
| 49 | + [ValidateNotNullOrEmpty()] |
| 50 | + [string] $UserName = 'Marius Storhaug', |
| 51 | + |
| 52 | + # The git author email written to each clone's local config. |
| 53 | + [Parameter()] |
| 54 | + [ValidateNotNullOrEmpty()] |
| 55 | + [string] $UserEmail = 'MariusStorhaug@users.noreply.github.com' |
| 56 | +) |
| 57 | + |
| 58 | +Set-StrictMode -Version Latest |
| 59 | +$ErrorActionPreference = 'Stop' |
| 60 | + |
| 61 | +if ((-not $PSBoundParameters.ContainsKey('UserName')) -or (-not $PSBoundParameters.ContainsKey('UserEmail'))) { |
| 62 | + Write-Warning "Using part of the default maintainer identity ($UserName <$UserEmail>). Pass both -UserName and -UserEmail to attribute your own commits (memory pushes to main)." |
| 63 | +} |
| 64 | + |
| 65 | +$repositories = @( |
| 66 | + [pscustomobject]@{ Name = 'docs'; Url = 'https://github.com/MSXOrg/docs.git'; Changes = 'pull requests' } |
| 67 | + [pscustomobject]@{ Name = 'memory'; Url = 'https://github.com/MSXOrg/memory.git'; Changes = 'push to main' } |
| 68 | +) |
| 69 | + |
| 70 | +if ($PSCmdlet.ShouldProcess($Root, 'Create workspace root')) { |
| 71 | + New-Item -ItemType Directory -Force -Path $Root | Out-Null |
| 72 | +} |
| 73 | + |
| 74 | +$results = foreach ($repo in $repositories) { |
| 75 | + $path = Join-Path $Root $repo.Name |
| 76 | + if (Test-Path (Join-Path $path '.git')) { |
| 77 | + if ($PSCmdlet.ShouldProcess($path, 'Fetch and fast-forward')) { |
| 78 | + Write-Verbose "Updating $path" |
| 79 | + git -C $path fetch origin --quiet |
| 80 | + if ($LASTEXITCODE -ne 0) { |
| 81 | + throw "git fetch failed for '$path' (exit $LASTEXITCODE). Check network access and credentials for $($repo.Url)." |
| 82 | + } |
| 83 | + git -C $path pull --ff-only --quiet |
| 84 | + if ($LASTEXITCODE -ne 0) { |
| 85 | + Write-Warning "Could not fast-forward '$path' (local changes or diverged history). Left as-is." |
| 86 | + } |
| 87 | + } |
| 88 | + } else { |
| 89 | + if (Test-Path $path) { |
| 90 | + throw "Cannot clone into '$path': it exists but is not a git repository. Remove it or choose a different -Root." |
| 91 | + } |
| 92 | + if ($PSCmdlet.ShouldProcess($repo.Url, "Clone into '$path'")) { |
| 93 | + Write-Verbose "Cloning $($repo.Url) into $path" |
| 94 | + git clone --quiet $repo.Url $path |
| 95 | + if ($LASTEXITCODE -ne 0) { |
| 96 | + throw "git clone failed for $($repo.Url) (exit $LASTEXITCODE). Check access and credentials (MSXOrg/memory is private)." |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + # Isolated identity: write repository-local config only. Git still reads |
| 102 | + # global and system config; the script never writes to them. |
| 103 | + if ($PSCmdlet.ShouldProcess($path, 'Set repository-local git identity')) { |
| 104 | + git -C $path config user.name $UserName |
| 105 | + if ($LASTEXITCODE -ne 0) { throw "git config user.name failed for '$path' (exit $LASTEXITCODE)." } |
| 106 | + git -C $path config user.email $UserEmail |
| 107 | + if ($LASTEXITCODE -ne 0) { throw "git config user.email failed for '$path' (exit $LASTEXITCODE)." } |
| 108 | + } |
| 109 | + |
| 110 | + [pscustomobject]@{ Repository = $repo.Name; Path = $path; Changes = $repo.Changes } |
| 111 | +} |
| 112 | + |
| 113 | +$results |
0 commit comments