Improve switch-branch.ps1

This commit is contained in:
Markus Fleschutz 2021-09-01 10:26:05 +02:00
parent 1aef356a75
commit 0149a7e5a6

View File

@ -1,8 +1,8 @@
<#
.SYNOPSIS
switch-branch.ps1 [<branch-name>] [<repo-dir>]
switch-branch.ps1 [<BranchName>] [<RepoDir>]
.DESCRIPTION
Switches the branch in the current/given Git repository (including submodules).
Switches to another branch in a Git repository (including submodules).
.EXAMPLE
PS> .\switch-branch.ps1 main C:\MyRepo
.NOTES
@ -16,6 +16,7 @@ param([string]$BranchName = "", [string]$RepoDir = "$PWD")
try {
if ($BranchName -eq "") { $BranchName = read-host "Enter name of branch to switch to" }
$StopWatch = [system.diagnostics.stopwatch]::startNew()
$RepoDir = resolve-path "$RepoDir"
if (-not(test-path "$RepoDir" -pathType container)) { throw "Can't access directory: $RepoDir" }
set-location "$RepoDir"
@ -41,7 +42,8 @@ try {
if ($lastExitCode -ne "0") { throw "'git submodule update' failed" }
$RepoDirName = (get-item "$RepoDir").Name
"✔️ switched Git repository 📂$RepoDirName to branch '$BranchName'"
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"✔️ switched Git repository 📂$RepoDirName to $BranchName branch in $Elapsed sec"
exit 0
} catch {
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"