diff --git a/Data/scripts.csv b/Data/scripts.csv index 9386b9bb..28fe7ecd 100644 --- a/Data/scripts.csv +++ b/Data/scripts.csv @@ -36,6 +36,7 @@ check-windows-system-files.ps1, checks the validity of the Windows system files check-xml-file.ps1, checks the given XML file for validity cherry-picker.ps1, cherry-picks a Git commit into multiple branches clean-repo.ps1, cleans the current/given Git repository from untracked files (including submodules) +clean-repos.ps1, cleans all Git repositories under the current/given directory from untracked files (including submodules) clear-recycle-bin.ps1, removes the content of the recycle bin folder (can not be undo!) clone-repos.ps1, clones well-known Git repositories close-calculator.ps1, closes the calculator program gracefully diff --git a/README.md b/README.md index 79951e3d..81897bd9 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,7 @@ Mega Collection of PowerShell Scripts * [build-repos.ps1](Scripts/build-repos.ps1) - builds all Git repositories under the current/given directory * [cherry-picker.ps1](Scripts/cherry-picker.ps1) - cherry-picks a Git commit into multiple branches * [clean-repo.ps1](Scripts/clean-repo.ps1) - cleans the current/given Git repository from untracked files (including submodules) +* [clean-repos.ps1](Scripts/clean-repos.ps1) - cleans all Git repositories under the current/given directory from untracked files (including submodules) * [clone-repos.ps1](Scripts/clone-repos.ps1) - clones well-known Git repositories * [configure-git.ps1](Scripts/configure-git.ps1) - sets up the Git user configuration * [create-branch.ps1](Scripts/create-branch.ps1) - creates a new branch in the current/given Git repository diff --git a/Scripts/clean-repos.ps1 b/Scripts/clean-repos.ps1 new file mode 100755 index 00000000..72a4314a --- /dev/null +++ b/Scripts/clean-repos.ps1 @@ -0,0 +1,40 @@ +<# +.SYNTAX clean-repos.ps1 [<parent-dir>] +.DESCRIPTION cleans all Git repositories under the current/given directory from untracked files (including submodules) +.LINK https://github.com/fleschutz/PowerShell +.NOTES Author: Markus Fleschutz / License: CC0 +#> + +param($ParentDir = "$PWD") + +try { + $StopWatch = [system.diagnostics.stopwatch]::startNew() + + if (-not(test-path "$ParentDir" -pathType container)) { throw "Can't access directory: $ParentDir" } + set-location $ParentDir + + $Null = (git --version) + if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" } + + [int]$Count = 0 + get-childItem "$ParentDir" -attributes Directory | foreach-object { + "Cleaning Git repository 📂$($_.Name) from untracked files ..." + set-location $_.FullName + + & git clean -fdx # force + recurse into dirs + don't use the standard ignore rules + if ($lastExitCode -ne "0") { throw "'git clean -fdx' failed" } + + & git submodule foreach --recursive git clean -fdx + if ($lastExitCode -ne "0") { throw "'git clean -fdx' in submodules failed" } + + set-location .. + $Count++ + } + + [int]$Elapsed = $StopWatch.Elapsed.TotalSeconds + write-host -foregroundColor green "✔️ cleaned $Count Git repositories at $ParentDir in $Elapsed sec." + exit 0 +} catch { + write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +}