-
Notifications
You must be signed in to change notification settings - Fork 0
Merge branch 'dev/lomazov' of https://github.com/Daniel-Lomazov/SelfSnap into dev/lomazov #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4999682
chore: expand cleanup script to cover all cache/artifact types
AtLomDan 283c146
fix: strip git-quoted path delimiters in cleanup script
AtLomDan 72f8714
fix: address review comments in cleanup_repo_artifacts.ps1
Copilot ae3f186
test: update repo hygiene tests for new cleanup_repo_artifacts script
AtLomDan fd6a0aa
Merge branch 'dev/lomazov' of https://github.com/Daniel-Lomazov/SelfS…
AtLomDan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,248 @@ | ||
| param( | ||
| [switch]$ListOnly, | ||
| [switch]$RepairAcl, | ||
| [string[]]$Exclude = @(), | ||
| [switch]$RelaunchedElevated | ||
| ) | ||
|
|
||
| $ErrorActionPreference = "Stop" | ||
| $PSNativeCommandUseErrorActionPreference = $true | ||
|
|
||
| $repoRoot = (Resolve-Path (Join-Path $PSScriptRoot "..")).Path | ||
|
|
||
| function Test-Administrator { | ||
| $identity = [Security.Principal.WindowsIdentity]::GetCurrent() | ||
| $principal = [Security.Principal.WindowsPrincipal]::new($identity) | ||
| return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) | ||
| } | ||
|
|
||
| function Get-RelaunchArgumentList { | ||
| $arguments = @( | ||
| "-NoProfile", | ||
| "-ExecutionPolicy", "Bypass", | ||
| "-File", $PSCommandPath | ||
| ) | ||
| if ($RepairAcl) { $arguments += "-RepairAcl" } | ||
| if ($ListOnly) { $arguments += "-ListOnly" } | ||
| if ($Exclude.Count -gt 0) { $arguments += "-Exclude"; $arguments += $Exclude } | ||
| $arguments += "-RelaunchedElevated" | ||
| return $arguments | ||
| } | ||
|
|
||
| # ── What this script removes ───────────────────────────────────────────────── | ||
| # | ||
| # Via git status (ignored + untracked): | ||
| # .pytest_cache/, .pytest_tmp/, .pytest-work/, pytest-cache-files-*/ | ||
| # .ruff_cache/, .mypy_cache/, .hypothesis/ | ||
| # .coverage, coverage.xml, cov_annotate/ | ||
| # __pycache__/, *.pyc, *.pyo, *.pyd, pytest*.out | ||
| # build/, dist/, *.egg-info/, .uv-cache/, tmp/, artifacts/ | ||
| # and any other git-ignored or untracked item | ||
| # | ||
| # Via explicit sweep (catches files that may be committed/tracked by git): | ||
| # cov_annotate/, .coverage, coverage.xml | ||
| # | ||
| # Via recursive directory sweep under the repo tree: | ||
| # __pycache__, .pytest_cache, .pytest_tmp, .pytest-work | ||
| # .ruff_cache, .mypy_cache, .hypothesis, .uv-cache, *.egg-info | ||
| # | ||
| # Via recursive file sweep: | ||
| # *.pyc, *.pyo, *.pyd, pytest*.out | ||
| # | ||
| # Protected (never removed even if gitignored): | ||
| # .venv/, venv/, .git/ | ||
| # | ||
| # ───────────────────────────────────────────────────────────────────────────── | ||
|
|
||
| # Directories that are always protected — never removed even if gitignored. | ||
| $ProtectedTopLevel = [System.Collections.Generic.HashSet[string]]::new( | ||
| [string[]]@('.venv', 'venv', '.git'), | ||
| [StringComparer]::OrdinalIgnoreCase | ||
| ) | ||
|
|
||
| # Explicit paths relative to repo root that are targeted even when tracked by git. | ||
| $ExplicitPaths = @( | ||
| 'cov_annotate' | ||
| '.coverage' | ||
| 'coverage.xml' | ||
| ) | ||
|
|
||
| # Directory names removed recursively anywhere in the tree. | ||
| $RecursiveDirNames = @( | ||
| '__pycache__' | ||
| '.pytest_cache' | ||
| '.pytest_tmp' | ||
| '.pytest-work' | ||
| '.ruff_cache' | ||
| '.mypy_cache' | ||
| '.hypothesis' | ||
| '.uv-cache' | ||
| ) | ||
|
|
||
| # File patterns removed recursively anywhere in the tree. | ||
| $RecursiveFilePatterns = @( | ||
| '*.pyc' | ||
| '*.pyo' | ||
| '*.pyd' | ||
| 'pytest*.out' | ||
| ) | ||
|
|
||
| # ── Candidate collection ────────────────────────────────────────────────────── | ||
|
|
||
| function Get-ArtifactCandidates { | ||
| param([string]$RepoRoot, [string[]]$ExcludeList) | ||
|
|
||
| $seen = [System.Collections.Generic.HashSet[string]]::new([StringComparer]::OrdinalIgnoreCase) | ||
| $candidates = [System.Collections.Generic.List[object]]::new() | ||
|
|
||
| function Add-Candidate { | ||
| param([string]$FullPath, [string]$Label) | ||
| if (-not (Test-Path -LiteralPath $FullPath)) { return } | ||
| $relPath = $FullPath.Substring($RepoRoot.Length).TrimStart('\').TrimEnd('\') | ||
| if ([string]::IsNullOrWhiteSpace($relPath)) { return } | ||
| $topLevel = $relPath.Split('\')[0] | ||
| if ($ProtectedTopLevel.Contains($topLevel)) { return } | ||
| if ($ExcludeList -contains $relPath) { return } | ||
| if (-not $seen.Add($relPath)) { return } | ||
| $item = Get-Item -LiteralPath $FullPath -Force | ||
| $candidates.Add([pscustomobject]@{ | ||
| RelativePath = $relPath | ||
| FullPath = $item.FullName | ||
| IsDirectory = $item.PSIsContainer | ||
| Label = $Label | ||
| }) | ||
| } | ||
|
|
||
| # Pass 1: git status — ignored (!!) and untracked (??) | ||
| $gitLines = & git -C $RepoRoot status --porcelain=v1 --ignored --untracked-files=all 2>&1 | ||
| foreach ($line in $gitLines) { | ||
| if ($line.Length -lt 4) { continue } | ||
| $code = $line.Substring(0, 2) | ||
| if ($code -ne '!!' -and $code -ne '??') { continue } | ||
| # git quotes paths that contain spaces or special characters — strip those quotes | ||
| $rel = $line.Substring(3).Trim().Trim('"').TrimEnd('/').Replace('/', '\') | ||
| $lbl = if ($code -eq '!!') { 'git-ignored' } else { 'untracked' } | ||
| Add-Candidate -FullPath (Join-Path $RepoRoot $rel) -Label $lbl | ||
| } | ||
|
|
||
| # Pass 2: explicit known artifacts (catches committed files like coverage.xml) | ||
| foreach ($rel in $ExplicitPaths) { | ||
| Add-Candidate -FullPath (Join-Path $RepoRoot $rel) -Label 'explicit' | ||
| } | ||
|
|
||
| # Pass 3: recursive directory sweep (skip protected top-level roots) | ||
| $searchRoots = @( | ||
| Get-ChildItem -Path $RepoRoot -Directory -Force -ErrorAction SilentlyContinue | | ||
| Where-Object { -not $ProtectedTopLevel.Contains($_.Name) } | ||
| ) | ||
| foreach ($dirName in $RecursiveDirNames) { | ||
| foreach ($root in $searchRoots) { | ||
| Get-ChildItem -Path $root.FullName -Filter $dirName -Recurse -Directory -Force -ErrorAction SilentlyContinue | | ||
| ForEach-Object { Add-Candidate -FullPath $_.FullName -Label 'recursive-dir' } | ||
| } | ||
| # searchRoots only recurses *inside* top-level dirs, so a matching dir | ||
| # sitting directly at repo root (e.g., $RepoRoot\__pycache__) would be | ||
| # missed; catch it here as a safety net. | ||
| Add-Candidate -FullPath (Join-Path $RepoRoot $dirName) -Label 'recursive-dir' | ||
| } | ||
|
|
||
| # Pass 4: recursive file sweep (skip protected top-level roots) | ||
| foreach ($pattern in $RecursiveFilePatterns) { | ||
| foreach ($root in $searchRoots) { | ||
| Get-ChildItem -Path $root.FullName -Filter $pattern -Recurse -File -Force -ErrorAction SilentlyContinue | | ||
| ForEach-Object { Add-Candidate -FullPath $_.FullName -Label 'recursive-file' } | ||
| } | ||
| Get-ChildItem -Path $RepoRoot -Filter $pattern -File -Force -ErrorAction SilentlyContinue | | ||
| ForEach-Object { Add-Candidate -FullPath $_.FullName -Label 'recursive-file' } | ||
| } | ||
|
|
||
| # Sort deepest paths first so children are removed before parents. | ||
| return @($candidates | Sort-Object @{ Expression = { ($_.RelativePath -split '\\').Count }; Descending = $true }, RelativePath) | ||
| } | ||
|
|
||
| # ── Removal ─────────────────────────────────────────────────────────────────── | ||
|
|
||
| function Remove-Artifact { | ||
| param([pscustomobject]$Item) | ||
|
|
||
| if (-not (Test-Path -LiteralPath $Item.FullPath)) { | ||
| return [pscustomobject]@{ Path = $Item.RelativePath; Status = 'already-absent'; Note = '' } | ||
| } | ||
|
|
||
| try { | ||
| Remove-Item -LiteralPath $Item.FullPath -Recurse -Force -ErrorAction Stop | ||
| return [pscustomobject]@{ Path = $Item.RelativePath; Status = 'removed'; Note = '' } | ||
| } | ||
| catch { | ||
| if (-not $RepairAcl) { | ||
| return [pscustomobject]@{ Path = $Item.RelativePath; Status = 'failed'; Note = $_.Exception.Message } | ||
| } | ||
| if (-not (Test-Administrator)) { | ||
| return [pscustomobject]@{ Path = $Item.RelativePath; Status = 'failed'; Note = 'ACL repair requires an elevated session.' } | ||
| } | ||
| try { | ||
| $null = takeown /F $Item.FullPath /R /D Y 2>&1 | ||
| $null = icacls $Item.FullPath /reset /T 2>&1 | ||
| Remove-Item -LiteralPath $Item.FullPath -Recurse -Force -ErrorAction Stop | ||
| return [pscustomobject]@{ Path = $Item.RelativePath; Status = 'removed'; Note = 'ACL repaired' } | ||
| } | ||
| catch { | ||
| return [pscustomobject]@{ Path = $Item.RelativePath; Status = 'failed'; Note = $_.Exception.Message } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # ── Main ────────────────────────────────────────────────────────────────────── | ||
|
|
||
| $candidates = Get-ArtifactCandidates -RepoRoot $repoRoot -ExcludeList $Exclude | ||
|
|
||
| if ($candidates.Count -eq 0) { | ||
| Write-Host "No artifact candidates found under $repoRoot." | ||
| exit 0 | ||
| } | ||
|
|
||
| Write-Host "Artifact candidates under ${repoRoot}:" | ||
| $candidates | | ||
| Select-Object Label, | ||
| @{ Name = 'Kind'; Expression = { if ($_.IsDirectory) { 'dir' } else { 'file' } } }, | ||
| RelativePath | | ||
| Format-Table -AutoSize | | ||
| Out-String | | ||
| Write-Host | ||
|
|
||
| if ($ListOnly) { | ||
| exit 0 | ||
| } | ||
|
|
||
| # ── Elevation for ACL repair ────────────────────────────────────────────────── | ||
|
|
||
| if ($RepairAcl -and -not (Test-Administrator) -and -not $RelaunchedElevated) { | ||
| $relaunchArgs = Get-RelaunchArgumentList | ||
| Write-Host "Relaunching with elevation for ACL repair under $repoRoot..." | ||
| try { | ||
| $process = Start-Process -FilePath "powershell.exe" -Verb RunAs -WorkingDirectory $repoRoot -ArgumentList $relaunchArgs -Wait -PassThru | ||
| } | ||
| catch { | ||
| Write-Warning "Elevation was not completed. Cleanup was not performed." | ||
| exit 2 | ||
| } | ||
| exit $process.ExitCode | ||
| } | ||
|
|
||
| # ── Removal ─────────────────────────────────────────────────────────────────── | ||
|
|
||
| $results = foreach ($c in $candidates) { Remove-Artifact -Item $c } | ||
|
|
||
| $results | Format-Table Path, Status, Note -AutoSize | Out-String | Write-Host | ||
|
|
||
| $failed = @($results | Where-Object { $_.Status -eq 'failed' }) | ||
| if ($failed.Count -gt 0) { | ||
| Write-Warning "$($failed.Count) item(s) could not be removed. Run with -RepairAcl to attempt ACL repair." | ||
| if ($RepairAcl) { | ||
| exit 2 | ||
| } | ||
| exit 1 | ||
| } | ||
|
|
||
| Write-Host "Repo artifact cleanup complete." | ||
| exit 0 | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When -RepairAcl is enabled, the script attempts takeown/icacls but doesn't check for admin rights or relaunch elevated. In a non-elevated session this will typically fail and surface only the raw exception message, which is less actionable than explicitly detecting the non-admin case (and optionally relaunching with -Verb RunAs, like the earlier cleanup_pytest_artifacts.ps1 behavior).