1
0
mirror of https://github.com/fleschutz/PowerShell.git synced 2025-03-28 23:56:52 +01:00

Update switch-branch.ps1

This commit is contained in:
Markus Fleschutz 2022-10-26 14:56:25 +02:00
parent d494d50d91
commit 32df71fd8d

View File

@ -1,6 +1,6 @@
<# <#
.SYNOPSIS .SYNOPSIS
Switches the branch in a Git repository Switches the Git branch
.DESCRIPTION .DESCRIPTION
This PowerShell script switches to another branch in a Git repository (including submodules). This PowerShell script switches to another branch in a Git repository (including submodules).
.PARAMETER BranchName .PARAMETER BranchName
@ -23,32 +23,32 @@ try {
$StopWatch = [system.diagnostics.stopwatch]::startNew() $StopWatch = [system.diagnostics.stopwatch]::startNew()
"⏳ Step 1/6 - Searching for Git executable..." Write-Host "⏳ (1/6) Searching for Git executable... " -noNewline
& git --version & git --version
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" } if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
$RepoDir = Resolve-Path "$RepoDir" $RepoDir = Resolve-Path "$RepoDir"
$RepoDirName = (Get-Item "$RepoDir").Name $RepoDirName = (Get-Item "$RepoDir").Name
"Step 2/6 - Checking folder 📂$RepoDirName..." "(2/6) Checking folder 📂$RepoDirName..."
if (-not(Test-Path "$RepoDir" -pathType container)) { throw "Can't access directory: $RepoDir" } if (-not(Test-Path "$RepoDir" -pathType container)) { throw "Can't access directory: $RepoDir" }
$Result = (git status) $Result = (git status)
if ($lastExitCode -ne "0") { throw "'git status' in $RepoDir failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'git status' in $RepoDir failed with exit code $lastExitCode" }
if ("$Result" -notmatch "nothing to commit, working tree clean") { throw "Git repository is NOT clean: $Result" } if ("$Result" -notmatch "nothing to commit, working tree clean") { throw "Git repository is NOT clean: $Result" }
"Step 3/6 - Fetching updates..." "(3/6) Fetching updates..."
& git -C "$RepoDir" fetch --all --prune --prune-tags --force & git -C "$RepoDir" fetch --all --prune --prune-tags --force
if ($lastExitCode -ne "0") { throw "'git fetch' failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'git fetch' failed with exit code $lastExitCode" }
"Step 4/6 - Switching branch..." "(4/6) Switching to branch '$BranchName'..."
& git -C "$RepoDir" checkout --recurse-submodules "$BranchName" & git -C "$RepoDir" checkout --recurse-submodules "$BranchName"
if ($lastExitCode -ne "0") { throw "'git checkout $BranchName' failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'git checkout $BranchName' failed with exit code $lastExitCode" }
"Step 5/6 - Pulling updates..." "(5/6) Pulling updates..."
& git -C "$RepoDir" pull --recurse-submodules & git -C "$RepoDir" pull --recurse-submodules
if ($lastExitCode -ne "0") { throw "'git pull' failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'git pull' failed with exit code $lastExitCode" }
"Step 6/6 - Updating submodules..." "(6/6) - Updating submodules..."
& git -C "$RepoDir" submodule update --init --recursive & git -C "$RepoDir" submodule update --init --recursive
if ($lastExitCode -ne "0") { throw "'git submodule update' failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'git submodule update' failed with exit code $lastExitCode" }