PowerShell/scripts/switch-branch.ps1

67 lines
2.7 KiB
PowerShell
Raw Normal View History

2023-10-31 13:03:45 +01: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
2024-02-22 07:26:22 +01:00
This PowerShell script switches to a given branch in a Git repository (including updating submodules).
2023-09-28 11:20:44 +02:00
.PARAMETER branchName
2024-02-22 07:26:22 +01:00
Specifies the target branch name
.PARAMETER pathToRepo
Specifies the file path to the local Git repository
2021-07-13 21:10:02 +02:00
.EXAMPLE
2024-03-12 09:13:04 +01:00
PS> ./switch-branch main C:\Repos\rust
2024-02-22 07:26:22 +01:00
(1/6) Searching for Git executable... git version 2.43.0.windows.1
(2/6) Checking Git repository...
2023-09-04 12:55:43 +02:00
(3/6) Fetching updates...
(4/6) Switching to branch 'main'...
(5/6) Pulling updates...
(6/6) Updating submodules...
2024-03-12 09:13:04 +01:00
Switched repository 📂rust to 'main' branch 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
#>
2024-02-22 07:26:22 +01:00
param([string]$branchName = "", [string]$pathToRepo = "$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" }
2024-02-22 07:26:22 +01:00
if ($pathToRepo -eq "") { $pathToRepo = Read-Host "Enter the path to the local Git repository" }
2021-07-15 15:51:22 +02:00
2024-02-22 07:26:22 +01: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" }
2024-02-22 07:26:22 +01:00
Write-Host "⏳ (2/6) Checking Git repository..."
$pathToRepo = Resolve-Path "$pathToRepo"
if (-not(Test-Path "$pathToRepo" -pathType container)) { throw "Can't access directory: $pathToRepo" }
$result = (git status)
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" }
2024-03-12 09:13:04 +01:00
$repoDirName = (Get-Item "$pathToRepo").Name
2021-04-01 14:10:20 +02:00
2024-02-22 07:26:22 +01:00
"⏳ (3/6) Fetching updates..."
& git -C "$pathToRepo" 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'..."
2024-02-22 07:26:22 +01:00
& git -C "$pathToRepo" checkout --recurse-submodules "$branchName"
2023-09-28 11:20:44 +02:00
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..."
2024-02-22 07:26:22 +01:00
& git -C "$pathToRepo" 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..."
2024-02-22 07:26:22 +01:00
& git -C "$pathToRepo" 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
2024-02-22 07:26:22 +01:00
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
2024-03-12 09:13:04 +01:00
"✔️ Switched repository 📂$repoDirName to '$branchName' branch 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
}