Updated new-branch.ps1 and switch-branch.ps1

This commit is contained in:
Markus Fleschutz 2024-06-20 15:34:42 +02:00
parent ad41aa48b2
commit 2599f9a6e5
2 changed files with 9 additions and 6 deletions

View File

@ -8,14 +8,14 @@
.PARAMETER pathToRepo .PARAMETER pathToRepo
Specifies the file path to the local Git repository (current working directory per default) Specifies the file path to the local Git repository (current working directory per default)
.EXAMPLE .EXAMPLE
PS> ./new-branch.ps1 test123 C:\Repos\rust PS> ./new-branch.ps1 test123
(1/6) Searching for Git executable... git version 2.45.0 (1/6) Searching for Git executable... git version 2.45.0
(2/6) Checking local repository... 📂C:\Repos\rust (2/6) Checking local repository... 📂C:\Repos\rust
(3/6) Fetching remote updates... (3/6) Fetching remote updates...
(4/6) Creating new branch... (4/6) Creating new branch...
(5/6) Pushing updates... (5/6) Pushing updates...
(6/6) Updating submodules... (6/6) Updating submodules...
Created branch 'test123' in repo 📂rust in 18s (based on 'main') Created branch 'test123' in 📂rust repo in 18s (based on 'main')
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
@ -39,7 +39,10 @@ try {
if ($lastExitCode -ne "0") { throw "'git status' in $pathToRepo failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'git status' in $pathToRepo failed with exit code $lastExitCode" }
$repoName = (Get-Item "$pathToRepo").Name $repoName = (Get-Item "$pathToRepo").Name
"⏳ (3/6) Fetching remote updates..." 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" }
& git -C "$pathToRepo" fetch --all --recurse-submodules --prune --prune-tags --force & git -C "$pathToRepo" fetch --all --recurse-submodules --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" }
@ -59,7 +62,7 @@ try {
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" }
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds [int]$elapsed = $stopWatch.Elapsed.TotalSeconds
"✔️ Created branch '$newBranch' in repo 📂$repoName in $($elapsed)s (based on '$currentBranch')" "✔️ Created branch '$newBranch' in 📂$repoName repo in $($elapsed)s (based on '$currentBranch')"
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"

View File

@ -2,13 +2,13 @@
.SYNOPSIS .SYNOPSIS
Switches the Git branch Switches the Git branch
.DESCRIPTION .DESCRIPTION
This PowerShell script switches to the given branch in a Git repository (also updates submodules). This PowerShell script switches to the given branch in a Git repository and also updates the submodules.
.PARAMETER branchName .PARAMETER branchName
Specifies the Git branch name to switch to Specifies the Git branch name to switch to
.PARAMETER pathToRepo .PARAMETER pathToRepo
Specifies the file path to the local Git repository Specifies the file path to the local Git repository
.EXAMPLE .EXAMPLE
PS> ./switch-branch main C:\Repos\rust PS> ./switch-branch main
(1/6) Searching for Git executable... git version 2.43.0.windows.1 (1/6) Searching for Git executable... git version 2.43.0.windows.1
(2/6) Checking local repository... C:\Repos\rust (2/6) Checking local repository... C:\Repos\rust
(3/6) Fetching remote updates... (3/6) Fetching remote updates...