|
| 1 | +using System.Text.RegularExpressions; |
| 2 | +using GitVersion.Common; |
| 3 | +using GitVersion.Extensions; |
| 4 | +using GitVersion.Logging; |
| 5 | +using GitVersion.Model.Configuration; |
| 6 | + |
| 7 | +namespace GitVersion; |
| 8 | + |
| 9 | +internal class MainlineBranchFinder |
| 10 | +{ |
| 11 | + private readonly Config configuration; |
| 12 | + private readonly ILog log; |
| 13 | + private readonly IEnumerable<KeyValuePair<string, BranchConfig>>? mainlineBranchConfigs; |
| 14 | + private readonly IGitRepository repository; |
| 15 | + private readonly RepositoryStore repositoryStore; |
| 16 | + |
| 17 | + |
| 18 | + public MainlineBranchFinder(RepositoryStore repositoryStore, |
| 19 | + IGitRepository repository, |
| 20 | + Config configuration, |
| 21 | + IEnumerable<KeyValuePair<string, BranchConfig>>? mainlineBranchConfigs, |
| 22 | + ILog log) |
| 23 | + { |
| 24 | + this.repositoryStore = repositoryStore.NotNull(); |
| 25 | + this.repository = repository.NotNull(); |
| 26 | + this.configuration = configuration.NotNull(); |
| 27 | + this.mainlineBranchConfigs = mainlineBranchConfigs; |
| 28 | + this.log = log.NotNull(); |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | + public IDictionary<string, List<IBranch>> FindMainlineBranches(ICommit commit) |
| 33 | + { |
| 34 | + var branchOriginFinder = new BranchOriginFinder(commit, this.repositoryStore, this.configuration, this.log); |
| 35 | + return this.repository.Branches |
| 36 | + .Where(BranchIsMainline) |
| 37 | + .Select(branchOriginFinder.BranchOrigin) |
| 38 | + .Where(bc => bc != BranchCommit.Empty) |
| 39 | + .GroupBy(bc => bc.Commit.Sha, bc => bc.Branch) |
| 40 | + .ToDictionary(group => group.Key, x => x.ToList()); |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + private bool BranchIsMainline(INamedReference branch) |
| 45 | + { |
| 46 | + var matcher = new MainlineConfigBranchMatcher(branch, this.log); |
| 47 | + return this.mainlineBranchConfigs?.Any(matcher.IsMainline) == true; |
| 48 | + } |
| 49 | + |
| 50 | + private class MainlineConfigBranchMatcher |
| 51 | + { |
| 52 | + private readonly INamedReference branch; |
| 53 | + private readonly ILog log; |
| 54 | + |
| 55 | + public MainlineConfigBranchMatcher(INamedReference branch, ILog log) |
| 56 | + { |
| 57 | + this.branch = branch; |
| 58 | + this.log = log; |
| 59 | + } |
| 60 | + |
| 61 | + public bool IsMainline(KeyValuePair<string, BranchConfig> mainlineBranchConfig) |
| 62 | + { |
| 63 | + var (_, value) = mainlineBranchConfig; |
| 64 | + if (value?.Regex == null) |
| 65 | + return false; |
| 66 | + |
| 67 | + var mainlineRegex = value.Regex; |
| 68 | + var branchName = this.branch.Name.WithoutRemote; |
| 69 | + var match = Regex.IsMatch(branchName, mainlineRegex); |
| 70 | + this.log.Info($"'{mainlineRegex}' {(match ? "matches" : "does not match")} '{branchName}'."); |
| 71 | + return match; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + private class BranchOriginFinder |
| 77 | + { |
| 78 | + private readonly ICommit commit; |
| 79 | + private readonly Config configuration; |
| 80 | + private readonly ILog log; |
| 81 | + private readonly IRepositoryStore repositoryStore; |
| 82 | + |
| 83 | + public BranchOriginFinder(ICommit commit, IRepositoryStore repositoryStore, Config configuration, ILog log) |
| 84 | + { |
| 85 | + this.repositoryStore = repositoryStore; |
| 86 | + this.commit = commit; |
| 87 | + this.configuration = configuration; |
| 88 | + this.log = log; |
| 89 | + } |
| 90 | + |
| 91 | + public BranchCommit BranchOrigin(IBranch branch) |
| 92 | + { |
| 93 | + var branchOrigin = FindBranchOrigin(branch); |
| 94 | + return branchOrigin == null |
| 95 | + ? BranchCommit.Empty |
| 96 | + : new BranchCommit(branchOrigin, branch); |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + private ICommit? FindBranchOrigin(IBranch branch) |
| 101 | + { |
| 102 | + if (branch.Tip == null) |
| 103 | + return null; |
| 104 | + |
| 105 | + var branchName = branch.Name.Friendly; |
| 106 | + var mergeBase = this.repositoryStore.FindMergeBase(branch.Tip, this.commit); |
| 107 | + if (mergeBase is not null) |
| 108 | + { |
| 109 | + this.log.Info($"Found merge base {mergeBase.Sha} for '{branchName}'."); |
| 110 | + return mergeBase; |
| 111 | + } |
| 112 | + |
| 113 | + var branchCommit = this.repositoryStore.FindCommitBranchWasBranchedFrom(branch, this.configuration); |
| 114 | + if (branchCommit != BranchCommit.Empty) |
| 115 | + { |
| 116 | + this.log.Info($"Found parent commit {branchCommit.Commit.Sha} for '{branchName}'."); |
| 117 | + return branchCommit.Commit; |
| 118 | + } |
| 119 | + |
| 120 | + this.log.Info($"Found no merge base or parent commit for '{branchName}'."); |
| 121 | + return null; |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments