PowerShell/Scripts/switch-branch.ps1

67 lines
2.6 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2022-10-26 14:56:25 +02:00
Switches the Git branch
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2022-01-30 10:49:30 +01:00
This PowerShell script switches to another branch in a Git repository (including submodules).
2023-09-28 11:20:44 +02:00
.PARAMETER branchName
2021-10-16 16:50:10 +02:00
Specifies the branch name
2023-09-28 11:20:44 +02:00
.PARAMETER repoDir
2022-11-23 14:57:39 +01:00
Specifies the path to the local Git repository
2021-07-13 21:10:02 +02:00
.EXAMPLE
2021-09-27 08:35:45 +02:00
PS> ./switch-branch main C:\MyRepo
2023-09-04 12:55:43 +02:00
(1/6) Searching for Git executable... git version 2.42.0.windows.1
(2/6) Checking local repository...
(3/6) Fetching updates...
(4/6) Switching to branch 'main'...
(5/6) Pulling updates...
(6/6) Updating submodules...
2023-09-28 11:20:44 +02:00
Switched repo 📂MyRepo to branch 'main' in 22 sec
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-30 10:49:30 +01:00
.NOTES
2022-04-26 14:36:01 +02:00
Author: Markus Fleschutz | License: CC0
2021-02-13 10:26:14 +01:00
#>
2023-09-28 11:20:44 +02:00
param([string]$branchName = "", [string]$repoDir = "$PWD")
2021-02-13 10:26:14 +01:00
try {
2023-09-28 11:20:44 +02:00
if ($branchName -eq "") { $branchName = Read-Host "Enter the branch name to switch to" }
if ($repoDir -eq "") { $repoDir = Read-Host "Enter the path to the local Git repository" }
2021-07-15 15:51:22 +02:00
2021-09-01 10:26:05 +02:00
$StopWatch = [system.diagnostics.stopwatch]::startNew()
2021-10-01 08:31:48 +02:00
2023-04-06 08:18:39 +02:00
Write-Host "⏳ (1/6) Searching for Git executable... " -noNewline
2022-08-10 14:42:07 +02:00
& git --version
2021-04-01 14:10:20 +02:00
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
2023-09-04 12:55:43 +02:00
Write-Host "⏳ (2/6) Checking local repository..."
2023-09-28 11:20:44 +02:00
$repoDir = Resolve-Path "$repoDir"
if (-not(Test-Path "$repoDir" -pathType container)) { throw "Can't access directory: $repoDir" }
2021-04-01 14:10:20 +02:00
$Result = (git status)
2023-09-28 11:20:44 +02:00
if ($lastExitCode -ne "0") { throw "'git status' in $repoDir failed with exit code $lastExitCode" }
2021-04-22 09:47:48 +02:00
if ("$Result" -notmatch "nothing to commit, working tree clean") { throw "Git repository is NOT clean: $Result" }
2023-09-28 11:20:44 +02:00
$repoDirName = (Get-Item "$repoDir").Name
2021-04-01 14:10:20 +02:00
2023-09-28 11:20:44 +02:00
"⏳ (3/6) Fetching latest updates..."
& git -C "$repoDir" fetch --all --prune --prune-tags --force
2022-05-13 08:58:47 +02:00
if ($lastExitCode -ne "0") { throw "'git fetch' failed with exit code $lastExitCode" }
2021-03-17 10:45:19 +01:00
2023-09-28 11:20:44 +02:00
"⏳ (4/6) Switching to branch '$branchName'..."
& git -C "$repoDir" checkout --recurse-submodules "$branchName"
if ($lastExitCode -ne "0") { throw "'git checkout $branchName' failed with exit code $lastExitCode" }
2021-02-15 10:56:22 +01:00
2022-10-26 14:56:25 +02:00
"⏳ (5/6) Pulling updates..."
2023-09-28 11:20:44 +02:00
& git -C "$repoDir" pull --recurse-submodules
2022-03-03 09:49:58 +01:00
if ($lastExitCode -ne "0") { throw "'git pull' failed with exit code $lastExitCode" }
2021-04-23 08:56:09 +02:00
2022-10-28 09:25:16 +02:00
"⏳ (6/6) Updating submodules..."
2023-09-28 11:20:44 +02:00
& git -C "$repoDir" submodule update --init --recursive
2022-05-13 08:58:47 +02:00
if ($lastExitCode -ne "0") { throw "'git submodule update' failed with exit code $lastExitCode" }
2021-02-15 11:34:05 +01:00
2021-09-01 10:26:05 +02:00
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
2023-09-28 11:20:44 +02:00
"✔️ Switched repo 📂$repoDirName to branch '$branchName' in $Elapsed sec"
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-02-13 10:26:14 +01:00
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-02-13 10:26:14 +01:00
exit 1
2022-07-29 08:53:29 +02:00
}