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).
|
2021-10-16 16:50:10 +02:00
|
|
|
|
.PARAMETER BranchName
|
|
|
|
|
Specifies the branch name
|
|
|
|
|
.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
|
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
|
|
|
|
#>
|
|
|
|
|
|
2021-07-15 15:51:22 +02:00
|
|
|
|
param([string]$BranchName = "", [string]$RepoDir = "$PWD")
|
2021-02-13 10:26:14 +01:00
|
|
|
|
|
|
|
|
|
try {
|
2021-07-15 15:51:22 +02:00
|
|
|
|
if ($BranchName -eq "") { $BranchName = read-host "Enter name of branch to switch to" }
|
2022-11-23 14:57:39 +01:00
|
|
|
|
if ($RepoDir -eq "") { $RepoDir = read-host "Enter 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
|
|
|
|
|
2022-10-26 14:56:25 +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" }
|
|
|
|
|
|
2022-08-10 14:42:07 +02:00
|
|
|
|
$RepoDir = Resolve-Path "$RepoDir"
|
2022-09-01 10:56:49 +02:00
|
|
|
|
$RepoDirName = (Get-Item "$RepoDir").Name
|
2022-12-13 14:02:22 +01:00
|
|
|
|
Write-Host "⏳ (2/6) Checking Git repository... 📂$RepoDirName"
|
2022-08-10 14:42:07 +02:00
|
|
|
|
if (-not(Test-Path "$RepoDir" -pathType container)) { throw "Can't access directory: $RepoDir" }
|
|
|
|
|
|
2021-04-01 14:10:20 +02:00
|
|
|
|
$Result = (git status)
|
2022-03-03 09:49:58 +01: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" }
|
2021-04-01 14:10:20 +02:00
|
|
|
|
|
2022-10-26 14:56:25 +02:00
|
|
|
|
"⏳ (3/6) Fetching updates..."
|
2022-07-29 08:53:29 +02:00
|
|
|
|
& 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
|
|
|
|
|
2022-12-13 14:02:22 +01:00
|
|
|
|
"⏳ (4/6) Switching branch..."
|
2022-05-13 08:58:47 +02:00
|
|
|
|
& git -C "$RepoDir" checkout --recurse-submodules "$BranchName"
|
2022-03-03 09:49:58 +01: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..."
|
2022-05-13 08:58:47 +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..."
|
2022-05-13 08:58:47 +02:00
|
|
|
|
& git -C "$RepoDir" submodule update --init --recursive
|
|
|
|
|
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
|
2022-12-16 11:27:24 +01:00
|
|
|
|
"✔️ switched to $BranchName branch in 📂$RepoDirName repository 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
|
|
|
|
}
|