PowerShell/Scripts/switch-branch.ps1

59 lines
2.1 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2021-10-16 16:50:10 +02:00
Switches the branch in a Git repository (including submodules)
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2021-10-16 16:50:10 +02:00
This script switches to another branch in a Git repository (including submodules).
.PARAMETER BranchName
Specifies the branch name
.PARAMETER RepoDir
Specifies the path to the 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-08-29 17:50:03 +02:00
.NOTES
Author: Markus Fleschutz · License: CC0
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
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" }
2021-09-01 10:26:05 +02:00
$StopWatch = [system.diagnostics.stopwatch]::startNew()
2021-10-01 08:31:48 +02:00
2021-10-18 16:46:46 +02:00
"⏳ Checking requirements [step 1/3]... "
2021-04-22 09:55:54 +02:00
$RepoDir = resolve-path "$RepoDir"
2021-03-16 08:45:53 +01:00
if (-not(test-path "$RepoDir" -pathType container)) { throw "Can't access directory: $RepoDir" }
set-location "$RepoDir"
2021-02-28 18:49:53 +01:00
2021-04-17 11:29:32 +02:00
$Null = (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" }
$Result = (git status)
if ($lastExitCode -ne "0") { throw "'git status' failed in $RepoDir" }
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
2021-10-18 16:46:46 +02:00
"⏳ Fetching latest updates and switching branch [step 2/3]..."
2021-10-06 09:10:49 +02:00
& git fetch --all --recurse-submodules --prune --prune-tags --force
2021-06-17 08:27:20 +02:00
if ($lastExitCode -ne "0") { throw "'git fetch' failed" }
2021-03-17 10:45:19 +01:00
& git checkout --recurse-submodules "$BranchName"
2021-10-01 08:31:48 +02:00
if ($lastExitCode -ne "0") { throw "'git checkout $BranchName' failed" }
2021-02-15 10:56:22 +01:00
2021-10-18 16:46:46 +02:00
"⏳ Pulling updates and updating submodules [step 3/3]..."
& git pull --recurse-submodules
2021-04-23 08:56:09 +02:00
if ($lastExitCode -ne "0") { throw "'git pull' failed" }
2021-02-15 11:34:05 +01:00
& git submodule update --init --recursive
if ($lastExitCode -ne "0") { throw "'git submodule update' failed" }
2021-05-27 10:14:12 +02:00
$RepoDirName = (get-item "$RepoDir").Name
2021-09-01 10:26:05 +02:00
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"✔️ switched Git 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 {
2021-09-16 20:19:10 +02:00
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
2021-02-13 10:26:14 +01:00
exit 1
}