1- <#
2- Tags a branch (default: main) with a version computed by GitVersion.
3- - If -Directory is omitted: script may run from any folder INSIDE a Git repo.
4- - If -Directory is provided: it MUST be the repo root.
5- #>
6-
71[CmdletBinding ()]
82param (
93 [string ] $Directory ,
@@ -18,9 +12,7 @@ $ErrorActionPreference = 'Stop'
1812
1913function Resolve-TargetDirectory {
2014 param ([string ]$Dir )
21-
2215 if ([string ]::IsNullOrWhiteSpace($Dir )) { return $PSScriptRoot }
23-
2416 if ([System.IO.Path ]::IsPathRooted($Dir )) {
2517 return (Resolve-Path - LiteralPath $Dir ).ProviderPath
2618 } else {
@@ -47,10 +39,7 @@ function Get-GitTopLevel {
4739}
4840
4941function Assert-GitRepository {
50- param (
51- [string ]$Path ,
52- [bool ] $RequireTopLevel
53- )
42+ param ([string ]$Path , [bool ]$RequireTopLevel )
5443 $inside = (git - C " $Path " rev- parse - -is - inside- work- tree 2> $null ).Trim()
5544 if ($inside -ne ' true' ) {
5645 if ($RequireTopLevel ) { throw " Path '$Path ' is not the root of a Git repository (or not a working tree)." }
@@ -84,33 +73,39 @@ function Get-GitVersionJson {
8473function Compute-BumpedVersion {
8574 param (
8675 [string ]$SemVerBase ,
87- [switch ]$Maj , [switch ]$Min , [switch ]$Pat
76+ [bool ]$BumpMajor ,
77+ [bool ]$BumpMinor ,
78+ [bool ]$BumpPatch
8879 )
89- $count = @ ($Maj , $Min , $Pat | Where-Object { $_ }).Count
80+
81+ $count = @ ($BumpMajor , $BumpMinor , $BumpPatch | Where-Object { $_ }).Count
9082 if ($count -gt 1 ) { throw " Provide only one of -BumpMajor, -BumpMinor, or -BumpPatch." }
9183 if ($count -eq 0 ) { return $null }
9284
9385 $numeric = $SemVerBase.Split (' -' , 2 )[0 ].Split(' +' , 2 )[0 ]
9486 if ($numeric -notmatch ' ^(?<maj>\d+)\.(?<min>\d+)\.(?<pat>\d+)$' ) {
9587 throw " Unable to parse SemVer '$SemVerBase ' for bumping."
9688 }
97- $maj = [int ]$Matches [' maj' ]; $min = [int ]$Matches [' min' ]; $pat = [int ]$Matches [' pat' ]
98- if ($Maj ) { $maj += 1 ; $min = 0 ; $pat = 0 }
99- elseif ($Min ) { $min += 1 ; $pat = 0 }
89+
90+ $maj = [int ]$Matches [' maj' ]
91+ $min = [int ]$Matches [' min' ]
92+ $pat = [int ]$Matches [' pat' ]
93+
94+ if ($BumpMajor ) { $maj += 1 ; $min = 0 ; $pat = 0 }
95+ elseif ($BumpMinor ) { $min += 1 ; $pat = 0 }
10096 else { $pat += 1 }
97+
10198 return " {0}.{1}.{2}" -f $maj , $min , $pat
10299}
103100
104101function Test-TagExistsLocal {
105102 param ([string ]$TagName )
106- # Uses exit code instead of try/catch
107103 $null = git show-ref -- verify -- quiet " refs/tags/$TagName "
108104 return ($LASTEXITCODE -eq 0 )
109105}
110106
111107function Test-TagExistsRemote {
112108 param ([string ]$TagName , [string ]$Remote = ' origin' )
113- # Returns non-empty output if remote has the tag
114109 $out = git ls- remote -- tags $Remote " refs/tags/$TagName " 2> $null
115110 return -not [string ]::IsNullOrWhiteSpace($out )
116111}
@@ -128,7 +123,6 @@ try {
128123 if ($root ) { Write-Host " Detected repo root: $root " - ForegroundColor DarkGray }
129124 }
130125
131- # Switch branch
132126 $currentBranch = (git rev- parse -- abbrev- ref HEAD).Trim()
133127 if ($currentBranch -ne $Branch ) {
134128 Write-Host " Checking out branch '$Branch ' (was '$currentBranch ')..." - ForegroundColor Cyan
@@ -144,7 +138,9 @@ try {
144138 $gv = Get-GitVersionJson
145139
146140 $bumped = Compute- BumpedVersion - SemVerBase $gv.SemVer `
147- - Maj:$BumpMajor - Min:$BumpMinor - Pat:$BumpPatch
141+ - BumpMajor:([bool ]$BumpMajor.IsPresent ) `
142+ - BumpMinor:([bool ]$BumpMinor.IsPresent ) `
143+ - BumpPatch:([bool ]$BumpPatch.IsPresent )
148144
149145 if ($bumped ) {
150146 $versionToTag = $bumped
@@ -156,7 +152,6 @@ try {
156152
157153 $tagName = if ($versionToTag -match ' ^[vV]\d' ) { $versionToTag } else { " v$versionToTag " }
158154
159- # ✅ Fixed: check exit codes / output rather than try/catch
160155 if (Test-TagExistsLocal $tagName ) { throw " Tag '$tagName ' already exists locally. Aborting." }
161156 if (Test-TagExistsRemote $tagName ) { throw " Tag '$tagName ' already exists on 'origin'. Aborting." }
162157
0 commit comments