Example #1
0
        public SemanticVersion FindVersion(GitVersionContext context)
        {
            // If current commit is tagged, don't do anything except add build metadata
            if (context.IsCurrentCommitTagged)
            {
                // Will always be 0, don't bother with the +0 on tags
                var semanticVersionBuildMetaData = metaDataCalculator.Create(context.CurrentCommit, context);
                semanticVersionBuildMetaData.CommitsSinceTag = null;
                var semanticVersion = new SemanticVersion(context.CurrentCommitTaggedVersion)
                {
                    BuildMetaData = semanticVersionBuildMetaData
                };
                return(semanticVersion);
            }

            var baseVersion = baseVersionFinder.GetBaseVersion(context);

            if (baseVersion.ShouldIncrement)
            {
                IncrementVersion(context, baseVersion);
            }
            else
            {
                Logger.WriteInfo("Skipping version increment");
            }

            if (!baseVersion.SemanticVersion.PreReleaseTag.HasTag() && !string.IsNullOrEmpty(context.Configuration.Tag))
            {
                UpdatePreReleaseTag(context, baseVersion);
            }

            baseVersion.SemanticVersion.BuildMetaData = metaDataCalculator.Create(baseVersion.BaseVersionSource, context);

            return(baseVersion.SemanticVersion);
        }
Example #2
0
        public SemanticVersion FindVersionInternal(GitVersionContext context)
        {
            SemanticVersion taggedSemanticVersion = null;

            if (context.IsCurrentCommitTagged)
            {
                // Will always be 0, don't bother with the +0 on tags
                var semanticVersionBuildMetaData = metaDataCalculator.Create(context.CurrentCommit, context);
                semanticVersionBuildMetaData.CommitsSinceTag = null;

                var semanticVersion = new SemanticVersion(context.CurrentCommitTaggedVersion)
                {
                    BuildMetaData = semanticVersionBuildMetaData
                };
                taggedSemanticVersion = semanticVersion;
            }

            var             baseVersion = baseVersionCalculator.GetBaseVersion(context);
            SemanticVersion semver;

            if (context.Configuration.VersioningMode == VersioningMode.Mainline)
            {
                semver = mainlineVersionCalculator.FindMainlineModeVersion(baseVersion, context);
            }
            else
            {
                semver = PerformIncrement(context, baseVersion);
                semver.BuildMetaData = metaDataCalculator.Create(baseVersion.BaseVersionSource, context);
            }

            var hasPreReleaseTag = semver.PreReleaseTag.HasTag();
            var branchConfigHasPreReleaseTagConfigured = !string.IsNullOrEmpty(context.Configuration.Tag);
            var preReleaseTagDoesNotMatchConfiguration = hasPreReleaseTag && branchConfigHasPreReleaseTagConfigured && semver.PreReleaseTag.Name != context.Configuration.Tag;

            if (!semver.PreReleaseTag.HasTag() && branchConfigHasPreReleaseTagConfigured || preReleaseTagDoesNotMatchConfiguration)
            {
                UpdatePreReleaseTag(context, semver, baseVersion.BranchNameOverride);
            }

            if (taggedSemanticVersion != null)
            {
                // replace calculated version with tagged version only if tagged version greater or equal to calculated version
                if (semver.CompareTo(taggedSemanticVersion, false) > 0)
                {
                    taggedSemanticVersion = null;
                }
                else
                {
                    // set the commit count on the tagged ver
                    taggedSemanticVersion.BuildMetaData.CommitsSinceVersionSource = semver.BuildMetaData.CommitsSinceVersionSource;
                }
            }

            return(taggedSemanticVersion ?? semver);
        }
Example #3
0
        public SemanticVersion FindVersion(GitVersionContext context)
        {
            SemanticVersion taggedSemanticVersion = null;

            // If current commit is tagged, don't do anything except add build metadata
            if (context.IsCurrentCommitTagged)
            {
                // Will always be 0, don't bother with the +0 on tags
                var semanticVersionBuildMetaData = metaDataCalculator.Create(context.CurrentCommit, context);
                semanticVersionBuildMetaData.CommitsSinceTag = null;

                var semanticVersion = new SemanticVersion(context.CurrentCommitTaggedVersion)
                {
                    BuildMetaData = semanticVersionBuildMetaData
                };
                taggedSemanticVersion = semanticVersion;
            }

            var             baseVersion = baseVersionFinder.GetBaseVersion(context);
            SemanticVersion semver;

            if (context.Configuration.VersioningMode == VersioningMode.Mainline)
            {
                var mainlineMode = new MainlineVersionCalculator(metaDataCalculator, log);
                semver = mainlineMode.FindMainlineModeVersion(baseVersion, context);
            }
            else
            {
                semver = PerformIncrement(context, baseVersion);
                semver.BuildMetaData = metaDataCalculator.Create(baseVersion.BaseVersionSource, context);
            }

            var hasPreReleaseTag = semver.PreReleaseTag.HasTag();
            var branchConfigHasPreReleaseTagConfigured = !string.IsNullOrEmpty(context.Configuration.Tag);
            var preReleaseTagDoesNotMatchConfiguration = hasPreReleaseTag && branchConfigHasPreReleaseTagConfigured && semver.PreReleaseTag.Name != context.Configuration.Tag;

            if (!semver.PreReleaseTag.HasTag() && branchConfigHasPreReleaseTagConfigured || preReleaseTagDoesNotMatchConfiguration)
            {
                UpdatePreReleaseTag(context, semver, baseVersion.BranchNameOverride);
            }


            if (taggedSemanticVersion != null)
            {
                // set the commit count on the tagged ver
                taggedSemanticVersion.BuildMetaData.CommitsSinceVersionSource = semver.BuildMetaData.CommitsSinceVersionSource;
            }

            return(taggedSemanticVersion ?? semver);
        }
Example #4
0
        public SemanticVersion FindVersion(GitVersionContext context)
        {
            SemanticVersion taggedSemanticVersion = null;

            // If current commit is tagged, don't do anything except add build metadata
            if (context.IsCurrentCommitTagged)
            {
                // Will always be 0, don't bother with the +0 on tags
                var semanticVersionBuildMetaData = metaDataCalculator.Create(context.CurrentCommit, context);
                semanticVersionBuildMetaData.CommitsSinceTag = null;



                var semanticVersion = new SemanticVersion(context.CurrentCommitTaggedVersion)
                {
                    BuildMetaData = semanticVersionBuildMetaData
                };
                taggedSemanticVersion = semanticVersion;
            }

            var baseVersion = baseVersionFinder.GetBaseVersion(context);
            var semver      = baseVersion.SemanticVersion;
            var increment   = IncrementStrategyFinder.DetermineIncrementedField(context, baseVersion);

            if (increment != null)
            {
                semver = semver.IncrementVersion(increment.Value);
            }
            else
            {
                Logger.WriteInfo("Skipping version increment");
            }

            if (!semver.PreReleaseTag.HasTag() && !string.IsNullOrEmpty(context.Configuration.Tag))
            {
                UpdatePreReleaseTag(context, semver, baseVersion.BranchNameOverride);
            }

            semver.BuildMetaData = metaDataCalculator.Create(baseVersion.BaseVersionSource, context);

            if (taggedSemanticVersion != null)
            {
                // set the commit count on the tagged ver
                taggedSemanticVersion.BuildMetaData.CommitsSinceVersionSource = semver.BuildMetaData.CommitsSinceVersionSource;
            }

            return(taggedSemanticVersion ?? semver);
        }
        public SemanticVersion FindMainlineModeVersion(BaseVersion baseVersion, GitVersionContext context)
        {
            if (baseVersion.SemanticVersion.PreReleaseTag.HasTag())
            {
                throw new NotSupportedException("Mainline development mode doesn't yet support pre-release tags on master");
            }

            using (log.IndentLog("Using mainline development mode to calculate current version"))
            {
                var mainlineVersion = baseVersion.SemanticVersion;

                // Forward merge / PR
                //          * feature/foo
                //         / |
                // master *  *
                //

                var mergeBase   = baseVersion.BaseVersionSource;
                var mainline    = GetMainline(context, baseVersion.BaseVersionSource);
                var mainlineTip = mainline.Tip;

                // when the current branch is not mainline, find the effective mainline tip for versioning the branch
                if (!context.CurrentBranch.IsSameBranch(mainline))
                {
                    mergeBase = FindMergeBaseBeforeForwardMerge(context, baseVersion.BaseVersionSource, mainline, out mainlineTip);
                    log.Info($"Current branch ({context.CurrentBranch.FriendlyName}) was branch from {mergeBase}");
                }

                var mainlineCommitLog = context.Repository.GetMainlineCommitLog(baseVersion, mainlineTip);
                var directCommits     = new List <Commit>(mainlineCommitLog.Count);

                // Scans commit log in reverse, aggregating merge commits
                foreach (var commit in mainlineCommitLog)
                {
                    directCommits.Add(commit);
                    if (commit.Parents.Count() > 1)
                    {
                        mainlineVersion = AggregateMergeCommitIncrement(context, commit, directCommits, mainlineVersion, mainline);
                    }
                }

                // This will increment for any direct commits on mainline
                mainlineVersion = IncrementForEachCommit(context, directCommits, mainlineVersion, mainline);
                mainlineVersion.BuildMetaData = metaDataCalculator.Create(mergeBase, context);

                // branches other than master always get a bump for the act of branching
                if (context.CurrentBranch.FriendlyName != "master")
                {
                    var branchIncrement = FindMessageIncrement(context, null, context.CurrentCommit, mergeBase, mainlineCommitLog);
                    log.Info($"Performing {branchIncrement} increment for current branch ");

                    mainlineVersion = mainlineVersion.IncrementVersion(branchIncrement);
                }

                return(mainlineVersion);
            }
        }
        public SemanticVersion FindMainlineModeVersion(BaseVersion baseVersion, GitVersionContext context)
        {
            if (baseVersion.SemanticVersion.PreReleaseTag.HasTag())
            {
                throw new NotSupportedException("Mainline development mode doesn't yet support pre-release tags on master");
            }

            using (Logger.IndentLog("Using mainline development mode to calculate current version"))
            {
                var mainlineVersion = baseVersion.SemanticVersion;

                // Forward merge / PR
                //          * feature/foo
                //         / |
                // master *  *
                //
                var mainlineTip          = GetMainlineTip(context);
                var commitsNotOnMainline = context.Repository.Commits.QueryByPath(context.PathFilter, new CommitFilter
                {
                    IncludeReachableFrom = context.CurrentBranch,
                    ExcludeReachableFrom = mainlineTip,
                    SortBy          = CommitSortStrategies.Time,
                    FirstParentOnly = true
                }).Where(c => c.Sha != baseVersion.BaseVersionSource.Sha && c.Parents.Count() == 1).ToList();

                var commitLog = context.Repository.Commits.QueryByPath(context.PathFilter, new CommitFilter
                {
                    IncludeReachableFrom = context.CurrentBranch,
                    ExcludeReachableFrom = baseVersion.BaseVersionSource,
                    SortBy          = CommitSortStrategies.Time,
                    FirstParentOnly = true
                })
                                .Where(c => c.Sha != baseVersion.BaseVersionSource.Sha)
                                .Except(commitsNotOnMainline)
                                .ToList();
                commitLog.Reverse();
                var directCommits = new List <Commit>();

                // Scans commit log in reverse, aggregating merge commits
                foreach (var commit in commitLog)
                {
                    directCommits.Add(commit);
                    if (commit.Parents.Count() > 1)
                    {
                        mainlineVersion = AggregateMergeCommitIncrement(context, commit, directCommits, mainlineVersion);
                    }
                }

                if (context.CurrentBranch.FriendlyName != "master")
                {
                    var mergedHead    = context.CurrentCommit;
                    var findMergeBase = context.Repository.ObjectDatabase.FindMergeBase(context.CurrentCommit, mainlineTip);
                    Logger.WriteInfo(string.Format("Current branch ({0}) was branch from {1}", context.CurrentBranch.FriendlyName, findMergeBase));

                    var branchIncrement = FindMessageIncrement(context, null, mergedHead, findMergeBase, directCommits);
                    // This will increment for any direct commits on master
                    mainlineVersion = IncrementForEachCommit(context, directCommits, mainlineVersion, "master");
                    mainlineVersion.BuildMetaData = metaDataCalculator.Create(findMergeBase, context);
                    // Don't increment if the merge commit is a merge into mainline
                    // this ensures PR's and forward merges end up correct.
                    if (mergedHead.Parents.Count() == 1 || mergedHead.Parents.First() != mainlineTip)
                    {
                        Logger.WriteInfo(string.Format("Performing {0} increment for current branch ", branchIncrement));
                        mainlineVersion = mainlineVersion.IncrementVersion(branchIncrement);
                    }
                }
                else
                {
                    // If we are on master, make sure no commits get left behind
                    mainlineVersion = IncrementForEachCommit(context, directCommits, mainlineVersion);
                    mainlineVersion.BuildMetaData = metaDataCalculator.Create(baseVersion.BaseVersionSource, context);
                }

                return(mainlineVersion);
            }
        }