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 #!/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) .DESCRIPTION switches the branch in the current/given Git repository (including submodules)
.LINK https://github.com/fleschutz/PowerShell .LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0 .NOTES Author: Markus Fleschutz / License: CC0
#> #>
param($Branch = "", $RepoDir = "$PWD") param($BranchName = "", $RepoDir = "$PWD")
if ($Branch -eq "") { if ($BranchName -eq "") {
$Branch = read-host "Enter branch to switch to" $BranchName = read-host "Enter name of branch to switch to"
}
try {
& git --version
} catch {
write-error "ERROR: can't execute 'git' - make sure Git is installed and available"
exit 1
} }
try { try {
if (-not(test-path "$RepoDir" -pathType container)) { throw "Can't access directory: $RepoDir" } if (-not(test-path "$RepoDir" -pathType container)) { throw "Can't access directory: $RepoDir" }
set-location "$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 & git fetch --all --recurse-submodules
if ($lastExitCode -ne "0") { throw "'git fetch' failed" } if ($lastExitCode -ne "0") { throw "'git fetch' failed" }
& git switch --recurse-submodules $Branch & git switch --recurse-submodules "$BranchName"
if ($lastExitCode -ne "0") { throw "'git switch $Branch' failed" } if ($lastExitCode -ne "0") { throw "'git switch $BranchName' failed" }
& git submodule update --init --recursive & git submodule update --init --recursive
if ($lastExitCode -ne "0") { throw "'git submodule update' failed" } if ($lastExitCode -ne "0") { throw "'git submodule update' failed" }