Update switch-branch.ps1

This commit is contained in:
Markus Fleschutz 2024-03-12 09:13:04 +01:00
parent c1104a14af
commit adc7c4b240

View File

@ -8,14 +8,14 @@
.PARAMETER pathToRepo .PARAMETER pathToRepo
Specifies the file path to the local Git repository Specifies the file path to the local Git repository
.EXAMPLE .EXAMPLE
PS> ./switch-branch main C:\MyRepo PS> ./switch-branch main C:\Repos\rust
(1/6) Searching for Git executable... git version 2.43.0.windows.1 (1/6) Searching for Git executable... git version 2.43.0.windows.1
(2/6) Checking Git repository... (2/6) Checking Git repository...
(3/6) Fetching updates... (3/6) Fetching updates...
(4/6) Switching to branch 'main'... (4/6) Switching to branch 'main'...
(5/6) Pulling updates... (5/6) Pulling updates...
(6/6) Updating submodules... (6/6) Updating submodules...
Switched repo 📂MyRepo to branch 'main' in 22 sec Switched repository 📂rust to 'main' branch in 22 sec.
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
@ -40,7 +40,7 @@ try {
$result = (git status) $result = (git 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" } if ("$result" -notmatch "nothing to commit, working tree clean") { throw "Git repository is NOT clean: $result" }
$pathToRepoName = (Get-Item "$pathToRepo").Name $repoDirName = (Get-Item "$pathToRepo").Name
"⏳ (3/6) Fetching updates..." "⏳ (3/6) Fetching updates..."
& git -C "$pathToRepo" fetch --all --prune --prune-tags --force & git -C "$pathToRepo" fetch --all --prune --prune-tags --force
@ -59,7 +59,7 @@ try {
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 [int]$elapsed = $stopWatch.Elapsed.TotalSeconds
"✔️ Switched repo 📂$pathToRepoName to branch '$branchName' in $elapsed sec." "✔️ Switched repository 📂$repoDirName to '$branchName' branch in $elapsed sec."
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"