|
| 1 | +$Prefix = "v?" |
| 2 | +$TagPattern = "$Prefix(?<version>\d+\.\d+\.\d+(?:\d+\.)?)" |
| 3 | + |
| 4 | +[ValidateSet("Major","Minor","Build","Revision")] |
| 5 | +[string]$Increment = "Build" |
| 6 | + |
| 7 | + |
| 8 | +function Get-Log { |
| 9 | + [CmdletBinding()] |
| 10 | + param ( |
| 11 | + [Parameter()] |
| 12 | + [ValidateNotNullOrEmpty()] |
| 13 | + [String]$Root = $Pwd |
| 14 | + ) |
| 15 | + end { |
| 16 | + $Path = [LibGit2Sharp.Repository]::Discover((Convert-Path $Root)) |
| 17 | + if(!$Path) { |
| 18 | + Write-Warning "The path is not in a git repository!" |
| 19 | + return |
| 20 | + } |
| 21 | + |
| 22 | + try { |
| 23 | + $repo = New-Object LibGit2Sharp.Repository $Path |
| 24 | + |
| 25 | + # # We have to transform the object to keep the data around after .Dispose() |
| 26 | + # $repo.Head | |
| 27 | + # Select-Object $BranchProperties | |
| 28 | + # ForEach-Object { $_.PSTypeNames.Insert(0,"PSGit.Branch"); $_ } |
| 29 | + |
| 30 | + $Log = $repo.Commits | Select-Object Id, |
| 31 | + @{name = "Branch"; expr = { $c = $_; $repo.Branches.Where{$c.Id -eq $_.Tip.Id} }}, |
| 32 | + @{name = "Tags"; expr = { $c = $_; $repo.Tags.Where{$c.Id -eq $_.Target.Id} }}, |
| 33 | + Parents, Author, |
| 34 | + @{name = "Date"; expr = { $_.Author.When}}, |
| 35 | + Message | |
| 36 | + ForEach-Object { $_.PSTypeNames.Insert(0, "PSGit.Commit"), $_ } |
| 37 | + |
| 38 | + Write-Verbose "Scanning $($Log.Count) commits" |
| 39 | + foreach($commit in $Log) { |
| 40 | + $commit.Parents = $Log.Where{$_.Id -in $commit.Parents.Id} |
| 41 | + foreach($parent in $commit.Parents) { |
| 42 | + if(!$parent.Branch) { |
| 43 | + $parent.Branch = $commit.Branch |
| 44 | + } |
| 45 | + # foreach($parent in $Log.Where{$_.Id -eq $p.Id}) { |
| 46 | + # if($parent.Children) { |
| 47 | + # $parent.Children += $commit |
| 48 | + # } else { |
| 49 | + # $parent | Add-Member -MemberType NoteProperty -Name Children -Value @($commit) |
| 50 | + # } |
| 51 | + # } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + $Commits = @($Log) |
| 56 | + [Array]::Reverse($Commits) |
| 57 | + |
| 58 | + [Version]$Version = "1.0.0" |
| 59 | + foreach($Commit in $Commits) { |
| 60 | + if ($Commit.Tags.Name -match $TagPattern) { |
| 61 | + [Version]$Version = $Matches["Version"] |
| 62 | + $CommitsSinceVersionSource = 0 |
| 63 | + $BranchesSinceVersionSource = 0 |
| 64 | + } else { |
| 65 | + # Find the previously tagged build version |
| 66 | + $CommitsSinceVersionSource = 0 |
| 67 | + $Parents = $Commit |
| 68 | + do { |
| 69 | + $Parents = @($Parents)[0].Parents |
| 70 | + $CommitsSinceVersionSource += 1 |
| 71 | + Write-Verbose "Check parent $(@($Parents)[0].Id) for tags" |
| 72 | + } while ($Parents.Tags.Name -notmatch $TagPattern) |
| 73 | + |
| 74 | + # We are always building the NEXT version |
| 75 | + if (@($Parents)[0].Tags.Name -match $TagPattern) { |
| 76 | + [Version]$Version = $Matches["Version"] |
| 77 | + $Version = [Version]::new($Version.Major, $Version.Minor + 1, 0, $CommitsSinceVersionSource) |
| 78 | + } |
| 79 | + |
| 80 | + switch -regex ($Commit.Branch.Name) { |
| 81 | + "master" { |
| 82 | + $Version = [Version]::new($Version.Major, $Version.Minor, $CommitsSinceVersionSource) |
| 83 | + } |
| 84 | + "features?" { |
| 85 | + # $Increment += "Build" |
| 86 | + if ($Commit.Parents[0].Branch.Name -eq "master") { |
| 87 | + $BranchesSinceVersionSource += 1 |
| 88 | + } |
| 89 | + $Version = [Version]::new($Version.Major, $Version.Minor, $BranchesSinceVersionSource, $CommitsSinceVersionSource) |
| 90 | + } |
| 91 | + "releases?" { |
| 92 | + if ($Commit.Branch.Name -match "releases?/$Prefix(?<version>\d+\.\d+\.\d+(?:\d+\.)?)") { |
| 93 | + [Version]$Version = $Matches["Version"] |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + $Commit | Add-Member -MemberType NoteProperty -Name Version -Value $Version |
| 100 | + $Commit | Add-Member -MemberType NoteProperty -Name CommitsSinceVersionSource -Value $CommitsSinceVersionSource |
| 101 | + $Commit | Add-Member -MemberType NoteProperty -Name BranchesSinceVersionSource -Value $BranchesSinceVersionSource |
| 102 | + } |
| 103 | + $Log |
| 104 | + } finally { |
| 105 | + if($null -ne $repo) { |
| 106 | + $repo.Dispose() |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments