Update clean-repo.ps1

This commit is contained in:
Markus Fleschutz 2022-02-28 10:57:50 +01:00
parent f9ef1c93c7
commit e1feb43253

View File

@ -1,14 +1,13 @@
<# <#
.SYNOPSIS .SYNOPSIS
Cleans a Git repository from untracked files (including submodules) Cleans a Git repository from untracked files
.DESCRIPTION .DESCRIPTION
This PowerShell script cleans a Git repository from untracked files (including submodules). This PowerShell script deletes all untracked files and folders in a Git repository (including submodules).
IMPORTANT NOTE: To be used with care! This cannot be undone!
.PARAMETER RepoDir .PARAMETER RepoDir
Specifies the path to the Git repository Specifies the path to the Git repository
.EXAMPLE .EXAMPLE
PS> ./clean-repo C:\MyRepo PS> ./clean-repo C:\MyRepo
🧹 Cleaning from untracked files...
cleaned Git repository 📂C:\MyRepo in 0 sec
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
@ -20,20 +19,21 @@ param([string]$RepoDir = "$PWD")
try { try {
$StopWatch = [system.diagnostics.stopwatch]::startNew() $StopWatch = [system.diagnostics.stopwatch]::startNew()
if (-not(test-path "$RepoDir" -pathType container)) { throw "Can't access directory: $RepoDir" } "⏳ Step 1/3: Checking requirements..."
if (-not(test-path "$RepoDir" -pathType container)) { throw "Can't access repository folder at: $RepoDir - maybe a typo or missing folder permissions?" }
$RepoDirName = (get-item "$RepoDir").Name
"🧹 Cleaning from untracked files..."
$Null = (git --version) $Null = (git --version)
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" } if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
"⏳ Step 2/3: Cleaning repository..."
& git -C "$RepoDir" clean -xfd -f # force + recurse into dirs + don't use the standard ignore rules & git -C "$RepoDir" clean -xfd -f # force + recurse into dirs + don't use the standard ignore rules
if ($lastExitCode -ne "0") { throw "'git clean -xfd -f' failed" } if ($lastExitCode -ne "0") { throw "'git clean -xfd -f' failed (exited with $lastExitCode)" }
"⏳ Step 3/3: Cleaning submodules..."
& git -C "$RepoDir" submodule foreach --recursive git clean -xfd -f & git -C "$RepoDir" submodule foreach --recursive git clean -xfd -f
if ($lastExitCode -ne "0") { throw "'git clean -xfd -f' in submodules failed" } if ($lastExitCode -ne "0") { throw "'git clean -xfd -f' in submodules failed (exited with $lastExitCode)" }
$RepoDirName = (get-item "$RepoDir").Name
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds [int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"✔️ cleaned Git repository 📂$RepoDirName in $Elapsed sec" "✔️ cleaned Git repository 📂$RepoDirName in $Elapsed sec"
exit 0 # success exit 0 # success