Improved switch-branch.ps1

This commit is contained in:
Markus Fleschutz 2021-04-01 14:10:20 +02:00
parent f02f1e726b
commit 49d81b01f2

View File

@ -1,33 +1,33 @@
#!/bin/powershell
<#
.SYNTAX ./switch-branch.ps1 [<branch>] [<repo-dir>]
.SYNTAX ./switch-branch.ps1 [<branch-name>] [<repo-dir>]
.DESCRIPTION switches the branch in the current/given Git repository (including submodules)
.LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0
#>
param($Branch = "", $RepoDir = "$PWD")
param($BranchName = "", $RepoDir = "$PWD")
if ($Branch -eq "") {
$Branch = read-host "Enter branch to switch to"
}
try {
& git --version
} catch {
write-error "ERROR: can't execute 'git' - make sure Git is installed and available"
exit 1
if ($BranchName -eq "") {
$BranchName = read-host "Enter name of branch to switch to"
}
try {
if (-not(test-path "$RepoDir" -pathType container)) { throw "Can't access directory: $RepoDir" }
set-location "$RepoDir"
& git --version
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
$Result = (git status)
if ($lastExitCode -ne "0") { throw "'git status' failed in $RepoDir" }
if ("$Result" -notmatch "nothing to commit, working tree clean") { throw "Repository is NOT clean: $Result" }
& git fetch --all --recurse-submodules
if ($lastExitCode -ne "0") { throw "'git fetch' failed" }
& git switch --recurse-submodules $Branch
if ($lastExitCode -ne "0") { throw "'git switch $Branch' failed" }
& git switch --recurse-submodules "$BranchName"
if ($lastExitCode -ne "0") { throw "'git switch $BranchName' failed" }
& git submodule update --init --recursive
if ($lastExitCode -ne "0") { throw "'git submodule update' failed" }