2024-10-01 15:11:03 +02:00
|
|
|
|
<#
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.SYNOPSIS
|
2023-03-28 08:43:16 +02:00
|
|
|
|
Cleans a repo
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.DESCRIPTION
|
2023-03-28 08:43:16 +02:00
|
|
|
|
This PowerShell script deletes all untracked files and folders in a local Git repository (including submodules).
|
2022-04-13 10:47:53 +02:00
|
|
|
|
NOTE: To be used with care! This cannot be undone!
|
2024-05-22 07:30:39 +02:00
|
|
|
|
.PARAMETER path
|
|
|
|
|
Specifies the file path to the local Git repository (current working directory by default)
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.EXAMPLE
|
2024-05-22 07:30:39 +02:00
|
|
|
|
PS> ./clean-repo.ps1 C:\Repos\rust
|
|
|
|
|
⏳ (1/4) Searching for Git executable... git version 2.45.0
|
2024-06-12 13:41:51 +02:00
|
|
|
|
⏳ (2/4) Checking local repository... C:\Repos\rust
|
2023-08-06 21:35:36 +02:00
|
|
|
|
⏳ (3/4) Removing untracked files in repository...
|
|
|
|
|
⏳ (4/4) Removing untracked files in submodules...
|
2024-09-24 20:53:46 +02:00
|
|
|
|
✅ Cleaned up 📂rust repo in 2s.
|
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
|
|
|
|
#>
|
|
|
|
|
|
2024-05-22 07:30:39 +02:00
|
|
|
|
param([string]$path = "$PWD")
|
2021-02-28 18:26:20 +01:00
|
|
|
|
|
2021-02-15 16:54:38 +01:00
|
|
|
|
try {
|
2024-03-05 14:50:09 +01:00
|
|
|
|
$stopWatch = [system.diagnostics.stopwatch]::startNew()
|
2021-05-19 07:33:57 +02:00
|
|
|
|
|
2023-06-21 16:35:13 +02:00
|
|
|
|
Write-Host "⏳ (1/4) Searching for Git executable... " -noNewline
|
2022-08-11 11:56:14 +02:00
|
|
|
|
& 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
|
|
|
|
|
2024-06-12 13:41:51 +02:00
|
|
|
|
"⏳ (2/4) Checking local repository... $path"
|
2024-05-22 07:30:39 +02:00
|
|
|
|
if (-not(Test-Path "$path" -pathType container)) { throw "Can't access repo folder '$path' - maybe a typo or missing folder permissions?" }
|
|
|
|
|
$repoName = (Get-Item "$path").Name
|
2023-04-06 09:45:48 +02:00
|
|
|
|
|
2023-06-21 16:35:13 +02:00
|
|
|
|
"⏳ (3/4) Removing untracked files in repository..."
|
2024-05-22 07:30:39 +02:00
|
|
|
|
& git -C "$path" clean -xfd -f # to delete all untracked files in the main repo
|
2022-04-27 11:11:20 +02:00
|
|
|
|
if ($lastExitCode -ne "0") {
|
2023-03-01 10:52:37 +01:00
|
|
|
|
Write-Warning "'git clean' failed with exit code $lastExitCode, retrying once..."
|
2024-05-22 07:30:39 +02:00
|
|
|
|
& git -C "$path" clean -xfd -f
|
2022-04-27 11:11:20 +02:00
|
|
|
|
if ($lastExitCode -ne "0") { throw "'git clean' failed with exit code $lastExitCode" }
|
|
|
|
|
}
|
2021-02-15 16:54:38 +01:00
|
|
|
|
|
2023-06-21 16:35:13 +02:00
|
|
|
|
"⏳ (4/4) Removing untracked files in submodules..."
|
2024-05-22 07:30:39 +02:00
|
|
|
|
& git -C "$path" submodule foreach --recursive git clean -xfd -f # to delete all untracked files in the submodules
|
2022-04-04 16:15:29 +02:00
|
|
|
|
if ($lastExitCode -ne "0") { throw "'git clean' in the submodules failed with exit code $lastExitCode" }
|
2021-02-15 16:54:38 +01:00
|
|
|
|
|
2024-03-05 14:50:09 +01:00
|
|
|
|
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
|
2024-09-24 20:53:46 +02:00
|
|
|
|
"✅ Cleaned up 📂$repoName repo in $($elapsed)s."
|
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
|
2023-07-30 21:31:37 +02:00
|
|
|
|
}
|