From 137941e2b7e69e810db13c5f8014e6b14605d68f Mon Sep 17 00:00:00 2001 From: Joel Timothy Oh Date: Fri, 19 Jul 2024 20:41:19 +0000 Subject: [PATCH] Enhancement (generate): Add tag type support for function `Get-RepositoryReleasePrevious` --- README.md | 12 +--- .../Private/Get-RepositoryReleasePrevious.ps1 | 59 +++++++++++++------ 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index ec2eda2..99da196 100644 --- a/README.md +++ b/README.md @@ -101,17 +101,7 @@ The names of all available release notes variants that can be chosen from can be ##### Valid tags -At present, generating of release notes is only possible for tags following the format `MAJOR.MINOR.PATCH`, prepended with a lowercase `v`: - -```shell -# Valid tags -git tag v0.12.1 -git tag v1.0.12 - -# Invalid tags -git tag v1.0.12-alpha -git tag v1.0.12-beta.1 -``` +Generating of release notes supports all tag refs. Tags *need not* follow [Semantic Versioning](https://semver.org/) though the convention is recommended. #### Creating releases diff --git a/src/PSRepositoryReleaseManager/Private/Get-RepositoryReleasePrevious.ps1 b/src/PSRepositoryReleaseManager/Private/Get-RepositoryReleasePrevious.ps1 index f26879e..e634d55 100644 --- a/src/PSRepositoryReleaseManager/Private/Get-RepositoryReleasePrevious.ps1 +++ b/src/PSRepositoryReleaseManager/Private/Get-RepositoryReleasePrevious.ps1 @@ -8,44 +8,65 @@ function Get-RepositoryReleasePrevious { [Parameter(Mandatory=$false)] [ValidateNotNullOrEmpty()] [string]$Ref + , + [Parameter(Mandatory=$false)] + [ValidateSet( + "SemVer", + "All" + )] + [string]$TagType ) try { Push-Location $Path - $isRefTag = $false - if ($Ref) { - # Validate specified ref is an existing ref - "Validating ref '$Ref'" | Write-Verbose - git rev-parse "$Ref" | Write-Verbose - if ($LASTEXITCODE) { - throw "An error occurred." - } - "Verifying if ref '$Ref' is a tag" | Write-Verbose - git show-ref --verify "refs/tags/$Ref" 2> $null | Write-Verbose - if (!$LASTEXITCODE) { - $isRefTag = $true - } + "Ref: $Ref" | Write-Verbose + "TagType: $TagType" | Write-Verbose + if (!$Ref) { + "Using default ref 'HEAD'" | Write-Verbose + $Ref = 'HEAD' } + if (!$TagType) { + "Using default tag type 'All'" | Write-Verbose + $TagType = 'All' + } + # Validate specified ref is an existing ref + "Validating ref '$Ref'" | Write-Verbose + $commitSHA = git rev-parse "$Ref" + $commitSHA | Write-Verbose + if ($LASTEXITCODE) { + throw "An error occurred." + } + $isRefTagged = if (git tag --points-at "$Ref") { + "Ref '$Ref' is a tagged commit." | Write-Verbose + $true + }else { + "Ref '$Ref' is not a tagged commit." | Write-Verbose + $false + } # Retrieve previous release tags of the specified ref within the same git tree - "Retrieving info on previous release tags" | Write-Verbose - $tagsPreviousInfo = (git --no-pager log "$Ref" --date-order --simplify-by-decoration --pretty="format:%H %D") -split "`n" | % { - if ($_ -match '\s+tag:\s+(v\d+\.\d+\.\d+)(,\s+|$)') { + if ($TagType -eq 'SemVer') { + $tagPattern = 'v\d+\.\d+\.\d+' + }elseif ($TagType -eq 'All') { + $tagPattern = '.+?' + } + $tagsPreviousInfo = (git --no-pager log "$Ref" --date-order --simplify-by-decoration --pretty='format:%H %D') -split "`n" | % { + if ($_ -match "\s+tag:\s+($tagPattern)(,\s+|$)") { $_ } } "Previous release tags info:" | Write-Verbose $tagsPreviousInfo | Write-Verbose - if ($isRefTag -And @($tagsPreviousInfo).Count -eq 1) { + if ($isRefTagged -And @($tagsPreviousInfo).Count -eq 1) { "No previous tags for ref '$Ref' exists in the repository '$($Path)'." | Write-Verbose return } - $tagPreviousCommitSHA = if ($isRefTag) { # If specified ref is a tag, the previous tag is on the following line + $tagPreviousCommitSHA = if ($isRefTagged -And ($tagsPreviousInfo[0] | Select-String -Pattern $commitSHA)) { (@($tagsPreviousInfo)[1] -split "\s")[0] }else { (@($tagsPreviousInfo)[0] -split "\s")[0] } "Previous release commit SHA: $tagPreviousCommitSHA" | Write-Verbose - $tagsPrevious = git tag --points-at "$tagPreviousCommitSHA" | Sort-Object -Descending # Returns an array of tags if they point to the same commit + $tagsPrevious = git tag --points-at "$tagPreviousCommitSHA" | Sort-Object -Descending | ? { $_ -match $tagPattern } # Returns an array of tags if they point to the same commit "Previous release tag(s):" | Write-Verbose $tagsPrevious | Write-Verbose $tagsPrevious