Updated list-repos.ps1 and switch-branch.ps1

This commit is contained in:
Markus Fleschutz 2025-02-24 09:20:40 +01:00
parent fc487f5f3d
commit 37075acd8c
2 changed files with 13 additions and 13 deletions

View File

@ -8,9 +8,9 @@
.EXAMPLE
PS> ./list-repos.ps1 C:\MyRepos
REPOSITORY LATEST TAG BRANCH REMOTE URL STATUS
---------- ---------- ------ ---------- ------
📂cmake v3.30.2 master https://github.com/Kitware/CMake clean 0
REPOSITORY LATEST TAG BRANCH REMOTE URL STATUS
---------- ---------- ------ ---------- ------
📂cmake v3.30.2 master https://github.com/Kitware/CMake 0 clean
...
.LINK
https://github.com/fleschutz/PowerShell
@ -36,7 +36,7 @@ function ListRepos {
$status = (git -C "$dir" status --short)
if ("$status" -eq "") { $status = "✅clean" }
elseif ("$status" -like " M *") { $status = "changed" }
New-Object PSObject -property @{'REPOSITORY'="📂$dirName";'LATEST TAG'="$latestTag";'BRANCH'="$branch";'REMOTE URL'="$remoteURL";'STATUS'="$status$numCommits"}
New-Object PSObject -property @{'REPOSITORY'="📂$dirName";'LATEST TAG'="$latestTag";'BRANCH'="$branch";'REMOTE URL'="$remoteURL$numCommits";'STATUS'="$status"}
}
}
@ -44,9 +44,9 @@ try {
if (-not(Test-Path "$parentDir" -pathType container)) { throw "Can't access parent directory at: $parentDir" }
$null = (git --version)
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
if ($lastExitCode -ne 0) { throw "Can't execute 'git' - make sure Git is installed and available" }
ListRepos | Format-Table -property @{e='REPOSITORY';width=19},@{e='LATEST TAG';width=16},@{e='BRANCH';width=19},@{e='REMOTE URL';width=47},@{e='STATUS';width=12}
ListRepos | Format-Table -property @{e='REPOSITORY';width=19},@{e='LATEST TAG';width=16},@{e='BRANCH';width=19},@{e='REMOTE URL';width=50},@{e='STATUS';width=10}
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"

View File

@ -31,33 +31,33 @@ try {
Write-Host "⏳ (1/6) Searching for Git executable... " -noNewline
& git --version
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
if ($lastExitCode -ne 0) { throw "Can't execute 'git' - make sure Git is installed and available" }
Write-Host "⏳ (2/6) Checking local repository... $pathToRepo"
if (-not(Test-Path "$pathToRepo" -pathType container)) { throw "Can't access repo folder: $pathToRepo" }
$result = (git -C "$pathToRepo" status)
if ($lastExitCode -ne "0") { throw "'git status' in $pathToRepo failed with exit code $lastExitCode" }
if ($lastExitCode -ne 0) { throw "'git status' in $pathToRepo failed with exit code $lastExitCode" }
if ("$result" -notmatch "nothing to commit, working tree clean") { throw "Git repository is NOT clean: $result" }
$repoDirName = (Get-Item "$pathToRepo").Name
Write-Host "⏳ (3/6) Fetching remote updates... " -noNewline
& git -C "$pathToRepo" remote get-url origin
if ($lastExitCode -ne "0") { throw "'git remote get-url origin' failed with exit code $lastExitCode" }
if ($lastExitCode -ne 0) { throw "'git remote get-url origin' failed with exit code $lastExitCode" }
& git -C "$pathToRepo" fetch --all --prune --prune-tags --force
if ($lastExitCode -ne "0") { throw "'git fetch' failed with exit code $lastExitCode" }
if ($lastExitCode -ne 0) { throw "'git fetch' failed with exit code $lastExitCode" }
"⏳ (4/6) Switching to branch '$branchName'..."
& git -C "$pathToRepo" checkout --recurse-submodules "$branchName"
if ($lastExitCode -ne "0") { throw "'git checkout $branchName' failed with exit code $lastExitCode" }
if ($lastExitCode -ne 0) { throw "'git checkout $branchName' failed with exit code $lastExitCode" }
"⏳ (5/6) Pulling remote updates..."
& git -C "$pathToRepo" pull --recurse-submodules
if ($lastExitCode -ne "0") { throw "'git pull' failed with exit code $lastExitCode" }
if ($lastExitCode -ne 0) { throw "'git pull' failed with exit code $lastExitCode" }
"⏳ (6/6) Updating submodules..."
& git -C "$pathToRepo" submodule update --init --recursive
if ($lastExitCode -ne "0") { throw "'git submodule update' failed with exit code $lastExitCode" }
if ($lastExitCode -ne 0) { throw "'git submodule update' failed with exit code $lastExitCode" }
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
"✅ Switched 📂$repoDirName repo to '$branchName' branch in $($elapsed)s."