Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,14 @@ jobs:
throw 'converge.ps1 must treat an unwritable status report as a failed run.'
}

# scripts/test-sync-dotfiles.ps1 exercises the Windows counterpart of
# scripts/sync-dotfiles.sh against a real git fixture: a checkout left on
# a release branch has to converge onto main, and a dirty or non-git path
# has to be refused.
- name: Validate dotfiles sync convergence
shell: pwsh
run: ./scripts/test-sync-dotfiles.ps1

- name: Validate best-effort Windows desktop apps
shell: pwsh
run: |
Expand Down
24 changes: 21 additions & 3 deletions platforms/windows-11/converge.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,27 @@ function Invoke-DotfilesSync {
return
}
$fetch = Invoke-Git -Dir $DotfilesDir -GitArgs @('fetch', '--quiet', 'origin', 'main')
$merge = Invoke-Git -Dir $DotfilesDir -GitArgs @('merge', '--quiet', '--ff-only', 'origin/main')
if ($fetch.Code -ne 0 -or $merge.Code -ne 0) {
Add-Step 'dotfiles-sync' 'error' 'fetch or fast-forward of dotfiles origin/main failed'
if ($fetch.Code -ne 0) {
Add-Step 'dotfiles-sync' 'error' 'fetch of dotfiles origin/main failed'
return
}
# The sync itself lives in scripts/sync-dotfiles.ps1, the Windows
# counterpart of scripts/sync-dotfiles.sh, so the machine-global dotfiles
# checkout converges the same way on every OS. In particular the checkout
# is moved onto main before it is fast-forwarded: a checkout left on a
# release branch cannot fast-forward to origin/main and would fail on
# every run (rebuildup/pc-setup#283).
$syncScript = Join-Path (Join-Path $DeployDir 'scripts') 'sync-dotfiles.ps1'
if (-not (Test-Path -LiteralPath $syncScript)) {
Add-Step 'dotfiles-sync' 'skipped' 'no sync-dotfiles.ps1 in the deploy checkout'
return
}
& powershell.exe -NoProfile -ExecutionPolicy Bypass -File $syncScript `
-DotfilesDir $DotfilesDir -RepoUrl $DotfilesRepoUrl -Ref 'main' *>&1 |
ForEach-Object { Write-Log ([string]$_) }
$syncCode = $LASTEXITCODE
if ($syncCode -ne 0) {
Add-Step 'dotfiles-sync' 'error' ('sync-dotfiles.ps1 exited ' + $syncCode + '; see ' + $script:LogFile)
return
}
Add-Step 'dotfiles-sync' 'ok' ('fast-forwarded ' + $DotfilesDir)
Expand Down
9 changes: 9 additions & 0 deletions scripts/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -618,4 +618,13 @@ grep -Fq 'cannot modify the parent shell PATH' bootstrap.sh
grep -Fq 'source ~/.bashrc' bootstrap.sh
grep -Fq 'bootstrap.sh | bash && source ~/.bashrc' platforms/ubuntu-wsl/README.md

printf 'Checking the Windows dotfiles sync rules...\n'
if command -v pwsh >/dev/null 2>&1; then
# scripts/sync-dotfiles.ps1 is the Windows counterpart of
# scripts/sync-dotfiles.sh. A machine-global dotfiles checkout that was left
# on a release branch has to be moved onto main before it is fast-forwarded,
# and a dirty or non-git path has to be refused (rebuildup/pc-setup#283).
pwsh -NoProfile -NonInteractive -File scripts/test-sync-dotfiles.ps1
fi

printf 'All repository checks passed.\n'
104 changes: 104 additions & 0 deletions scripts/sync-dotfiles.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Converge the machine-global dotfiles checkout onto the target ref.
#
# Windows counterpart of scripts/sync-dotfiles.sh. The two have to behave the
# same way, because the same checkout is the machine-global dotfiles state on
# every OS:
#
# * the checkout is cloned into ~/.dotfiles when it does not exist yet;
# * a path that exists but is not a git checkout is never overwritten;
# * an unexpected origin is refused;
# * a checkout with local changes is refused, never reset or stashed;
# * the checkout is moved onto the target ref (main) before it is
# fast-forwarded, so a checkout that was left on a release branch still
# converges (rebuildup/pc-setup#283).
#
# Windows PowerShell 5.1 compatible.
[CmdletBinding()]
param(
[string]$DotfilesDir = $(if ($env:DOTFILES_DIR) { $env:DOTFILES_DIR } else { Join-Path $HOME '.dotfiles' }),
[string]$RepoUrl = $(if ($env:DOTFILES_REPO_URL) { $env:DOTFILES_REPO_URL } else { 'https://github.com/rebuildup/dotfiles.git' }),
[string]$Ref = $(if ($env:DOTFILES_REF) { $env:DOTFILES_REF } else { 'main' })
)

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

function Stop-WithError {
param([Parameter(Mandatory)][string]$Message)
[Console]::Error.WriteLine($Message)
exit 1
}

function Invoke-Git {
param(
[Parameter(Mandatory)][string]$Dir,
[Parameter(Mandatory)][string[]]$GitArgs
)
$output = & git -C $Dir @GitArgs 2>&1
$code = $LASTEXITCODE
foreach ($line in @($output)) { Write-Host ([string]$line) }
return $code
}

if (-not (Test-Path -LiteralPath $DotfilesDir)) {
Write-Host "cloning dotfiles ($Ref) -> $DotfilesDir"
$parent = Split-Path -Parent $DotfilesDir
if ($parent -and -not (Test-Path -LiteralPath $parent)) {
New-Item -ItemType Directory -Path $parent -Force | Out-Null
}
& git clone --branch $Ref --single-branch $RepoUrl $DotfilesDir
if ($LASTEXITCODE -ne 0) { Stop-WithError "dotfiles clone failed with exit code $LASTEXITCODE" }
exit 0
}

if (-not (Test-Path -LiteralPath (Join-Path $DotfilesDir '.git'))) {
Stop-WithError "refusing to overwrite non-git dotfiles path: $DotfilesDir"
}

$currentOrigin = (& git -C $DotfilesDir remote get-url origin 2>$null)
if ($LASTEXITCODE -ne 0 -or $currentOrigin -ne $RepoUrl) {
Stop-WithError "existing dotfiles checkout has unexpected origin: $currentOrigin"
}

$status = (& git -C $DotfilesDir status --porcelain 2>$null)
if ($LASTEXITCODE -ne 0) {
Stop-WithError "git status failed in $DotfilesDir"
}
if (@($status | Where-Object { $_ -ne '' }).Count -gt 0) {
Write-Host "existing dotfiles checkout has local changes; refusing automatic update: $DotfilesDir"
& git -C $DotfilesDir status --short
exit 1
}

Write-Host "updating existing dotfiles checkout to $Ref"

$remoteTrackingRef = "refs/remotes/origin/$Ref"
$fetchRefSpec = "+refs/heads/${Ref}:$remoteTrackingRef"
$configuredFetchSpecs = @(& git -C $DotfilesDir config --get-all remote.origin.fetch)
if ($configuredFetchSpecs -notcontains $fetchRefSpec) {
& git -C $DotfilesDir config --add remote.origin.fetch $fetchRefSpec
if ($LASTEXITCODE -ne 0) { Stop-WithError "git remote fetch configuration failed with exit code $LASTEXITCODE" }
}

& git -C $DotfilesDir fetch origin $fetchRefSpec
if ($LASTEXITCODE -ne 0) { Stop-WithError "git fetch failed with exit code $LASTEXITCODE" }

# Move onto the target ref before merging. A checkout that was left on a
# release branch cannot fast-forward to origin/main, and without the switch it
# would fail on every run.
& git -C $DotfilesDir show-ref --verify --quiet "refs/heads/$Ref"
if ($LASTEXITCODE -eq 0) {
& git -C $DotfilesDir switch $Ref
}
else {
& git -C $DotfilesDir switch --track -c $Ref "origin/$Ref"
}
if ($LASTEXITCODE -ne 0) { Stop-WithError "git switch to $Ref failed with exit code $LASTEXITCODE" }

& git -C $DotfilesDir merge --ff-only "origin/$Ref"
if ($LASTEXITCODE -ne 0) { Stop-WithError "git fast-forward failed with exit code $LASTEXITCODE" }

$linkScript = Join-Path $DotfilesDir 'script\link.ps1'
if (-not (Test-Path -LiteralPath $linkScript)) {
Stop-WithError "dotfiles link script is unavailable after sync: $linkScript"
}
104 changes: 104 additions & 0 deletions scripts/test-sync-dotfiles.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

# Behavioural check for scripts/sync-dotfiles.ps1, the Windows counterpart of
# scripts/sync-dotfiles.sh. The rule that matters is the branch switch: a
# machine-global dotfiles checkout that was left on a release branch has to be
# moved onto main before it is fast-forwarded, otherwise every converge run
# fails and the dotfiles state is never adopted (rebuildup/pc-setup#283).

$repo = Resolve-Path (Join-Path $PSScriptRoot '..')
$syncScript = Join-Path $repo 'scripts\sync-dotfiles.ps1'

# The sync reports a refusal on stderr. Windows PowerShell turns a native
# command's stderr into a terminating error while the preference is Stop, so
# the refusal path is exercised with the preference relaxed and judged by the
# exit code instead.
function Invoke-Sync {
param(
[Parameter(Mandatory)][string]$DotfilesDir,
[Parameter(Mandatory)][string]$RepoUrl
)
$previousPreference = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
try {
& powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File $syncScript `
-DotfilesDir $DotfilesDir -RepoUrl $RepoUrl -Ref 'main' *> $null
return $LASTEXITCODE
}
finally {
$ErrorActionPreference = $previousPreference
}
}

$root = Join-Path ([System.IO.Path]::GetTempPath()) "pc-setup-sync-dotfiles-$([guid]::NewGuid())"
$origin = Join-Path $root 'origin'
$checkout = Join-Path $root 'dotfiles'

try {
New-Item -ItemType Directory -Force -Path $root | Out-Null

# A repository that looks like the dotfiles checkout: the link script has
# to exist, because the sync refuses a checkout without it.
$seed = Join-Path $root 'seed'
New-Item -ItemType Directory -Force -Path (Join-Path $seed 'script') | Out-Null
Set-Content -LiteralPath (Join-Path $seed 'script\link.ps1') -Value '# link'
& git -C $seed init --quiet --initial-branch=main
& git -C $seed config user.email 'ci@example.invalid'
& git -C $seed config user.name 'CI'
& git -C $seed add -A
& git -C $seed commit --quiet -m 'seed'
& git -C $seed clone --quiet --bare . $origin
if ($LASTEXITCODE -ne 0) { throw 'could not create the origin repository' }

# The origin URL has to be comparable with what git stores. A local path is
# stored with forward slashes, so the test uses the same spelling.
$originUrl = ($origin -replace '\\', '/')

& git clone --quiet --branch main --single-branch $originUrl $checkout
if ($LASTEXITCODE -ne 0) { throw 'could not clone the origin repository' }

# A release branch that the checkout is left on.
& git -C $checkout checkout --quiet -b 'release/res-integration'
Set-Content -LiteralPath (Join-Path $checkout 'release.txt') -Value 'release'
& git -C $checkout add -A
& git -C $checkout commit --quiet -m 'release'
& git -C $checkout push --quiet origin 'release/res-integration'
if ($LASTEXITCODE -ne 0) { throw 'could not push the release branch' }

& powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File $syncScript `
-DotfilesDir $checkout -RepoUrl $originUrl -Ref 'main' | Out-Null
if ($LASTEXITCODE -ne 0) { throw "sync-dotfiles.ps1 exited $LASTEXITCODE" }

$branch = (& git -C $checkout symbolic-ref --quiet --short HEAD).Trim()
if ($branch -ne 'main') { throw "the checkout is on '$branch', expected main" }

$local = (& git -C $checkout rev-parse HEAD).Trim()
$remote = (& git -C $checkout rev-parse 'origin/main').Trim()
if ($local -ne $remote) { throw 'the checkout was not fast-forwarded to origin/main' }
Write-Host 'ok a checkout left on a release branch converges onto main'

# A checkout with local changes is refused, and the change is preserved.
$marker = Join-Path $checkout 'local-change.txt'
Set-Content -LiteralPath $marker -Value 'local'
$dirtyCode = Invoke-Sync -DotfilesDir $checkout -RepoUrl $originUrl
if ($dirtyCode -eq 0) { throw 'a dirty checkout must not be updated automatically' }
if (-not (Test-Path -LiteralPath $marker)) { throw 'the local change was discarded' }
Write-Host 'ok a dirty checkout is refused and its changes are preserved'

# A checkout that is not a git repository is never overwritten.
$notARepo = Join-Path $root 'not-a-repo'
New-Item -ItemType Directory -Force -Path $notARepo | Out-Null
Set-Content -LiteralPath (Join-Path $notARepo 'keep.txt') -Value 'keep'
$notARepoCode = Invoke-Sync -DotfilesDir $notARepo -RepoUrl $originUrl
if ($notARepoCode -eq 0) { throw 'a non-git path must be refused' }
if (-not (Test-Path -LiteralPath (Join-Path $notARepo 'keep.txt'))) {
throw 'the non-git path was overwritten'
}
Write-Host 'ok a non-git path is refused and left untouched'
}
finally {
Remove-Item -LiteralPath $root -Recurse -Force -ErrorAction SilentlyContinue
}

Write-Host 'ok dotfiles sync rules verified'
Loading