Last active
September 19, 2023 23:29
Revisions
-
manoj-choudhari-git revised this gist
Sep 19, 2023 . 1 changed file with 1 addition and 0 deletions.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 charactersOriginal file line number Diff line number Diff line change @@ -4,6 +4,7 @@ ################################################################ ## Parameter is_dry_run, defaulted to true to avoid deletion by mistake ## Invoke Command - "$ delete-stale-branches.ps1 -is_dry_run $true" param([Boolean]$is_dry_run = $true) ##--------------------------------------------------------------- -
manoj-choudhari-git revised this gist
Sep 19, 2023 . 1 changed file with 24 additions and 5 deletions.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 charactersOriginal file line number Diff line number Diff line change @@ -3,6 +3,8 @@ ## from the machine where the remote repository is cloned. ################################################################ ## Parameter is_dry_run, defaulted to true to avoid deletion by mistake param([Boolean]$is_dry_run = $true) ##--------------------------------------------------------------- ## List of Main Branches (Which Should Not Be Deleted) @@ -16,16 +18,24 @@ $defaultBranch = git symbolic-ref refs/remotes/origin/HEAD --short write-host "Default Branch In Repo: $defaultBranch" if ($excludedBranchNames -inotcontains $defaultBranch) { $excludedBranchNames += $defaultBranch } ##--------------------------------------------------------------- ## Prune Local References Of Remote Branches ##--------------------------------------------------------------- ## Just to avoid error due to non-existing remote branch ## prune local list git fetch --prune ##--------------------------------------------------------------- ## Cleanup of Stale Branches - No Commits since 3 Months ##--------------------------------------------------------------- ## List all remote branches $remoteBranches = git branch -r ## Explicitly Exclude HEAD $remoteBranchesFiltered = $remoteBranches | Where { $_ -notlike '*HEAD*'}; @@ -45,10 +55,19 @@ foreach ($branch in $remoteBranchesFiltered) ## If not found, delete the branch if (!$lastCommitDate) { Write-Host "Deleting branch $branchName" if ($is_dry_run -eq $false ) { ## --porcelain is used so that git command writes script friendly output git push origin -d $branchName --porcelain } } } } ##--------------------------------------------------------------- ## Prune Local References Again ##--------------------------------------------------------------- git fetch --prune -
manoj-choudhari-git revised this gist
Sep 19, 2023 . 1 changed file with 6 additions and 0 deletions.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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,9 @@ ################################################################ ## Note: This script should be executed ## from the machine where the remote repository is cloned. ################################################################ ##--------------------------------------------------------------- ## List of Main Branches (Which Should Not Be Deleted) ##--------------------------------------------------------------- -
manoj-choudhari-git created this gist
Sep 19, 2023 .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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ ##--------------------------------------------------------------- ## List of Main Branches (Which Should Not Be Deleted) ##--------------------------------------------------------------- ## Exclusion List - Add general default branch names $excludedBranchNames = @('main', 'master', 'develop') ## Just in case if there is any other name of default branch, add that too in exclusion list $defaultBranch = git symbolic-ref refs/remotes/origin/HEAD --short write-host "Default Branch In Repo: $defaultBranch" if ($excludedBranchNames -inotcontains $defaultBranch) { $excludedBranchNames.Add($defaultBranch); } ##--------------------------------------------------------------- ## Cleanup of Stale Branches - No Commits since 3 Months ##--------------------------------------------------------------- ## List all remote branches $remoteBranches = git branch -r ## Explicitly Exclude HEAD $remoteBranchesFiltered = $remoteBranches | Where { $_ -notlike '*HEAD*'}; ## Loop through each branch foreach ($branch in $remoteBranchesFiltered) { ## Remove the origin/ prefix $branchName = $branch.Trim().Substring(7) ## Continue deletion only if Branch name is not present in exclusion list if ( $excludedBranchNames -inotcontains $branchName) { ## Try to find at least 1 commit in last 3 months $lastCommitDate = git log -1 --since="3 months ago" --format="%cd" origin/$branchName ## If not found, delete the branch if (!$lastCommitDate) { Write-Host "Deleting branch $branchName" ## For dry run comment this line git branch -D origin/$branchName } } }