2024-10-01 13:37:53 +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
|
2024-06-20 15:34:42 +02:00
|
|
|
This PowerShell script switches to the given branch in a Git repository and also updates the submodules.
|
2023-09-28 11:20:44 +02:00
|
|
|
.PARAMETER branchName
|
2024-05-02 12:12:32 +02:00
|
|
|
Specifies the Git branch name to switch to
|
2024-02-22 07:26:22 +01:00
|
|
|
.PARAMETER pathToRepo
|
|
|
|
Specifies the file path to the local Git repository
|
2021-07-13 21:10:02 +02:00
|
|
|
.EXAMPLE
|
2024-06-20 15:34:42 +02:00
|
|
|
PS> ./switch-branch main
|
2024-02-22 07:26:22 +01:00
|
|
|
⏳ (1/6) Searching for Git executable... git version 2.43.0.windows.1
|
2024-06-19 10:03:34 +02:00
|
|
|
⏳ (2/6) Checking local repository... C:\Repos\rust
|
2024-04-22 12:33:26 +02:00
|
|
|
⏳ (3/6) Fetching remote updates...
|
2023-09-04 12:55:43 +02:00
|
|
|
⏳ (4/6) Switching to branch 'main'...
|
2024-04-22 12:33:26 +02:00
|
|
|
⏳ (5/6) Pulling remote updates...
|
2023-09-04 12:55:43 +02:00
|
|
|
⏳ (6/6) Updating submodules...
|
2024-09-24 20:53:46 +02:00
|
|
|
✅ Switched 📂rust repo to 'main' branch in 22s.
|
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" }
|
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-06-19 10:03:34 +02:00
|
|
|
Write-Host "⏳ (2/6) Checking local repository... $pathToRepo"
|
2024-03-19 13:14:58 +01:00
|
|
|
if (-not(Test-Path "$pathToRepo" -pathType container)) { throw "Can't access repo folder: $pathToRepo" }
|
2024-05-02 12:12:32 +02:00
|
|
|
$result = (git -C "$pathToRepo" status)
|
2024-02-22 07:26:22 +01:00
|
|
|
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-06-19 10:03:34 +02:00
|
|
|
Write-Host "⏳ (3/6) Fetching remote updates... " -noNewline
|
|
|
|
& git -C "$pathToRepo" remote get-url origin
|
|
|
|
if ($lastExitCode -ne "0") { throw "'git remote get-url origin' failed with exit code $lastExitCode" }
|
|
|
|
|
2024-02-22 07:26:22 +01:00
|
|
|
& 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
|
|
|
|
2024-04-22 12:33:26 +02:00
|
|
|
"⏳ (5/6) Pulling remote 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-09-24 20:53:46 +02:00
|
|
|
"✅ Switched 📂$repoDirName repo to '$branchName' branch in $($elapsed)s."
|
2021-09-27 10:09:45 +02:00
|
|
|
exit 0 # success
|
2021-02-13 10:26:14 +01:00
|
|
|
} catch {
|
2024-06-19 10:03:34 +02:00
|
|
|
"⚠️ Error: $($Error[0]) in script line $($_.InvocationInfo.ScriptLineNumber)"
|
2021-02-13 10:26:14 +01:00
|
|
|
exit 1
|
2022-07-29 08:53:29 +02:00
|
|
|
}
|