Last active
April 9, 2021 23:51
-
-
Save dougbu/26ad82aec0a6d75f2063b100b63ceb9c to your computer and use it in GitHub Desktop.
Find all git repos under current location and report on their branches
This file contains 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 characters
# This script will find all subdirectories in the current directory that are Git repos | |
# and whose official GitHub repo has migrated to main but you still have master or dev branches. | |
Push-Location | |
[string[]] $local:repos = @() | |
if (Test-Path -PathType Container .git) { | |
$repos += "$PWD" | |
} else { | |
$repos = Get-ChildItem -Directory -Recurse | | |
Where-Object { Test-Path -PathType Container (Join-Path "$_" ".git") } | | |
ForEach-Object FullName | |
} | |
foreach ($repo in $repos) { | |
Set-Location $repo | |
$local:officialRemote = "" | |
$local:remotes = git remote | |
$local:url = "" | |
foreach ($remote in $remotes) { | |
$url = git remote get-url $remote | |
$local:isOfficialRemote = | |
$url -like "https://github.com/aspnet/*" -or | |
$url -like "https://github.com/dotnet/*" -or | |
$url -like "https://github.com/dougbu/*" -or | |
$url -like "https://github.com/mono/*" -or | |
$url -like "https://github.com/signalr/*" -or | |
$url -like "git@github.com:aspnet/*" -or | |
$url -like "git@github.com:dotnet/*" -or | |
$url -like "git@github.com:dougbu/*" -or | |
$url -like "git@github.com:mono/*" -or | |
$url -like "git@github.com:signalr/*" | |
if ($isOfficialRemote) { | |
# Let's fetch the remote to make sure we know about all branches. | |
git fetch $remote --prune 2>&1 >$NUL | |
$officialRemote = $remote | |
break | |
} | |
} | |
if ($officialRemote -eq "") { | |
# This repo didn't have any remotes pointing to an official GitHub repo, so let's skip. | |
continue | |
} | |
# Does this remote have a branch called 'main'? | |
git rev-parse "$officialRemote/main" 2>&1 >$NUL | |
$local:branchExists = $? | |
if ($branchExists) { | |
foreach ($remote in $remotes) { | |
# Fix up our HEAD for this remote. | |
git remote set-head $remote --auto >$NUL | |
# Does this remote have a branch called 'master'? | |
git rev-parse "$remote/master" 2>&1 >$NUL | |
$branchExists = $? | |
if ($branchExists) { | |
Write-Output "Repo '$url' was migrated to 'main' but there is still a branch called '$remote/master' in $repo" | |
} | |
# Does this remote have a branch called 'dev'? | |
git rev-parse "$remote/dev" 2>&1 >$NUL | |
$branchExists = $? | |
if ($branchExists) { | |
Write-Output "Repo $url was migrated to 'main' but there is still a branch called '$remote/dev' in $repo" | |
} | |
} | |
# Does this repo have a local branch called 'master'? | |
git rev-parse master 2>&1 >$NUL | |
$branchExists = $? | |
if ($branchExists) { | |
Write-Output "Repo $url was migrated to 'main' but you still have a local branch called 'master' in $repo" | |
} | |
# Does this repo have a local branch called 'dev'? | |
git rev-parse dev 2>&1 >$NUL | |
$branchExists = $? | |
if ($branchExists) { | |
Write-Output "Repo $url was migrated to 'main' but you still have a local branch called 'dev' $repo" | |
} | |
} | |
else { | |
Write-Output "Repo $url was not migrated to 'main'." | |
} | |
} | |
Pop-Location |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment