2021-09-27 10:38:12 +02:00
|
|
|
|
<#
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.SYNOPSIS
|
2022-04-26 14:26:04 +02:00
|
|
|
|
Clean a repository
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.DESCRIPTION
|
2022-02-28 10:57:50 +01:00
|
|
|
|
This PowerShell script deletes all untracked files and folders in a Git repository (including submodules).
|
2022-04-13 10:47:53 +02:00
|
|
|
|
NOTE: To be used with care! This cannot be undone!
|
2021-10-16 16:50:10 +02:00
|
|
|
|
.PARAMETER RepoDir
|
|
|
|
|
Specifies the path to the Git repository
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.EXAMPLE
|
2021-09-24 17:19:49 +02:00
|
|
|
|
PS> ./clean-repo C:\MyRepo
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.LINK
|
|
|
|
|
https://github.com/fleschutz/PowerShell
|
2022-01-29 12:47:46 +01:00
|
|
|
|
.NOTES
|
2022-03-28 16:25:24 +02:00
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
2021-02-15 16:54:38 +01:00
|
|
|
|
#>
|
|
|
|
|
|
2021-07-15 15:51:22 +02:00
|
|
|
|
param([string]$RepoDir = "$PWD")
|
2021-02-28 18:26:20 +01:00
|
|
|
|
|
2021-02-15 16:54:38 +01:00
|
|
|
|
try {
|
2021-05-19 07:33:57 +02:00
|
|
|
|
$StopWatch = [system.diagnostics.stopwatch]::startNew()
|
|
|
|
|
|
2022-02-28 10:57:50 +01:00
|
|
|
|
"⏳ Step 1/3: Checking requirements..."
|
2022-04-27 11:11:20 +02:00
|
|
|
|
if (-not(test-path "$RepoDir" -pathType container)) { throw "Can't access folder '$RepoDir' - maybe a typo or missing folder permissions?" }
|
2021-04-20 08:28:29 +02:00
|
|
|
|
|
2022-04-26 16:04:21 +02:00
|
|
|
|
$null = (git --version)
|
2021-03-24 11:45:30 +01:00
|
|
|
|
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
|
2022-02-28 10:57:50 +01:00
|
|
|
|
|
2022-04-26 14:26:04 +02:00
|
|
|
|
"⏳ Step 2/3: Cleaning repository..."
|
2022-04-04 16:15:29 +02:00
|
|
|
|
& git -C "$RepoDir" clean -xfd -f # to delete all untracked files in the main repo
|
2022-04-27 11:11:20 +02:00
|
|
|
|
if ($lastExitCode -ne "0") {
|
|
|
|
|
"'git clean' failed with exit code $lastExitCode, retrying once..."
|
|
|
|
|
& git -C "$RepoDir" clean -xfd -f
|
|
|
|
|
if ($lastExitCode -ne "0") { throw "'git clean' failed with exit code $lastExitCode" }
|
|
|
|
|
}
|
2021-02-15 16:54:38 +01:00
|
|
|
|
|
2022-02-28 10:57:50 +01:00
|
|
|
|
"⏳ Step 3/3: Cleaning submodules..."
|
2022-04-04 16:15:29 +02:00
|
|
|
|
& git -C "$RepoDir" submodule foreach --recursive git clean -xfd -f # to delete all untracked files in the submodules
|
|
|
|
|
if ($lastExitCode -ne "0") { throw "'git clean' in the submodules failed with exit code $lastExitCode" }
|
2021-02-15 16:54:38 +01:00
|
|
|
|
|
2022-03-28 16:25:24 +02:00
|
|
|
|
$RepoDirName = (Get-Item "$RepoDir").Name
|
2021-05-19 07:33:57 +02:00
|
|
|
|
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
|
2022-04-27 11:16:41 +02:00
|
|
|
|
"✔️ cleaned 📂$RepoDirName repo in $Elapsed sec"
|
2021-09-27 10:09:45 +02:00
|
|
|
|
exit 0 # success
|
2021-02-15 16:54:38 +01:00
|
|
|
|
} catch {
|
2022-04-13 12:06:32 +02:00
|
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
2021-02-15 16:54:38 +01:00
|
|
|
|
exit 1
|
|
|
|
|
}
|