|
| 1 | +using GitVersion.Common; |
| 2 | +using GitVersion.Configuration; |
| 3 | +using GitVersion.Core; |
| 4 | +using GitVersion.Extensions; |
| 5 | +using GitVersion.Git; |
| 6 | +using GitVersion.Logging; |
| 7 | + |
| 8 | +namespace GitVersion.VersionCalculation; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Version is extracted from older merge commits. |
| 12 | +/// BaseVersionSource is the commit where the message was found. |
| 13 | +/// </summary> |
| 14 | +internal sealed class MergeCommitVersionStrategy(ILog log, Lazy<GitVersionContext> contextLazy, |
| 15 | + IRepositoryStore repositoryStore, IIncrementStrategyFinder incrementStrategyFinder, IEffectiveBranchConfigurationFinder effectiveBranchConfigurationFinder, |
| 16 | + ITaggedSemanticVersionRepository taggedSemanticVersionRepository |
| 17 | + ) |
| 18 | + : IVersionStrategy |
| 19 | +{ |
| 20 | + private readonly ILog log = log.NotNull(); |
| 21 | + private readonly Lazy<GitVersionContext> contextLazy = contextLazy.NotNull(); |
| 22 | + private readonly IRepositoryStore repositoryStore = repositoryStore.NotNull(); |
| 23 | + private readonly IIncrementStrategyFinder incrementStrategyFinder = incrementStrategyFinder.NotNull(); |
| 24 | + private readonly IEffectiveBranchConfigurationFinder effectiveBranchConfigurationFinder = effectiveBranchConfigurationFinder.NotNull(); |
| 25 | + private readonly ITaggedSemanticVersionRepository taggedSemanticVersionRepository = taggedSemanticVersionRepository.NotNull(); |
| 26 | + |
| 27 | + private GitVersionContext Context => contextLazy.Value; |
| 28 | + |
| 29 | + public IEnumerable<BaseVersion> GetBaseVersions(EffectiveBranchConfiguration configuration) |
| 30 | + { |
| 31 | + configuration.NotNull(); |
| 32 | + |
| 33 | + if (!Context.Configuration.VersionStrategy.HasFlag(VersionStrategies.MergeCommit)) |
| 34 | + { |
| 35 | + yield break; |
| 36 | + } |
| 37 | + |
| 38 | + var taggedCommits = this.taggedSemanticVersionRepository.GetTaggedSemanticVersions( |
| 39 | + tagPrefix: Context.Configuration.TagPrefixPattern, |
| 40 | + format: Context.Configuration.SemanticVersionFormat, |
| 41 | + ignore: Context.Configuration.Ignore |
| 42 | + ); |
| 43 | + var previousVersion = new SemanticVersion(0, 0, 0); |
| 44 | + |
| 45 | + // Must Loop In Reverse To Ensure Correct Version Calculation |
| 46 | + foreach (var commit in configuration.Value.Ignore.Filter(Context.CurrentBranchCommits.ToArray()).Reverse()) |
| 47 | + { |
| 48 | + if (taggedCommits.Contains(commit)) |
| 49 | + { |
| 50 | + this.log.Debug($"Found tagged commit {commit}, adjusting previous version."); |
| 51 | + previousVersion = taggedCommits[commit].First().Value; |
| 52 | + } |
| 53 | + if (!commit.IsMergeCommit()) |
| 54 | + { |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + // Using Merge Message Since The Formats Are Identical |
| 59 | + if (!MergeMessage.TryParse(commit, Context.Configuration, out var mergeMessage)) |
| 60 | + { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + this.log.Info($"Found commit [{commit}] matching merge message format: {mergeMessage.FormatName}"); |
| 65 | + |
| 66 | + var currentBranch = this.repositoryStore.GetTargetBranch(mergeMessage.MergedBranch!.Friendly); |
| 67 | + var branchConfiguration = this.effectiveBranchConfigurationFinder.GetConfigurations(currentBranch, Context.Configuration).First(); |
| 68 | + var baseVersionSource = this.repositoryStore.FindMergeBase(commit.Parents[0], commit.Parents[1]); |
| 69 | + |
| 70 | + var label = branchConfiguration.Value.GetBranchSpecificLabel(mergeMessage.MergedBranch!.Friendly, ""); |
| 71 | + var increment = this.incrementStrategyFinder.DetermineIncrementedField( |
| 72 | + currentCommit: commit, |
| 73 | + baseVersionSource: baseVersionSource, |
| 74 | + shouldIncrement: true, |
| 75 | + configuration: branchConfiguration.Value, |
| 76 | + label: label |
| 77 | + ); |
| 78 | + |
| 79 | + yield return new BaseVersion($"Merge Commit From '{mergeMessage.MergedBranch?.Friendly}'", previousVersion |
| 80 | + ) |
| 81 | + { |
| 82 | + Operator = new() |
| 83 | + { |
| 84 | + Increment = increment, |
| 85 | + ForceIncrement = false, |
| 86 | + Label = label |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + previousVersion = previousVersion.Increment( |
| 91 | + increment, |
| 92 | + label, |
| 93 | + true |
| 94 | + ); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments