2021-02-13 10:26:14 +01:00
|
|
|
#!/bin/powershell
|
|
|
|
<#
|
2021-04-01 14:10:20 +02:00
|
|
|
.SYNTAX ./switch-branch.ps1 [<branch-name>] [<repo-dir>]
|
2021-03-22 20:10:18 +01:00
|
|
|
.DESCRIPTION switches the branch in the current/given Git repository (including submodules)
|
|
|
|
.LINK https://github.com/fleschutz/PowerShell
|
|
|
|
.NOTES Author: Markus Fleschutz / License: CC0
|
2021-02-13 10:26:14 +01:00
|
|
|
#>
|
|
|
|
|
2021-04-01 14:10:20 +02:00
|
|
|
param($BranchName = "", $RepoDir = "$PWD")
|
2021-02-13 10:26:14 +01:00
|
|
|
|
2021-04-01 14:10:20 +02:00
|
|
|
if ($BranchName -eq "") {
|
|
|
|
$BranchName = read-host "Enter name of branch to switch to"
|
2021-02-13 10:26:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
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-01 14:10:20 +02:00
|
|
|
& git --version
|
|
|
|
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" }
|
|
|
|
if ("$Result" -notmatch "nothing to commit, working tree clean") { throw "Repository is NOT clean: $Result" }
|
|
|
|
|
2021-03-17 10:45:19 +01:00
|
|
|
& git fetch --all --recurse-submodules
|
2021-03-17 16:33:22 +01:00
|
|
|
if ($lastExitCode -ne "0") { throw "'git fetch' failed" }
|
2021-03-17 10:45:19 +01:00
|
|
|
|
2021-04-01 14:10:20 +02:00
|
|
|
& git switch --recurse-submodules "$BranchName"
|
|
|
|
if ($lastExitCode -ne "0") { throw "'git switch $BranchName' failed" }
|
2021-02-15 10:56:22 +01:00
|
|
|
|
2021-02-15 11:34:05 +01:00
|
|
|
& git submodule update --init --recursive
|
|
|
|
if ($lastExitCode -ne "0") { throw "'git submodule update' failed" }
|
|
|
|
|
2021-02-15 10:56:22 +01:00
|
|
|
& git pull --recurse-submodules
|
2021-02-15 11:34:05 +01:00
|
|
|
if ($lastExitCode -ne "0") { throw "'git pull' failed" }
|
2021-02-15 10:56:22 +01:00
|
|
|
|
2021-02-13 10:26:14 +01:00
|
|
|
exit 0
|
|
|
|
} catch {
|
2021-02-16 10:03:20 +01:00
|
|
|
write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
2021-02-13 10:26:14 +01:00
|
|
|
exit 1
|
|
|
|
}
|